【问题标题】:Hibernate Mapping : Repeated column in mapping for entity ID休眠映射:实体 ID 映射中的重复列
【发布时间】:2015-11-20 00:06:04
【问题描述】:

我正在尝试将我的实体与 SQL Server 数据库进行映射。 并得到异常

实体映射中的重复列:com.agency.Hotel 列:ID(应使用 insert="false" update="false" 映射)

以下是我的酒店映射文件

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

<hibernate-mapping>
   <class name="com.agency.Hotel" table="Hotels">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <id name="ID" type="int" column="ID">
         <generator class="native"/>
      </id>
      <property name="name" column="Name" type="string"/>
      <property name="star" column="Star" type="int"/>
      <property name="pricePerWeek" column="pricePerWeek" type="double"/>

    <many-to-one name="location" class="com.agency.Location" fetch="select">
            <column name="ID" not-null="true" />
        </many-to-one>

   </class>
</hibernate-mapping>

和我的酒店实体:

package com.agency;
public class Hotel {
    private int iD = 0;
    private int star = 0;
    private String name = null;
    private double pricePerWeek = 0.0;
    private Location location = null;
    private int locationID = 0;
    public int getID() {
        return iD;
    }
    public void setID(int newID) {
        iD = newID;
    }
    public int getStar() {
        return star;
    }
    public void setStar(int newStar) {
        star = newStar;
    }
    public String getName() {
        return name;
    }
    public void setName(String newName) {
        name = newName;
    }
    public double getPricePerWeek() {
        return pricePerWeek;
    }
    public void setPricePerWeek(double newPricePerWeek) {
        pricePerWeek = newPricePerWeek;
    }
    public Location getLocation() {
        return location;
    }
    public void setLocation(Location newLocation) {
        location = newLocation;
    }
    public int getLocationID() {
        return locationID;
    }
    public void setLocationID(int newLocationID) {
        locationID = newLocationID;
    }
    @Override
    public String toString() {
        return "Hotel " + " [iD: " + getID() + "]" + " [star: " + getStar()
                + "]" + " [name: " + getName() + "]" + " [pricePerWeek: "
                + getPricePerWeek() + "]" + " [locationID: " + getLocationID()
                + "]";
    }
}

我已经检查了这个linkthis 并且仍然面临问题。

【问题讨论】:

  • &lt;column name="ID" not-null="true" /&gt; 应该类似于 &lt;column name="locationID" not-null="true" /&gt;&lt;column name="ID" not-null="true" /&gt; 表示Hotel.ID 也是Location 表中主键列的外键列。由于Hotel.ID 已定义为Hotel 表的主键列,因此这没有意义,因此会出现错误。如果情况确实如此,那么表之间将不会有&lt;many-to-one/&gt; 而是&lt;one-to-one/&gt; 关系,因为它们共享主键。

标签: java hibernate hibernate-mapping


【解决方案1】:

当我将每个表名的 ID 更改为 tableID 时问题得到解决

例如:

表名:酒店

身份字段:HotelID

所有映射都开始正常工作,因为 hibernate 存在一些问题或混淆,它没有正确映射。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 2016-11-20
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    相关资源
    最近更新 更多