Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我会犯这个错误&引用;实体映射中的重复列“;_Java_Hibernate - Fatal编程技术网

Java 为什么我会犯这个错误&引用;实体映射中的重复列“;

Java 为什么我会犯这个错误&引用;实体映射中的重复列“;,java,hibernate,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 or

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

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

类别客户测试 包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保存实体时,它必须知道应该使用哪个值进行更新/插入查询。

您的客户类将是:

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
@GeneratedValue(strategy = GenerationType.AUTO)     
public String getCustomerId() {         
return customerId;
}   

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

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

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

@Column(table="customer_Details")    
public String getMobileNo() {       
return MobileNo;    
}   
public void setMobileNo(String mobileNo) {      
MobileNo = mobileNo;    
}   
Thia将创建两个表

  • 客户:customerId,customerName
  • 客户详细信息:customerId、MobileNo、customerAddress 您正在为第1列的两个字段指定“customer_details”名称。getCustomerAddress 2.mobileno。这在hibernate中是不允许的。