31

hi my tables are as follows:

1- medical_company:

  • medical_company_id foreign key on account_entity table account_entity_id column (not a pk)
  • column1
  • column2
  • column3

2- account_entity:

  • account_entity_id (pk)
  • column1
  • column2
  • column3

3- person:

  • person_id (pk)
  • column1
  • column2
  • column3

4- employee_company:

  • company_id foreign key on medical_company table on medical_company_id
  • employee_id foreign key on person table on person_id
  • column1
  • column2

ENTITIES:

1- MedicalCompany:

@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- AccountEntity:

@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- Person:

 @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- EmployeeCompanyId:

@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- EmployeeCompany:

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

}

when trying to run the application, i am getting the following error:

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)

please advise why i am getting this error, and how to solve it.

1
  • 1
    it was solved? I have same situation. Commented Sep 16, 2015 at 10:01

6 Answers 6

44

I would remove the referencedColumnName attribute in MedicalCompany because you are naming the primary key field of AccountEntity. I think it is only necessary if you if it references a non-primary-key field.

@JoinColumn(name = "medical_company_id", referencedColumnName = "account_entity_id")
Sign up to request clarification or add additional context in comments.

3 Comments

This is the problem that I was having (on Hibernate 6.0.13.Final)
Awesome ! Solved my issue as well for Hibernate 5.3.1. However, I only started seeing this issue as soon as I started using hibernate.globally_quoted_identifiers property in my persistence.xml. Not sure why.
Solved my issue as well!! This should be marked as the answer!
12

This error is telling you that there is no column on the medical_company table called medical_company_id. The column on medical_company is just called id.

1 Comment

but there's a foreign key with this name ! @JoinColumn(name = "medical_company_id"
1

I received same error for different situation. I try to join 2 table with String key. This code block send same error.

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

I solved question with adding column and Fetch type (I have 2 join in the entity so I need to add 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;

Comments

0

Make sure your parent entity's primary key name: @Column(name = "id") is the same name as the referencedColumnName (foreign key) in the child entity: @JoinColumn(name = "created_by", referencedColumnName = "id")

I.e: id = id

Comments

0

This might happen if you removed column from entity but column referenced by another entity

Comments

-1

For those who ran into this problem and are using globally_quoted_identifiers, this helped me:

Change

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

To

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

Notice the backtick wrapping the referencedColumnName value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.