ManyToOne 和 OneToMany 时生成的密钥看起来不对

Generated Key looks wrong when ManyToOne and OneToMany

我有两个具有关系的实体,UserEntity:

@Embeddable
public class UserId extends EntityId implements Serializable {

   @Column( length = CCEntity.ID_MAX_SIZE, name = ccIdCN )
   private String ccId;

   @Column( length = NAME_MAX_SIZE, name = userIdCN )
   private String userId;

   ...
}

@Entity
@Table(name = TableNames.CC_Users)
public class UserEntity {

   @EmbeddedId
   private UserId id;

...

   @OneToMany(targetEntity = ProfileEntity.class, mappedBy = "user", fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.MERGE })
   private List<ProfileEntity> profiles;

和 ProfileEntity:

@Embeddable
public class ProfileId extends EntityId implements Serializable {

   @Column( length = CCEntity.ID_MAX_SIZE, name = ccIdCN )
   private String ccId;

   @Column( length = NAME_MAX_SIZE, name = profileIdCN )
   private String profileId;

   ....
}

@Entity
@Table(name = TableNames.CC_Profile)
public class ProfileEntity {

   @EmbeddedId
   protected ProfileId id;

...

   @ManyToOne 
   @JoinColumns(value = { 
      @JoinColumn( nullable = true, name = Columns.referenceIdCN, referencedColumnName = UserId.userIdCN ),
      @JoinColumn( nullable = true, name = Columns.ccIdOfReferenceCN, referencedColumnName = UserId.ccIdCN ),
   })
   private UserEntity user;

当 JPA 创建 tables 时,它会生成以下内容: Table CC_USER 主键:cc_id、user_id。那是对的。 Table CC_PROFILE 主键:cc_id、user_id、profile_id。这里不明白JPA为什么要加user_id列为主键。 table 也有列:reference_idcc_id_of_reference_id 设置为可为空。

我希望 属性 ProfileEntity 的用户是可选的,或者可以为空。如果我尝试添加一个用户为 null 的实体,我会得到:

Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "user_id" violates not-null constraint

感谢您的帮助

终于找到问题了。我有另一个实体,其表名与配置文件实体相同,但 ID 不同。该 ID 包含未预期的 user_id 列。