hibernate中的重复属性映射异常

时间:2014-07-23 14:11:11

标签: java hibernate hibernate-mapping

我正在使用这个简单的Hibernate Mapping xml来创建一个实体

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<hibernate-mapping>
   <class entity-name="FirstTestType">
      <property column="createdOn" name="java.sql.Timestamp" type="java.sql.Timestamp"/>
      <id column="id" name="id" type="java.lang.Long">
         <generator class="identity"/>
      </id>
      <property column="modifiedOn" name="java.sql.Timestamp" type="java.sql.Timestamp"/>
      <property column="testString" length="100" name="java.lang.String" type="java.lang.String"/>
      <property column="testInt" name="int" type="int"/>
      <property column="testBoolean" name="boolean" type="boolean"/>
      <property column="testLong" name="long" type="long"/>
   </class>
</hibernate-mapping> 

虽然我使用SchemaExport时有效地创建了我的架构

    final SchemaExport export = new SchemaExport(getHibernateConfigurationForSchema());

    export.create(true, true);

当我尝试创建一个sessionfactory时,我得到了这个异常

org.hibernate.MappingException: Duplicate property mapping of java.sql.Timestamp found in FirstTestType
    at org.appops.entityStore.hibernate.session.HibernateSessionFactoryProvider.setupSchemaSessionFactory(HibernateSessionFactoryProvider.java:78)
    at org.appops.entityStore.hibernate.session.HibernateSessionFactoryProviderTest.testSessionCreation(HibernateSessionFactoryProviderTest.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.MappingException: Duplicate property mapping of java.sql.Timestamp found in FirstTestType
    at org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:483)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:473)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:235)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
    at org.appops.entityStore.hibernate.session.HibernateSessionFactoryProvider.setupSchemaSessionFactory(HibernateSessionFactoryProvider.java:74)
    ... 11 more

你能帮我弄清楚Mapping xml的错误吗?

1 个答案:

答案 0 :(得分:2)

更改属性中的名称;

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<hibernate-mapping>
   <class entity-name="FirstTestType">
      <property column="createdOn" name="nameCreatedOn" type="java.sql.Timestamp"/>
      <id column="id" name="id" type="java.lang.Long">
         <generator class="identity"/>
      </id>
      <property column="modifiedOn" name="nameModifiedOn" type="java.sql.Timestamp"/>
      <property column="testString" length="100" name="nameTestString" type="java.lang.String"/>
      <property column="testInt" name="nameTestInt" type="int"/>
      <property column="testBoolean" name="nameTestBoolean" type="boolean"/>
      <property column="testLong" name="nameTestLong" type="long"/>
   </class>
</hibernate-mapping> 

你的课看起来像;

package basari.util;

public class FirstTestType {

    private Date nameCreatedOn;
    private int nameId;
    private String nameModifiedOn;
    private String nameTestString;
    private int nameTestInt;
    private Boolean nameTestBoolean;
    private Long nameTestLong;

    public FirstTestType(){

    }

    public Date getNameCreatedOn() {
        return nameCreatedOn;
    }
    public void setNameCreatedOn(Date nameCreatedOn) {
        this.nameCreatedOn = nameCreatedOn;
    }
    public int getNameId() {
        return nameId;
    }
    public void setNameId(int nameId) {
        this.nameId = nameId;
    }
    public String getNameModifiedOn() {
        return nameModifiedOn;
    }
    public void setNameModifiedOn(String nameModifiedOn) {
        this.nameModifiedOn = nameModifiedOn;
    }
    public String getNameTestString() {
        return nameTestString;
    }
    public void setNameTestString(String nameTestString) {
        this.nameTestString = nameTestString;
    }
    public int getNameTestInt() {
        return nameTestInt;
    }
    public void setNameTestInt(int nameTestInt) {
        this.nameTestInt = nameTestInt;
    }
    public Boolean getNameTestBoolean() {
        return nameTestBoolean;
    }
    public void setNameTestBoolean(Boolean nameTestBoolean) {
        this.nameTestBoolean = nameTestBoolean;
    }
    public Long getNameTestLong() {
        return nameTestLong;
    }
    public void setNameTestLong(Long nameTestLong) {
        this.nameTestLong = nameTestLong;
    }


}