enter image description here There are two entities: User and Role - they relate many to many. Accordingly, there are three tables: users, roles, user_roles . The problem is that user_roles has its own id, which Hibernate does not want to generate when it assigns a new role to the user (an exception is thrown: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint ) I can not delete this id, because the base is not mine (test task). Tell me, how can I tell Hiberneyt to generate an ID?

  • Show DB schema. - enzo
  • Attached the schema of the base to the description - Anatoly Mikhailin

2 answers 2

Since you (for unknown reasons) need a surrogate key in a table that serves as an association between user and role , you need to create another entity. You can work with this entity both separately and through the User and Role entities. To be honest, the first option seems easier to me.

Here I blinded an example of a project on Github , I hope you will understand. Consider only that the embedded database and the autogeneration of the circuit were used. In Posgtre, in order to generate an ID through JPA, it seems that you need to explicitly specify the sequence, otherwise it does not find it, and you can omit the @ForeignKey annotation so that Hibernate hbm2ddl.auto does not generate a foreign key where it is not asked.

 @Entity @Table(name = "users") public class User { @Id @GeneratedValue private Long id; @ForeignKey(name = "none") @OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL) private List<UserRole> userRoles; // ... public List<Role> getRoles() { List<Role> roles = new ArrayList<>(); for ( UserRole userRole : userRoles ) { roles.add(userRole.getRole()); } return roles; } } @Entity @Table(name = "roles") public class Role { @Id @GeneratedValue private Long id; @OneToMany(mappedBy = "role") @ForeignKey(name = "none") private List<UserRole> userRoles; // ... } @Entity @Table(name = "user_role") public class UserRole implements Serializable { @Id @GeneratedValue private Long id; @Embedded private UserRoleKey key; @ManyToOne @ForeignKey(name = "none") @JoinColumn(name = "user_id", insertable = false, updatable = false) private User user; @ManyToOne @ForeignKey(name = "none") @JoinColumn(name = "role_id", insertable = false, updatable = false) private Role role; // ... } @Embeddable public class UserRoleKey implements Serializable { @Column(name = "user_id") private Long userID; @Column(name = "role_id") private Long roleID; // ... } 

Conclusion hbm2ddl.auto :

 create table roles (id bigint generated by default as identity, name varchar(255), primary key (id)) create table user_role (id bigint generated by default as identity, role_id bigint, user_id bigint, primary key (id)) create table users (id bigint generated by default as identity, login varchar(255), name varchar(255), password varchar(255), primary key (id)) 
  • Thank you, I finally did something like this - Anatoly Mikhailin

You can try something like this if I correctly understood the question:

  @ManyToMany @JoinTable( name="UserRoles", joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")}, inverseJoinColumns={@JoinColumn(name="role_id", referencedColumnName="id")})