生成复合PK空ID

时间:2018-06-19 13:28:00

标签: java hibernate spring-mvc jpa java-ee

Spring Boot:2.0.2

休眠核心:5.2.17

方言:SQLServer2012Dialect

当尝试使用JpaRepository save()方法保存DistributionActivities实体时,操作失败,并显示以下消息:

  

org.hibernate.id.IdentifierGenerationException:生成的null ID   for:class DistributionActivitiesEmails

父实体:

@Entity
@Table(name = "DistributionActivities")
public class DistributionActivities implements Serializable 
{   
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)    
    @Column(name ="ActivityID")
    private Long activityID;

    @OneToMany(cascade={CascadeType.ALL})
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name="activityID", referencedColumnName="activityID")
    private Set<DistributionActivitiesEmails> distributionActivitiesEmails;
}

子实体:

@Entity
@Table(name = "DistributionActivitiesEmails ")
public class DistributionActivitiesEmails implements Serializable 
{
    @EmbeddedId
    private DistributionActivitiesEmailsId id;

    @Length(max = 50)
    private String EmailAdress;
}

复合PK:

@Embeddable
public class DistributionActivitiesEmailsId implements Serializable 
{
    @ManyToOne
    @JoinColumn(name="ActivityID")
    private DistributionActivities distributionActivities;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)    
    @Column(name ="ActivityEmailID")
    private Long activityEmailID;    
} 

1 个答案:

答案 0 :(得分:0)

根据@EmbeddedId javadoc,不支持在嵌入式id类中定义的关系映射。