jpa org.hibernate.MappingException:找不到具有逻辑名称的列

e4yzc0pl  于 2022-11-24  发布在  其他
关注(0)|答案(5)|浏览(132)

我的表格如下:
1-医疗_公司

  • medical_company_id外键帐户_实体帐户_实体_id列上(不是PK)
  • 栏1
  • 栏2
  • 第3栏

2-账户_实体

*帐户实体标识(pk)

  • 栏1
  • 栏2
  • 第3栏

3-人:

*人员标识(pk)

  • 栏1
  • 栏2
  • 第3栏

4-雇员_公司:

*medical_company表上的公司标识外键,该表位于medical_company_id
*person表上的雇员标识外键person_id

  • 栏1
  • 栏2
    实体:

1-医疗公司:

@SuppressWarnings("serial")
@Entity
@Table(name = "medical_company")
public class MedicalCompany implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
@Basic(fetch = FetchType.EAGER)
private Long id;

@OneToOne
@Cascade(value = { CascadeType.ALL })
@JoinColumn(name = "medical_company_id", referencedColumnName = "account_entity_id")
private AccountEntity accountEntity;

}
2-帐户实体:

@SuppressWarnings("serial")
@Entity
@Table(name = "account_entity")
public class AccountEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "account_entity_id", unique = true, nullable = false)
    @Basic(fetch = FetchType.EAGER)
    private Long id;

}

3-联系人:

@SuppressWarnings("serial")
@Entity
@Table(name = "person")
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "person_id", unique = true, nullable = false)
    @Basic(fetch = FetchType.EAGER)
    private Long id;

}

4-员工公司ID

@SuppressWarnings("serial")
@Embeddable
public class EmployeeCompanyId implements Serializable {

    @ManyToOne
    private Person person;

    @ManyToOne
    private MedicalCompany medicalCompany;

    @Size(max = 150, message = "{long.value}")
    @Column(name = "title_text", length = 150, nullable = true)
    private String titleText;

    @Column(name = "employee_manager")
    private long employeeManager;

}

5-员工公司:

@SuppressWarnings("serial")
@Entity
@Table(name = "employee_company")
@AssociationOverrides(value = {
        @AssociationOverride(name = "pk.medicalCompany", joinColumns = @JoinColumn(referencedColumnName = "medical_company_id")),
        @AssociationOverride(name = "pk.person", joinColumns = @JoinColumn(referencedColumnName = "person_id")),
        @AssociationOverride(name = "pk.titleText"),
        @AssociationOverride(name = "pk.employeeManager") })
public class EmployeeCompany implements Serializable {

    @EmbeddedId
    private EmployeeCompanyId pk = new EmployeeCompanyId();

    @Transient
    public void setEmployeeManager(long employeeManager) {
        this.pk.setEmployeeManager(employeeManager);
    }

    public long getEmployeeManager() {
        return pk.getEmployeeManager();
    }

    @Transient
    public void setTitleText(String titleText) {
        this.pk.setTitleText(titleText);
    }

    public String getTitleText() {
        return pk.getTitleText();
    }

    public void setPerson(Person person) {
        this.pk.setPerson(person);
    }

    @Transient
    public Person getPerson() {
        return this.pk.getPerson();
    }

    public void setMedicalCompany(MedicalCompany medicalCompany) {
        this.pk.setMedicalCompany(medicalCompany);
    }

    @Transient
    public MedicalCompany getMedicalCompany() {
        return this.pk.getMedicalCompany();
    }

    public void setPk(EmployeeCompanyId pk) {
        this.pk = pk;
    }

    public EmployeeCompanyId getPk() {
        return pk;
    }

}

尝试运行应用程序时,我收到以下错误:

org.hibernate.MappingException: Unable to find column with logical name: medical_company_id in org.hibernate.mapping.Table(medical_company) and its related supertables and secondary tables
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:550)
    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:126)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:110)
    at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:520)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

请告诉我为什么我得到这个错误,以及如何解决它。

cdmah0mi

cdmah0mi1#

我将删除MedicalCompany中的referencedColumnName属性,因为您正在命名AccountEntity的主键字段。我认为只有在它引用非主键字段时才有必要。

@JoinColumn(name = "medical_company_id", referencedColumnName = "account_entity_id")
roejwanj

roejwanj2#

此错误告诉您,medical_company表中没有名为medical_company_id的列。medical_company中的列仅称为id。

6jjcrrmo

6jjcrrmo3#

我在不同的情况下收到了相同的错误。我尝试用字符串键连接2个表。此代码块发送了相同的错误。

@OneToMany
@JoinColumn(name = "SHIPMENT_ID", referencedColumnName = "PRODUCT_SHIPMENT_GROUP")
private List<ProductShipment> productShipments = new ArrayList<>();

我通过添加列和Fetch类型解决了问题(我在实体中有2个连接,因此需要添加FetchMode)=〉

@OneToMany
@JoinColumn(name = "SHIPMENT_ID", referencedColumnName = "PRODUCT_SHIPMENT_GROUP")
@Fetch(value = FetchMode.SUBSELECT)
private List<ProductShipment> productShipments = new ArrayList<>();

@Column(name = "PRODUCT_SHIPMENT_GROUP")
private String productShipmentGroup;
pnwntuvh

pnwntuvh4#

对于那些遇到这个问题并正在使用globally_quoted_identifiers的人来说,这对我很有帮助:
变更

@JoinColumns({
    @JoinColumn(name = "new_col_1", referencedColumnName = "ref_col_1"),
    @JoinColumn(name = "new_col_2", referencedColumnName = "ref_col_2")
})

结束日期

@JoinColumns({
    @JoinColumn(name = "new_col_1", referencedColumnName = "`ref_col_1`"),
    @JoinColumn(name = "new_col_2", referencedColumnName = "`ref_col_2`")
})

请注意referencedColumnName值周围的反勾号。

1bqhqjot

1bqhqjot5#

确保父实体的主键名称:@Column(name = "id")与子实体中referencedColumnName(外键)的名称相同:@JoinColumn(name = "created_by", referencedColumnName = "id")
即:id = id

相关问题