java - 为什么我收到这个错误? "Repeated column in mapping for entity"

标签 java hibernate

我在执行时遇到以下异常, 我也发布了使用的实体。

Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: com.hybernate.chapter2.Customer column: customer_details (should be mapped with insert="false" update="false")
            at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:709)
            at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:731)
            at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:753)
            at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:506)
            at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
            at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
            at org.hibernate.cfg.Configuration.buil
            dSessionFactory(Configuration.java:1849)
                at com.hybernate.chapter2.CustomerTest.main(CustomerTest.java:20)

类别客户

package com.hybernate.chapter2;

import javax.persistence.Column; 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.SecondaryTable; 
import javax.persistence.Table;
import javax.persistence.TableGenerator; 

@Entity
@Table(name="Customer") @SecondaryTable(name="customer_Details")
public class Customer {     
private String customerName;        
private String customerId;      
private String customerAddress;         
private String MobileNo;    

public String getCustomerName() {       
return customerName;    
}   

public void setCustomerName(String customerName) {
        this.customerName = customerName;   }   

@Id
//@TableGenerator(name="customerID",initialValue=1,pkColumnName="customerId",pkColumnValue="customerValue",allocationSize=1,table="cust_PK")
@GeneratedValue     
public String getCustomerId() {         
return customerId;
}   

public void setCustomerId(String customerId) {      
this.customerId = customerId;   
}   

@Column(name="customer_details")    
public String getCustomerAddress() {        
return customerAddress;     
}   

public void setCustomerAddress(String customerAddress) {        
this.customerAddress = customerAddress;     
}   

@Column(name="customer_details")    
public String getMobileNo() {       
return MobileNo;    
}   
public void setMobileNo(String mobileNo) {      
MobileNo = mobileNo;    
}   

}

类 CustomerTest 包 com.hybernate.chapter2;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class CustomerTest { 

public static void main(String args[]) {
    Configuration config=new Configuration();
    config.addAnnotatedClass(Customer.class);
    //config.addAnnotatedClass(customer_Details.class);
    config.configure("hibernate.cfg.xml");  
    new SchemaExport(config).create(true,true);     
    ServiceRegistry serviceRegistry = new     
    StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();     
    SessionFactory factory = config.buildSessionFactory(serviceRegistry);   
    Session session=factory.getCurrentSession();    
    session.beginTransaction();
    Customer c2=new Customer();     
    c2.setCustomerName("sss");
    c2.setCustomerAddress("kkk");   
    c2.setMobileNo("87654");
    session.save(c2);   
    session.getTransaction().commit(); 

} 
}

最佳答案

您真的必须阅读异常消息! 应使用 insert="false"update="false"进行映射

问题在于您多次映射一个字段,如果只有一个字段可插入且只有一个字段可更新,则可能会出现这种情况。这个约束的原因是当 hibernate 保存一个实体时,它必须知道哪个值应该用于更新/插入查询。

关于java - 为什么我收到这个错误? "Repeated column in mapping for entity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24614488/

相关文章:

java - 如何在 Integer 列上的 Hibernate 中创建外键

java - 使用 hibernate 条件左连接

java - 外战如何注入(inject) Spring Bean ?

java - 如何在 Java 中比较字符串?

java - 列出美国mapreduce计划的所有航空公司

java - 如何检查 Java 程序的文件句柄泄漏?

触发了 hibernate 实体拦截器但设置值不保存

Hibernate session.get(..) 方法没有命中数据库

hibernate - createEntityManager 在 org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus 抛出 java.lang.NullPointerException

java - Spring Data JPA save() 方法不遵循 hashcode/equals 约定