查询时,Hibernate自引用实体重复字段

时间:2017-05-20 00:38:41

标签: java sql hibernate

我有这个实体。

@Entity
@Table(name = "CarePlan")
public class CarePlan extends Updateable{

    @Column(name = "careplan_system")
    private String system;

    @Column(name =  "careplan_code",unique = true)
    private String code;

    @Column(name ="description")
    private String description;

    @OneToMany(fetch = FetchType.EAGER,mappedBy = "carePlan")
    private Set<CarePlan> subsections = new HashSet<>();

    @ManyToOne(cascade={CascadeType.ALL})
    @JoinColumn(name="carePlan_code")
    private CarePlan carePlan;

    public String getSystem() {
        return system;
    }

    public void setSystem(String system) {
        this.system = system;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<CarePlan> getSubsections() {
        return subsections;
    }

    public void setSubsections(Set<CarePlan> subsections) {
        this.subsections = subsections;
    }

    public CarePlan getCarePlan() {
        return carePlan;
    }

    public void setCarePlan(CarePlan carePlan) {
        this.carePlan = carePlan;
    }
}

但是,我一直收到此错误

nested exception is org.hibernate.DuplicateMappingException: Table [care_plan] contains logical column name [carePlan_code] referring to multiple physical column names: [careplan_code], [care_plan_code]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1589)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:554)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175)
        at com.siva.homehealth.SivaHomeHealthApplication.main(SivaHomeHealthApplication.java:47)
Caused by: org.hibernate.DuplicateMappingException: Table [care_plan] contains logical column name [carePlan_code] referring to multiple physical column names: [careplan_code], [care_plan_code]
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.bindLogicalToPhysical(InFlightMetadataCollectorImpl.java:902)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl$TableColumnNameBinding.addBinding(InFlightMetadataCollectorImpl.java:890)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:961)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.addColumnNameBinding(InFlightMetadataCollectorImpl.java:942)
        at org.hibernate.cfg.Ejb3JoinColumn.addColumnBinding(Ejb3JoinColumn.java:788)
        at org.hibernate.cfg.Ejb3Column.linkWithValue(Ejb3Column.java:369)
        at org.hibernate.cfg.annotations.TableBinder.linkJoinColumnWithValueOverridingNameIfImplicit(TableBinder.java:730)
        at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:660)
        at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:101)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1786)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1730)
        at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1617)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)
        at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1648)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1585)

这是我用来生成表格的SQL脚本。

CREATE TABLE CarePlan (
  id bigserial NOT null PRIMARY KEY,
  time_created  timestamptz DEFAULT now() NOT NULL,
  time_updated  timestamptz DEFAULT now() NOT NULL,
  careplan_system VARCHAR(50),
    careplan_code VARCHAR(50) UNIQUE,
    description VARCHAR(500),
    parent_careplan_code VARCHAR(50),
    FOREIGN KEY (parent_careplan_code) REFERENCES CarePlan (careplan_code)
);

我错过了什么?

2 个答案:

答案 0 :(得分:0)

将您的实体重命名为此类。

@Entity
@Table(schema = "siva", name = "CarePlan")
public class CarePlan extends Updateable {

    @Column(name = "careplan_system")
    private String system;

    @Column(name = "code")
    private String code;

    @Column(name = "description")
    private String description;

    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "parent_careplan_code",referencedColumnName = "code")
    private CarePlan manager;

    @OneToMany(mappedBy = "manager",fetch = FetchType.EAGER)
    private Set<CarePlan> subsections = new HashSet<>();


    public String getSystem() {
        return system;
    }

    public void setSystem(String system) {
        this.system = system;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<CarePlan> getSubsections() {
        return subsections;
    }

    public void setSubsections(Set<CarePlan> subsections) {
        this.subsections = subsections;
    }

    public void setManager(CarePlan manager) {
        this.manager = manager;
    }

    public CarePlan getManager() {
        return manager;
    }
}

注意这个

 @JoinColumn(name = "parent_careplan_code",referencedColumnName = "code")

parent_careplan_code是你的外国人的名字

答案 1 :(得分:0)

我发现此问题与spring-boot/2129HIBERNATE-160有关,该问题的解决方法是添加以下内容:

spring.jpa.hibernate.naming:
  implicit-strategy: "org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl"
  physical-strategy: "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl"

或使用等效的属性文件。