2014-05-10 42 views
0

也許標題不是很明顯,但我會盡我所能地描述場景。 我有一個叫做Hotel的類,我想返回一個從數據庫中取出的對象,但是有一個字段不在數據庫中,我不知道如何處理這個原因,我得到一個錯誤。 我告訴你,這是酒店類:休眠。返回不完整的Object。映射問題

@Entity(name="Hotels") 
public class Hotel { 
    @Id @GeneratedValue 
    private int id; 
    @Column(name="name") 
    private String name; 
    @Column(name="category") 
    private int category; 
    @Column(name="base_price") 
    private int base_price; 
    @Column(name="region_id") 
    private int region_id; 
    @Column(name="adress") 
    private String address; 
    @Column(name="phone") 
    private int phone; 
    @Column(name="description") 
    private String description; 
    private Rate rate; 

//SETTERS and GETTERS 

public int getId() { 
    return id; 
} 
public void setId(int id){ 
    this.id=id; 
} 
public String getName() { 
    return name; 
} 

public void setName(String nombre) { 
    this.name = nombre; 
} 
public int getBase_price() { 
    return base_price; 
} 
public void setBase_price(int base_price) { 
    this.base_price = base_price; 
} 
public int getRegion_id() { 
    return region_id; 
} 
public void setRegion_id(int region_id) { 
    this.region_id = region_id; 
} 
public int getCategory() { 
    return category; 
} 
public void setCategory(int category) { 
    this.category = category; 
} 
public String getDescription() { 
    return description; 
} 
public void setDescription(String description) { 
    this.description = description; 
} 
public int getPhone() { 
    return phone; 
} 
public void setPhone(int phone) { 
    this.phone = phone; 
} 
public String getAddress() { 
    return address; 
} 
public void setAddress(String address) { 
    this.address = address; 
} 
public Rate getRate() { 
    return rate; 
} 
public void setRate(Rate rate) { 
    this.rate = rate; 
} 

}

這是我的數據庫結構:

mysql> describe Hotels; 
+-------------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+-------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| name  | varchar(255) | NO |  | NULL |    | 
| category | int(11)  | NO |  | NULL |    | 
| base_price | int(11)  | NO |  | NULL |    | 
| region_id | int(11)  | NO |  | NULL |    | 
| adress  | varchar(255) | NO |  | NULL |    | 
| phone  | int(11)  | NO |  | NULL |    | 
| photo_id | int(11)  | NO |  | NULL |    | 
| description | varchar(255) | NO |  | NULL |    | 
+-------------+--------------+------+-----+---------+----------------+ 

而且我要回這些領域從拍攝的飯店對象數據庫:名稱,電話,地址,基地價格,類別,描述和region_id。 我不在乎Rate,我想用Rate = null返回它。 這是我的功能,在那裏我有問題:

public Hotel buscaHotel(int id) { 

     Hotel nuevoHotel = new Hotel(); 

     SessionFactory sf = open(); 
     Session ss = sf.openSession(); 
     ss.beginTransaction(); 
     Query query = ss.createQuery("select u from Hotels u where id = :id "); 
     query.setParameter("id", id); 
     List<Hotel> list = query.list(); 
     if (list.size() != 0) { 

      nuevoHotel = list.get(0); 
      ss.getTransaction().commit(); 
      ss.close(); 
      return nuevoHotel; 
     } else { 
      ss.getTransaction().commit(); 
      ss.close(); 
      return nuevoHotel; 

     } 

    } 

我得到的錯誤,這是一個:

org.hibernate.MappingException: Could not determine type for: manager.Rate, at table: Hotels, for columns: [org.hibernate.mapping.Column(rate)] 

以防萬一,我的hibernate.cfg.xml我:

<mapping class="manager.Hotel"></mapping> 

回答

0

字段Rate rate未映射爲Hibernate,所以在序列化/反序列化數據庫中的數據時,它不知道如何處理它。

在您的班級和您的hibernate.cfg.xml文件中爲Rate類添加相關聯(@ManyToOne也許)。

如果您沒有Rate類的當前映射,mark會與@Transient一致,所以Hibernate會忽略它。

@Transient 
private Rate rate;