2015-11-18 211 views
0

我試圖用SQL Server數據庫映射我的實體。 並作爲休眠映射:實體ID映射中的重複列

在實體映射重複柱得到異常:com.agency.Hotel柱:ID(應與插入物被映射=「假」更新=「假」)

以下是對於酒店我的映射文件

<?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,仍然面臨問題。

+0

''應該像''。 ''表示'Hotel.ID'也是'Location'表中主鍵列的外鍵列。由於'Hotel.ID'已經被定義爲'Hotel'表的主鍵列,這是沒有意義的,因此是錯誤的。如果情況確實如此,那麼表格之間的關係就不會有'',因爲它們共享主鍵。 – manish

回答

0

,當我每個表名的ID更改爲TABLEID

例如,問題得到解決:

表名稱:酒店

標識字段:HotelID

所有映射開始工作正常,因爲有一些問題或混亂與休眠,它沒有正確映射。