maybe the title is not very self-explanatory but I going to describe the scenario as best as I can. I have a class called Hotel, and I want to return an object I take from the database, but there is one field that is not in the database and I don't know how to handle this cause I got an error. I show you, this is the Hotel class:
@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;
}
}
This is my database structure:
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 | |
+-------------+--------------+------+-----+---------+----------------+
And I want to return an Hotel object with these fields taken from the database: name,phone,address,base_price,category,description and region_id. I don't care about Rate, I want to return it with Rate=null. This is my function, where I have the problem:
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;
}
}
The error I got is this one:
org.hibernate.MappingException: Could not determine type for: manager.Rate, at table: Hotels, for columns: [org.hibernate.mapping.Column(rate)]
Just in case, on my hibernate.cfg.xml I have:
<mapping class="manager.Hotel"></mapping>
Field Rate rate
is unmapped for Hibernate
, so it doesn't know how to handle it when serializing/deserializing the data from the database.
Add the relevant association ( @ManyToOne
maybe) in your class and in your hibernate.cfg.xml file for Rate
class.
In case you don't have the current mapping for Rate
class ready, mark is with @Transient
so Hibernate will ignore it.
@Transient
private Rate rate;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.