繁体   English   中英

在对应的映射表中保存多对多关系

[英]save many-to-many relationship in corresponding mapping-table

在我的Spring-Boot应用程序中,我想在数据库的对应映射表中保存一个User-Role 多对多关系 ,但是hibernate给我一条错误消息。

我的User.java类:

@Entity
@Table(name = "users")
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  Long id;
  String firstname;
  String lastname;
  String username;
  String password;

  @ManyToMany(mappedBy = "users")
  private Set<Role> roles;
}

我的Role.java类:

@Entity
@Table(name = "roles")
public class Role {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  Long id;
  String name;
  String description;

  @ManyToMany(cascade = CascadeType.ALL)
  @JoinTable(
          name = "role_users", 
          joinColumns = 
              @JoinColumn(name = "users_id", referencedColumnName = "id"), 
          inverseJoinColumns = 
              @JoinColumn(name = "roles_id", referencedColumnName = "id")
  )
  private Set<User> users;
}

我的运行方法:

@Override
@Transactional
public void run(String... strings) throws Exception {
    //add new Roles
    Role roleA = new Role("Rolle A");
    Role roleB = new Role("Rolle B");
    Role roleC = new Role("Rolle C");

    //add new Users
    User userA = new User("FirstnameA", "LastnameA", "UsernameA", "PasswordA");
    User userB = new User("FirstnameB", "LastnameB", "UsernameB", "PasswordB");

    //add new Lists of Roles
    Set<Role> rolesA = new HashSet<Role>();
    rolesA.add(roleA);
    rolesA.add(roleB);
    Set<Role> rolesB = new HashSet<Role>();
    rolesB.add(roleA);
    rolesB.add(roleC);

    //add a list of roles in one user, two times
    userA.setRoles(rolesA);
    userB.setRoles(rolesB);

    //save both users in db
    userRepository.save(userA); //rolle AB
    userRepository.save(userB); //rolle AC

    //give each role the corresponding user
    Set<User> usersA = new HashSet<User>();
    usersA.add(userA);
    usersA.add(userB);
    roleA.setUsers(usersA);
    roleRepository.save(roleA);
    Set<User> usersB = new HashSet<User>();
    usersB.add(userA);
    roleB.setUsers(usersB);
    roleRepository.save(roleB);
    Set<User> usersC = new HashSet<User>();
    usersC.add(userB);
    roleC.setUsers(usersC);
    roleRepository.save(roleC);

}

休眠给我一个错误信息:

不能添加或更新子行,外键约束失败( projekt_grarole_users ,约束fk_roleusers_user外键( users_id )参考文献usersid )ON DELETE CASCADE ON UPDATE CASCADE)

我有此站点的构造,并且可以使用它,但是在我的代码中它不起作用。

我总是通过将2个@ManyToMany关系重写为@OneToMany到一个新的实体来解决这个问题,该实体为我提供了介于两者之间的表。

一个很好的理由是,当我拥有@ManyToMany关系时,就永远无法像获得carthetic产品那样真正轻松地获得想要的东西。 将此表作为一个实体完全可以消除此问题。

因此,总结一下:

  • 您已经有了UserRole类,几乎可以了
  • 创建一个新实体: UserRole ,其属性:User user和Role role
  • 用户角色中的两个@ManyToMany更改为@OneToMany的新@Entity UserRole
  • 将关系添加到UserRole中 :2 @ManyToOne关系到UserRole

现在,问题解决了,而且还有:可以查询UserRole关系。

我解决了! 多亏了一位朋友,我解决了我的问题。 如果有人感兴趣:我试图先在存储库中保存用户,然后再保存角色。 而且由于该角色在我要保存用户的那一刻不存在,因此Hibernate给了我该错误消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM