@Embeddable 具有复杂字段的复合主键

@Embeddable composite primary key with complex fields

我有这两个表:

我正在尝试用 JPA/Hibernate 映射它们,如下所示: 实体:

@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "global_task_dependency")
public class GlobalTaskDependency implements Serializable {
    private static final long serialVersionUID = 1901782042815898352L;

    @EmbeddedId
    private GlobalTaskDependencyPK id;
}

PK:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Embeddable
public class GlobalTaskDependencyPK implements Serializable {
    private static final long serialVersionUID = -2939678921007532701L;

    @ManyToOne
    @JoinColumn(name = "global_task_id")
    private Task globalTask;
    @ManyToOne
    @JoinColumn(name = "parent_task_id")
    private Task parentTask;
    @ManyToOne
    @JoinColumn(name = "child_task_id")
    private Task childTask;
}

异常:

Caused by: java.lang.NullPointerException
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1499)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1422)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:857)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:382)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 97 more

每次我尝试 运行 应用程序时都会遇到 NullPointerExeption。 当我用它的真实 ID Long 替换 Task 字段时,它工作正常,但我失去了 @ManyToOne 关系。

有什么正确映射的建议吗?

更新: 任务实体:

    @Data
    @Entity
    @Table(name = TASK_TABLE)
    @Inheritance(strategy = InheritanceType.JOINED)
    public class Task implements Serializable {

        private static final long serialVersionUID = 8412873233077674082L;

        @Id
        @GeneratedValue
        private Long id;

        @Version
        private int version = 1;

        @Column(name = "taskstatus_id")
        private TaskStatus taskStatus;

        @ManyToOne
        @JoinColumn(name = "tasktype_id")
        private TaskType taskType;

        @Column(name = "creation_date")
        private Date creationDate;
}

映射 GlobalTaskDependenc.java 中的关系 (ManyToOne) class 并定义可嵌入的 PrimaryKey class 只有外键字段类型。

@Table(name = "global_task_dependency")
public class GlobalTaskDependency implements Serializable {
    private static final long serialVersionUID = 1901782042815898352L;

    @EmbeddedId
    private GlobalTaskDependencyPK id;


    @ManyToOne
    @JoinColumn(name = "global_task_id")
    private Task globalTask;
    @ManyToOne
    @JoinColumn(name = "parent_task_id")
    private Task parentTask;
    @ManyToOne
    @JoinColumn(name = "parent_task_id")
    private Task childTask;

}

@Embeddable
public class GlobalTaskDependencyPK implements Serializable {
    private static final long serialVersionUID = -2939678921007532701L;


    @Column(name = "global_task_id")
    private Long globalTask;

    @Column(name = "parent_task_id")
    private Long parentTask;

    @Column(name = "parent_task_id")
    private Long childTask;
}