I have two tables with foreign key relations. I\'ve tried searching on how to do it and it always leads to OneToMany & ManyToOne mapping. I have these two tables.
<
I can re-create your exception by mixing my annotation mapping between fields and properties. I see in the problem you described above you annotate the field Id, but then annotate the property userRole, if I do the same I get the same error. I can fix the error by either annotating just properties or fields. Both these models work:
@Entity
@Table(name = "USER")
public class User {
private Long id;
private UserRole userRole;
String userName;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "USER_NAME")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@ManyToOne()
@JoinColumn(name="role_id")
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
}
This also works:
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne()
@JoinColumn(name="role_id")
private UserRole userRole;
@Column(name = "USER_NAME")
String userName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
}
Example of the other model object annotating the fields
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne()
@JoinColumn(name="role_id")
private UserRole userRole;
@Column(name = "USER_NAME")
String userName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
}
Test Code (I added this because there is often confusion that one must set the bidirectional relationship in order to get both users and userRole to persist with a save to the UserRole).
List users = new ArrayList<>();
User user1 = new User();
user1.setUserName("user1");
users.add(user1);
User user2 = new User();
user2.setUserName("user2");
users.add(user2);
UserRole userRole = new UserRole();
userRole.setRoleName("admin");
//Unidirectional relationship
user1.setUserRole(userRole);
user2.setUserRole(userRole);
//set Bidirectional relationship
userRole.setUsers(users);
userRole = userRoleRepository.save(userRole);
//Show that the two users and the UserRole persisted
UserRole result = userRoleRepository.findById(userRole.getId()).get();
assertEquals(2, result.getUsers().size());