Hibernate Mapping Exception:实体映射中的重复列

时间:2012-02-21 16:17:13

标签: hibernate java-ee orm persistence hibernate-mapping

特定实体存在映射异常。 无法从问题出现的地方找出答案。 我从头到尾检查了所有映射3次。 我仍然得到映射异常。

电子邮件发送给员工只映射一次。 但仍然是报告错误重复映射

错误是:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.cluster.entity.Email column: EMPLOYEE_ID (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:680)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:702)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:724)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:477)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:268)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1287)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1729)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at com.cluster.util.HibernateUtil.<clinit>(HibernateUtil.java:16)
    ... 1 more

电子邮件Pojo

package com.cluster.entity;

public class Email {

    private int intEmailID;
    private String strEmailName;

    //many to one
    private EmailType emailType;

    //many to one
    private Employee employee;

    public int getIntEmailID() {
        return intEmailID;
    }

    public void setIntEmailID(int intEmailID) {
        this.intEmailID = intEmailID;
    }

    public String getStrEmailName() {
        return strEmailName;
    }

    public void setStrEmailName(String strEmailName) {
        this.strEmailName = strEmailName;
    }

    public EmailType getEmailType() {
        return emailType;
    }

    public void setEmailType(EmailType emailType) {
        this.emailType = emailType;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

}

email.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        '-//Hibernate/Hibernate Mapping DTD 3.0//EN'
        'http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd'>


<hibernate-mapping package="com.cluster.entity" >
    <class name="Email" table="EMAIL">

        <id name="intEmailID" column="EMAIL_ID"> 
            <generator class="sequence">
                <param name="sequence">EMAIL_ID_SEQ</param>
            </generator>
        </id>

        <property name="strEmailName" column = "EMAIL_NAME"/>

        <many-to-one name="employee" column="EMPLOYEE_ID" not-null = "true" class = "Employee"/>

        <many-to-one name="emailType" column="EMAIL_TYPE_ID" not-null = "true" class = "EmailType"/>

    </class>

</hibernate-mapping>

相关脚本

CREATE TABLE EMPLOYEE
(
  EMPLOYEE_ID      NUMBER                       NOT NULL,
  FIRSTNAME        VARCHAR2(20 BYTE)            NOT NULL,
  LASTNAME         VARCHAR2(20 BYTE)            NOT NULL,
  DATE_OF_BIRTH    VARCHAR2(20 BYTE)            NOT NULL,
  SALARY           VARCHAR2(10 BYTE)            NOT NULL,
  DEPARTMENT_ID    NUMBER                       NOT NULL
);


CREATE TABLE EMAIL
(
 EMAIL_ID              NUMBER              NOT NULL,
 EMAIL_NAME            VARCHAR2(40 BYTE)   NOT NULL,
 EMPLOYEE_ID           NUMBER              NOT NULL,
 EMAIL_TYPE_ID         NUMBER              NOT NULL
);


CREATE TABLE EMAIL_TYPE
(
 EMAIL_TYPE_ID         NUMBER              NOT NULL,
 EMAIL_TYPE_NAME       VARCHAR2(40 BYTE)   NOT NULL
);


ALTER TABLE EMPLOYEE ADD 
(
 CONSTRAINT PK_EMPLOYEE_ID
 PRIMARY KEY (EMPLOYEE_ID)
);

ALTER TABLE EMAIL_TYPE ADD 
(
 CONSTRAINT PK_EMAIL_TYPE_ID
 PRIMARY KEY (EMAIL_TYPE_ID)
);

ALTER TABLE EMAIL ADD
(
 CONSTRAINT PK_EMAIL_ID
 PRIMARY KEY (EMAIL_ID)
);

ALTER TABLE EMAIL ADD
( 
 CONSTRAINT FK_EMAIL_EMPLOYEE_ID
 FOREIGN KEY (EMPLOYEE_ID)
 REFERENCES EMPLOYEE (EMPLOYEE_ID)
);

ALTER TABLE EMAIL ADD
( 
 CONSTRAINT FK_EMAIL_EMAIL_TYPE_ID
 FOREIGN KEY (EMAIL_TYPE_ID)
 REFERENCES EMAIL_TYPE (EMAIL_TYPE_ID)
);

电子邮件发送给员工只映射一次。 但仍然是报告错误重复映射

3 个答案:

答案 0 :(得分:10)

您是否已将Employee中的集合设置为反向?

<bag name="emails" inverse="true">
  <key column="EMPLOYEE_ID" not-null="true">
  ...
</bag>

答案 1 :(得分:7)

对于那些使用注释来解决这个问题的人来说,这些类应该是这样的:

@Entity
public class Email {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    private int intEmailID;

    @Column(insertable = false, updatable = false)
    private int employeeId;

    @ManyToOne
    private EmailType emailType;

    @ManyToOne
    @JoinColumn(name="employeeId")
    private Employee employee;

    // Getters and Setter
}


@Entity
public class Employee {

    @Id
    private int id;

    @OneToMany(cascade = {CascadeType.ALL}, mappedBy="employeeId")
    @PrimaryKeyJoinColumn
    private List<Email> emailList;

    public void setEmailList(List<Email> emailList) {
        this.emailList = emailList
    }

    public List<Email> getEmailList() {
        return emailList;
    }

    // Rest of getters and setters

}

答案 2 :(得分:2)

我认为您已使用oneToMany关系将Employee映射到Email。如果是,它没有错,但你必须确保只能在一个方向插入和更新

应与insert="false" update="false"

中的Employee对齐