【问题标题】:How to fix "Duplicate property mapping" exception?如何修复“重复属性映射”异常?
【发布时间】:2014-09-07 09:16:52
【问题描述】:

xml 文件从我的数据库映射一个新表,但是当我启动项目时,我得到一个我无法理解和解决的重复属性映射错误。这是我的休眠 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 文件:

<?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>

和 Message 持久类:

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 文件中注释掉 &lt;mapping resource="entities/messages.hbm.xml"/&gt;,但仍然收到相同的错误。

Edit2: 以上来自一个 java EE 项目,我将所有内容复制粘贴到一个简单的 java 项目中,它工作正常。有什么建议吗?

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

【问题讨论】:

  • 对于 Message 类是否还有具有 SenderId 属性的超类?或您正在使用的任何 MappedSuperClass,因为只有当任何 Super 类具有相似属性时才会出现这种情况。
  • @kasharma 不,我的项目中根本没有任何超类,确保我在类中添加了 final 修饰符并且问题仍然存在。
  • 您在其他实体中是否有属性 SenderID?阅读:hibernate.atlassian.net/browse/HHH-5836

标签: java hibernate


【解决方案1】:

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

Hibernate ORMHHH-2598

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2014-11-02
    • 1970-01-01
    • 2014-06-20
    • 2017-01-15
    相关资源
    最近更新 更多