English 中文(简体)
如何在春季/冬眠中处理两个数据库插入一个单表?
原标题:How to handle two database insertions fom a single form in spring/ hibernate?

我有3个模范班

User.java

public class UserData {
private Integer userID;
private String userName;
private String userPassword;
//getters and setters..

和许可类类似..

Permission.java

public class PermissionsData {
private Integer permissionID;
private Integer userID;
private Integer moduleID;
private boolean permissionIsReadOnly;
private boolean permissionIsModify;
private boolean permissionIsFull;
//getters and setters

AMD 模块类如,

module.java

public class ModuleData {
private Integer modId;
private String modName;

我的数据库中有一些模块。 当我创建新用户时, 我列出这些模块并添加了复选框来设置权限。 在提交表单时, 我需要将用户数据插入到用户表格中, 并将每个模块的用户权限插入到权限表格中 。

现在我只把角色插入到数据库... 我的控制器就像...

@RequestMapping(value = "/addRole")
    public ModelAndView addNewRole()
    {
        ModelAndView mav = new ModelAndView("addNewRole");
        RoleData role = new RoleData();
        mav.getModelMap().put("roleDataObj", role);

        List<ModuleData> moduleList = moduleService.getAllModules();
        mav.getModelMap().addAttribute("ModuleList", moduleList);
        return mav;
    }

How can i achieve to add data to two tables on one submit?? i would also like to know about the mapping betwewen modules and permission I am really new to this spring and hibernate. So plz guide me with sample codes.. Thanks in advance.

问题回答

如果您在映射中设置了串联

@Cascade(Cascade.all)
private List<Permission> permissions;

简单到

public void saveNewUser(...)
{
    User user = new User();
    // fill properties
    Permission p = new Permission();
    // fill properties
    user.getPermissions().add(p);

    session.save(user);
}

更新: 上面的代码可以使用类结构。 重要部分是每个用户每个模式的许可的收集/ 映射 。

public class Permission {
    private boolean IsReadOnly;
    private boolean IsModify;
    private boolean IsFull;
}

public class Module {
    private Integer id;
    private String name;
}

public class User {
    private Integer id;
    private String name;
    private String password;
    private Map<Module, Permission> permissions;
}

绘图和绘图

<class name="User">
  ...
  <map name="permissions" cascade="all">
    <key column="UserId"/>
    <index-many-to-many column="ModuleId" class="Module"/>
    <composite-element class="Permission">
      <property name="IsReadOnly"/>
      <property name="IsModify"/>
      <property name="IsFull"/>
    </composite-element>
  </map>




相关问题
Matrix to Represent a Triangle in Screen Space

So i have a set of four points in 3D Space. P1 [0, 0, 0] P2 [128, 0, 0] P3 [0, 128, 0] P4 [128, 128, 0] Which I m then projecting orthographically to the screen effectively giving me two ...

Using LINQ To SQL with multiple database tables

I have an employee class with as follows: public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public string UserName { get; set; } ...

Mapping points from Euclician 2-space onto a Poincare disc

For some reason it seems that everyone writing webpages about Poincare discs is only concerned with how to represent lines and measure distances. I d like to morph a collection of 2D points (as ...

Fluent NHibernate, varbinary(max) and SQLite

I have a varbinary field in my sql server database that needs to be varbinary(max). I create my database with NHibernate and I use Fluent Nhibernate for my mappings. I also use SQLite for my unit ...

Javascript redirect

I am working with javascript and here is what I am trying to do: 1. Send a get request which looks like "http://localhost:8080/myapp/verify.htm?verifyId=Agkvhs" My requests reaches my verify.jsp in ...

热门标签