There is an entity

public class RealEstateType { private long customer_id; private String name; private Customer customerByCustomerId; @Id @Column(name = "customer_id", insertable = false, updatable = false) public long getCustomerId() { return customer_id; } public void setCustomerId(long id) { this.customer_id = id; } 

which describes the table

 CREATE TABLE db.real_estate_type ( customer_id BIGINT NOT NULL, name CHARACTER VARYING(255), FOREIGN KEY (customer_id) REFERENCES db.customer (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ); 

Which customer_id is FOREIGN KEY

Hibernate throws an exception when @Column(name = "customer_id") , @Column(name = "customer_id", insertable = false, updatable = false) , @Column(name = "customer_id", insertable = true, updatable = false)

 amework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [spring.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for entity: hibernate.model.df.RealEstateType column: customer_id (should be mapped with insert="false" update="false") at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 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.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:756) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 

But if you delete customer_id and PK to make @Id @Column(name = "name") , then everything works.

If we leave customer_id and PK to make @Id @Column(name = "name") , then we get the same error.

Explain to me the reason for this behavior and how to solve it?

  • You have a strange customer_id - FK, but you declare it as PK. - Vartlok
  • WITHOUT PK does not allow Hibernate to describe the entity - Senior Pomidor
  • So you need to create it in a separate field. Hiberneit can not without a PC, but it must be a PC. - Vartlok
  • not. You can @Embeddable yuzat. but that's another story altogether - Senior Pomidor

1 answer 1

In this case, the @Id annotation is @Id ; in the exception text, it appears that the second field essentially refers to the same "customer_id" column. Show the entity class from the question completely, there randomly on getter for private Customer customerByCustomerId; No annotations referring to the same column (for example @OneToOne )? If so, then one of the two fields should be marked as insert="false", update="false"

  • Yes. you're right. then it is not clear why Hiberneyt so generated an entity for me ... - Senior Pomidor