带继承的JPA ManyToOne映射

时间:2016-07-27 11:23:46

标签: java hibernate jpa

有几个具有共同属性的实体,因此提取了常见的参数,如下面的代码段。

@MappedSuperclass()
public class BaseCustomerDemographicDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected long id;

    @ManyToOne
    @JoinColumn(name = "GROUP")
    protected Group group;

}

实体摘录如下。

@Entity
@Table(name = "INDIVIDUAL_CUSTOMER")
@Access(AccessType.FIELD)
public class IndividualCustomer extends BaseCustomerDemographicDetails {

    @NotNull
    @Embedded
    private PersonalInformation personalInformation;

    @Column(precision = 16, scale = 2)
    private BigDecimal annualIncome;

    private Boolean vip;

    private Boolean isStaff;

...
}
    @Entity
    @Access(AccessType.FIELD)
    @Table(name = "GROUP")
    public class Group {

        protected Group(){}

        @Id
        @NotNull
        @Column(name = "GROUPID")
        private String groupId;

        @NotNull
        @Column(name="GROUPNAME")
        private String groupName;

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "group")
        private List<BaseCustomerDemographicDetails> customerDemographicDetails;

    }

同样的OneToMany映射会在statrtup期间导致Hibernate异常,如下所示。

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:860)
 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:343)
 at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:319)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
 ... 16 common frames omitted
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: xx.xxx.xxxxxxx.xxxxxxxxx.domain.model.Group.customerDemographicDetails[xx.xxx.xxxxx.xxxxxxx.domain.model.BaseCustomerDemographicDetails]
 at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1135)
 at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:794)
 at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:729)
 at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
 at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
 at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
 at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
 ... 24 common frames omitted

官方文档与我尝试的内容同步,但没有运气,请帮助!!!!

http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html Stack:Spring Boot with Spring Data JPA

1 个答案:

答案 0 :(得分:1)

您无法将关系映射到@MappedSuperclass。请查看参考文档here@MappedSuperclass允许您仅在域模型中实现继承。从数据库的角度来看,这使它成为一个实现细节。 @Inheritance可以很容易地将通用字段和注释分组到一个通常是抽象的超类中,但是你不能与它建立关系。

如果您使用import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; @Entity(name = "BaseCustomerDemographicDetails") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class BaseCustomerDemographicDetails { @Id @GeneratedValue(strategy = GenerationType.AUTO) protected long id; @ManyToOne @JoinColumn(name = "GROUP_OF_CUSTOMERS") protected Group groupOfCustomers; } ,您可以放弃此映射(请注意我必须将GROUP更改为其他内容,因为它是大多数数据库中的保留字):

BaseCustomerDemographicDetails.java

import java.util.List;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Access(AccessType.FIELD)
@Table(name = "GROUP_OF_CUSTOMERS")
public class Group {

    protected Group() {
    }

    @Id
    @NotNull
    @Column(name = "GROUPID")
    private String groupId;

    @NotNull
    @Column(name = "GROUPNAME")
    private String groupName;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "groupOfCustomers")
    private List<BaseCustomerDemographicDetails> customerDemographicDetails;

}

Group.java

import java.math.BigDecimal;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
@Access(AccessType.FIELD)
public class IndividualCustomer extends BaseCustomerDemographicDetails {

    @NotNull
    @Embedded
    private PersonalInformation personalInformation;

    @Column(precision = 16, scale = 2)
    private BigDecimal annualIncome;

    private Boolean vip;

    private Boolean isStaff;

}

IndividualCustomer.java(无变化)

{{1}}