如何修复“重复属性映射"例外? [英] How to fix "Duplicate property mapping" exception?

查看:86
本文介绍了如何修复“重复属性映射"例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xml文件从数据库映射新表,但是启动项目时出现重复属性映射错误,我无法理解和解决.这是我的休眠cfg.xml

xml file to map a new table from my db but when I start the project I get a Duplicate property mapping error that I cannot understand and resolve. Here is my hibernate cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="session1">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password"/>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/realestate</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  <property name="hibernate.connection.CharSet">utf8</property>
  <property name="hibernate.connection.characterEncoding">utf8</property>
  <property name="hibernate.connection.useUnicode">true</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.current_session_context_class">thread</property>
  <property name="hibernate.c3p0.min_size">5</property>
  <property name="hibernate.c3p0.max_size">20</property>
  <property name="hibernate.c3p0.timeout">300</property>
  <property name="hibernate.c3p0.max_statements">50</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>
  <property name="hibernate.connection.CharSet">utf8</property>
  <property name="hibernate.connection.characterEncoding">utf8</property>
  <property name="hibernate.connection.useUnicode">true</property>
  <mapping resource="entities/users.hbm.xml"/>
  <mapping resource="entities/adminstration.hbm.xml"/>
  <mapping resource="entities/seller.hbm.xml"/>
  <mapping resource="entities/buyer.hbm.xml"/>
  <mapping resource="entities/renter.hbm.xml"/>
  <mapping resource="entities/leeser.hbm.xml"/>
  <mapping resource="entities/house.hbm.xml"/>
  <mapping resource="entities/userSellsHouse.hbm.xml"/>
  <mapping resource="entities/userRentsHouse.hbm.xml"/> 
    <mapping resource="entities/messages.hbm.xml"/> 


 </session-factory>
</hibernate-configuration>

messages.hbm.xml文件:

The messages.hbm.xml file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="entities.Message" table="MESSAGES" schema="realestate">
        <id name="messageID" type="int">
            <column name="messsageID" />

        </id>
        <property name="Date" type="java.lang.String">
            <column name="Date" />
        </property>
        <property name="SenderID" type="java.lang.Integer">
            <column name="Sender"  />
        </property>
        <property name="ReceiverID" type="java.lang.Integer">
            <column name="Receiver"  />
        </property>
        <property name="Message" type="java.lang.String">
            <column name="Message"  />
        </property>
        <property name="Theme" type="java.lang.String">
            <column name="Theme"  />
        </property>

    </class>
</hibernate-mapping>

和消息持久化类:

public class Message {
    int MessageID;
    int SenderID;
    int ReceiverID;
    String date;
    String message;
    String theme;


    public int getMessageID() {
        return MessageID;
    }
    public void setMessageID(int messageID) {
        MessageID = messageID;
    }
    public int getSenderID() {
        return SenderID;
    }
    public void setSenderID(int senderID) {
        SenderID = senderID;
    }
    public int getReceiverID() {
        return ReceiverID;
    }
    public void setReceiverID(int receiverID) {
        ReceiverID = receiverID;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public String getTheme() {
        return theme;
    }
    public void setTheme(String theme) {
        this.theme = theme;
    }

}

这是错误

...
    Caused by: org.hibernate.MappingException: Duplicate property mapping of SenderID found in entities.Messages
        at org.hibernate.mapping.PersistentClass.checkPropertyDuplication(PersistentClass.java:515)
        at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:505)
        at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
        at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
        at database.HibernateUtil.<clinit>(HibernateUtil.java:15)
        ... 46 more

编辑:我尝试将cfg文件中的<mapping resource="entities/messages.hbm.xml"/>注释掉,但仍然出现相同的错误.

I tried commenting out the <mapping resource="entities/messages.hbm.xml"/> from the cfg file and I still get the same error.

Edit2 :上面是一个Java EE项目,我将所有内容复制并粘贴到了一个简单的Java项目中,并且工作正常.有什么建议吗?

The above is from a java EE project, I copied paste everything in a simle java project and it worked fine. Any suggestion?

Edit3 :我将final修饰符添加到message类,以确保它不能被继承

I added the final modifier to messages class to be sure that it cannot be inherited

推荐答案

请通过以下链接,这可能对您有帮助...

Please go through the following link, this might help you...

休眠ORMHHH-2598

如果集合键不为null,则用相同的集合名称映射来自两个不同类的实体的集合会导致重复的backref属性异常

Mapping a collection of entities from two different classes with the same collection name results in duplicate backref property exception if collection keys are not null

这篇关于如何修复“重复属性映射"例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆