有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试将我的实体映射到SQL Server数据库。 作为一个例外

Repeated column in mapping for entity: com.agency.Hotel column: ID (should be mapped with 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()
                + "]";
    }
}

我已经检查了这个{a1}和{a2}并且仍然面临问题


共 (1) 个答案

  1. # 1 楼答案

    当我将每个表名的ID更改为tableID时,问题就解决了

    例如:

    表名:酒店

    标识字段:HotelID

    由于hibernate没有正确映射,所以所有映射都开始正常工作