我有一个子表(UserRoleMappings),它是UserProfile和UserRoles表的子表。 UserRoleMappings是一个表,其中映射UserProfile.userId和UserRoles.roleId。当我尝试向UserRoleMappings表中插入新记录时,出现以下错误。
ORA-01400:无法将NULL插入Corp.UserRoleMappings.USER_ID
下面是这三个实体的结构。
UserProfile.java
@Entity
@Table(name="UserProfile",schema="corp")
public class UserProfile implements Serializable {
@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_users_profile")
@SequenceGenerator(name="seq_users_profile",sequenceName="corp.seq_users_profile",allocationSize=20)
private Long userId;
// other fields ...
// getters and setters ...
}
UserRole.java
@Entity
@Table(name="UserRoles",schema="corp")
public class UserRole implements Serializable {
@Id
@Column(name="role_id")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="seq_userroles")
@SequenceGenerator(name="seq_userroles",sequenceName="corp.seq_userroles",allocationSize=20)
private Long roleId;
// other fields ...
// getters and setters ...
}
UserRoleMapping.java
@Entity
@Table(name="UserRoleMappings",schema="corp")
public class UserRoleMapping implements Serializable {
@Id
@ManyToOne
@JoinColumn(name="role_id",referencedColumnName = "role_id",nullable = false)
private UserRole userRole;
@Id
@ManyToOne
@JoinColumn(name="user_id",referencedColumnName = "user_id",nullable = false)
private UserProfile userProfile;
// other fields ...
// getters and setters ...
}
当我尝试在父表中已经有相应记录的UserRoleMapping表中插入新记录时,出现错误。仅对将INSERT插入此表失败。 UPDATE工作正常。我使用下面的代码插入记录。
UserRoleMapping userRoleMapping = new UserRoleMapping();
userRoleMapping.setUserRole(userRoleEntity); //userRoleEntity is an instance of UserRole
userRoleMapping.setUserProfile(userProfileEntity); //userProfileEntity is an instance of UserProfile
entityManager.merge(userRoleMapping);
entityManager.flush();
我不明白为什么休眠无法识别我设置的值。我只需要在子级中插入记录,因为相应的父级记录已经存在。请帮助我解决此问题。