hibernate XML映射中实体映射的重复列

时间:2018-06-07 18:24:19

标签: java hibernate hibernate-mapping hibernate-xml-mapping

我一直面临着“实体映射中的重复列”的问题。你能不能帮助我,我犯了错误。我在下面提到了我的代码。

USERAUDIT.hbm.xml

<hibernate-mapping>
    <class name="com.mkyong.user.UserAudit" table="USER_AUDIT_TBL">
        <id name="eventId" column="EVENT_ID" type="java.lang.Integer">
            <generator class="sequence">
                <param name="sequence">AUDIT_SEQUENCE</param>
            </generator>
        </id>
        <property name="userId" type="java.lang.Integer">
            <column name="USER_ID" length="10" not-null="true" unique="true" />
        </property>

        <set name="userAuditDtls" table="USER_AUTI_DTLS_TBL" inverse="true" 
            lazy="true" fetch="select">
            <key>
                <column name="EVENT_ID" not-null="true"  />
            </key>
            <one-to-many class="com.mkyong.user.UserAuditDtls" />
        </set>

    </class>
</hibernate-mapping>

USERAUDIT.java

enter code here公共类UserAudit实现了java.io.Serializable {

private Integer eventId;
private Integer userId;
//private UserAuditDtls userAuditDtls;
private Set<UserAuditDtls> userAuditDtls = 
        new HashSet<UserAuditDtls>(0);


public UserAudit(Integer eventId, Integer userId, Set<UserAuditDtls> userAuditDtls) {
    super();
    this.eventId = eventId;
    this.userId = userId;
    this.userAuditDtls = userAuditDtls;
}
public UserAudit() {
    super();
    // TODO Auto-generated constructor stub
}
public Integer getEventId() {
    return eventId;
}
public void setEventId(Integer eventId) {
    this.eventId = eventId;
}
public Integer getUserId() {
    return userId;
}
public void setUserId(Integer userId) {
    this.userId = userId;
}
public Set<UserAuditDtls> getUserAuditDtls() {
    return userAuditDtls;
}
public void setUserAuditDtls(Set<UserAuditDtls> userAuditDtls) {
    this.userAuditDtls = userAuditDtls;
}

}

USERAUDITDTLS.hbm.xml

<hibernate-mapping>
    <class name="com.mkyong.user.UserAuditDtls" table="PII_USER_AUTI_DTLS_TBL">
        <id name="eventId" type="java.lang.Integer">
            <column name="EVENT_ID" />
            <generator class="foreign">
                <param name="property">userAudit</param>
            </generator>
        </id>

        <many-to-one name="userAudit" class="com.mkyong.user.UserAudit" fetch="select">
            <column name="EVENT_ID" not-null="true" />
        </many-to-one>
        <property name="fieldName" type="string">
            <column name="FIELD_NAME" length="100" not-null="true" />
        </property>

    </class>
</hibernate-mapping>

UserAuditDtls.java

public class UserAuditDtls implements java.io.Serializable {

    private Integer eventId;
    private UserAudit userAudit;
    private String fieldName;


    public UserAuditDtls() {
        super();
        // TODO Auto-generated constructor stub
    }


    public UserAuditDtls(Integer eventId, UserAudit userAudit, String fieldName) {
        super();
        this.eventId = eventId;
        this.userAudit = userAudit;
        this.fieldName = fieldName;
    }


    public Integer getEventId() {
        return eventId;
    }
    public void setEventId(Integer eventId) {
        this.eventId = eventId;
    }
    public UserAudit getUserAudit() {
        return userAudit;
    }
    public void setUserAudit(UserAudit userAudit) {
        this.userAudit = userAudit;
    }
    public String getFieldName() {
        return fieldName;
    }
    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }


}

Main.Java

Session session = HibernateUtil.getSessionFactory().openSession();

        session.beginTransaction();
        UserAudit audit = new UserAudit();
        audit.setUserId(new Integer(100));

        session.save(audit);

        UserAuditDtls auditDtls = new UserAuditDtls();
        auditDtls.setFieldName("Small");
        auditDtls.setUserAudit(audit);
        audit.getUserAuditDtls().add(auditDtls);
        session.save(auditDtls);

        session.getTransaction().commit();

错误:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.mkyong.user.UserAuditDtls column: EVENT_ID (should be mapped with insert="false" update="false")
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
    at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
    at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1335)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1838)
    at com.mkyong.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)

表格

USER_AUDIT_TBL

event_id - pk userid -integer

USER_AUDIT_DTLS_TBL

event_id- fk fieldname - varchar

1 个答案:

答案 0 :(得分:0)

问题是USERAUDITDTLS.hbm.xml中的<column name="EVENT_ID" not-null="true" />