Hibernate Mapping:实体ID映射中的重复列

时间:2015-11-18 14:48:59

标签: java hibernate hibernate-mapping

我试图用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,但仍面临问题。

1 个答案:

答案 0 :(得分:0)

当我将每个表名的ID更改为tableID

时,问题得以解决

例如:

表名:酒店

身份字段:HotelID

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