实体映射中的重复列应使用insert =“false”update =“false”进行映射

时间:2018-04-05 03:32:30

标签: java hibernate annotations

我有一个pojo类地址,我正在尝试构建一个数据库表,其中应该有不同的家庭地址和办公地址。我正在使用AttributeOverride注释来使地址类单独显示办公地址和家庭地址的属性,但我得到以下问题。 线程“main”中的异常org.hibernate.MappingException:实体的映射中的重复列:org.blogPost.Employee列:CITY_NAME(应使用insert =“false”update =“false”映射)

请参阅以下代码并建议正确解决此问题。

主类。

    package org.blogPost;
    import java.util.Date;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;

    public class TestEmployee {

        public static void main(String[] args) {
            SessionFactory sessionfactory = new 
            AnnotationConfiguration().configure().buildSessionFactory();
            Session session = sessionfactory.openSession();
            session.beginTransaction();
            Date date= new Date();
            Employee emp1= new Employee();
            Address addr= new Address();
            Address home_addr= new Address();
            addr.setStreet("This is a test street");
            addr.setCity("test city");
            addr.setPincode("test Pin");
            home_addr.setStreet("Home Street Test");
            home_addr.setCity("Home test home");
            home_addr.setPincode("Test_home pin");
            //emp1.setEmpId(101);
            emp1.setEmpName("Employee five");
            emp1.setEmpLastName("Employee five SirName");
            emp1.setNumber(7755);
            emp1.setEmail("emp55@hibernate.com");
            emp1.setAddress(home_addr);
            emp1.setOfficeAddress(addr);
            emp1.setEmpLoginTime(date);
            session.save(emp1);
            session.getTransaction().commit();
            emp1= null;

            session=sessionfactory.openSession();
            session.beginTransaction();
            emp1=(Employee)session.get(Employee.class, 1);
            System.out.println("Below is the user details");
            System.out.println("Employee ID : "+emp1.getEmpId());
            System.out.println("Employee Name : "+emp1.getEmpName());
            System.out.println("Employee Address : "+emp1.getAddress());
            System.out.println("Employee email ID : "+emp1.getEmail());

        }
    }

我遇到问题的员工类。

  
package org.blogPost;

import java.util.Date;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="EmployeeInfo")
public class Employee {

    private int empId;
    private String empName;
    private String empLastName;
    private long number;
    private String email;
    private Date empLoginTime;
    private Address address;

    @AttributeOverrides({
    @AttributeOverride 
    (name="street",column=@Column(name="HOME_STREET_ADDRESS")),
    @AttributeOverride 
    (name="city",column=@Column(name="HOME_CITY_ADDRESS")),
    @AttributeOverride 
        (name="pincode",column=@Column(name="HOME_PIN_CODE"))   
    })
    private Address officeAddress;

    @Id
    @GeneratedValue(strategy=GenerationType.TABLE,generator="empID")
      @TableGenerator(name="empID",table="empPrimaryKey",pkColumnName="empKey",pkColumnValue="empValue",allocationSize=1)
    @Column(name="EmployeeId",nullable=false)
    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    @Column(name="First_Name",nullable=false)
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    @Column(name="Last_Name",nullable=false)
    public String getEmpLastName() {
        return empLastName;
    }
    public void setEmpLastName(String empLastName) {
        this.empLastName = empLastName;
    }
    @Column(name="Phone_number")
    public long getNumber() {
        return number;
    }
    public void setNumber(long number) {
        this.number = number;
    }
    @Column(name="Email_Address",nullable=false)
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Temporal(TemporalType.TIMESTAMP)
    public Date getEmpLoginTime() {
        return empLoginTime;
    }
    public void setEmpLoginTime(Date empLoginTime) {
        this.empLoginTime = empLoginTime;
    }
    @Embedded
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Embedded
    public Address getOfficeAddress() {
        return officeAddress;
    }
    public void setOfficeAddress(Address officeAddress) {
        this.officeAddress = officeAddress;
    }


}
  

地址类。     包org.blogPost;

import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Address {

    private String street;
    private String city;
    private String pincode;

    @Column(name="STREET_NAME")
    public String getStreet() {
        return street;
    }
    public void setStreet(String street) {
        this.street = street;
    }
    @Column(name="CITY_NAME")
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @Column(name="PIN_CODE")
    public String getPincode() {
        return pincode;
    }
    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

}

以下是我运行代码时遇到的异常。

Exception in thread "main" org.hibernate.MappingException: Repeated column in mapping for entity: org.blogPost.Employee column: CITY_NAME (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:670)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:692)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:688)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:714)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:468)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:215)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1149)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1334)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at org.blogPost.TestEmployee.main(TestEmployee.java:12)

1 个答案:

答案 0 :(得分:1)

请在getter方法(公共地址getOfficeAddress())上保留@AttributeOverrides注释,就像在其他成员变量的getter方法上使用注释一样(public int getEmpId())。它会起作用。