有一个实体
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
@Column(name = "customer_id")Hibernate 在, @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”列。完整显示问题中的实体类,吸气剂上是否有机会private Customer customerByCustomerId;没有带有指向同一列的链接的注释(例如@OneToOne)?如果是这样,则必须将两个字段之一标记为insert="false", update="false"