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

时间:2018-04-25 13:45:32

标签: hibernate many-to-one composite-primary-key

我有这两个表:

enter image description here

我正在尝试使用JPA / Hibernate映射它们: ENTITY:

@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$4.perform(EntityManagerFactoryBuilderImpl.java:857)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.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。 当我用它Task的真实ID替换Long fiels时,它工作正常,但我失去了@ManyToOne关系。

有关如何正确映射的任何建议吗?

UPD: 任务实体:

    @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;
}

1 个答案:

答案 0 :(得分:2)

在自己的 ManyToOne 类中映射关系(GlobalTaskDependenc.java)并定义仅具有外键字段类型的Embeddable PrimaryKey类。

@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;
}