Имеется сущность
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;
}
которая описывает таблицу
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
);
У которой customer_id является FOREIGN KEY
Hibernate кидает исключение при @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)
Но если удалить customer_id и PK сделать @Id @Column(name = "name"), то все работает.
Если оставить customer_id и PK сделать @Id @Column(name = "name"), то получаем такую же ошибку.
Объясните мне причину такого поведения и как его решить?
В данном случае аннотация @Id не причем, по тексту исключения похоже, что второе поле в сущности ссылается на ту же самую колонку "customer_id".
Покажите класс сущности из вопроса полностью, там случайно на getter-е для private Customer customerByCustomerId; нет аннотаций со ссылкой на ту же колонку (например @OneToOne)? Если есть, то одно из двух полей должно быть помечено как insert="false", update="false"
Сборка персонального компьютера от Artline: умный выбор для современных пользователей