我是hibernate的新手。我在MySQL中使用Hibernate来保存我的数据。这是我的实体类。
@Entity
@XmlRootElement
public class SecuredCustomer implements Serializable{
@Id
private String loginId;
private String password;
@Embedded
UserInfo userInformation = new UserInfo();
int allocatedRestaurantTableId;
@Embedded
Order order;
@Embedded
Bill bill;
public SecuredCustomer(){
allocatedRestaurantTableId = -1;
order = null;
bill = null;
}
public Bill getBill() {
return bill;
}
public void setBill(Bill bill) {
this.bill = bill;
}
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
public int getAllocatedRestaurantTableId() {
return allocatedRestaurantTableId;
}
public void setAllocatedRestaurantTableId(int allocatedRestaurantTableId) {
this.allocatedRestaurantTableId = allocatedRestaurantTableId;
}
public UserInfo getUserInformation() {
return userInformation;
}
public void setUserInformation(UserInfo userInformation) {
this.getUserInformation().setAddress(userInformation.getAddress());
this.getUserInformation().setAge(userInformation.getAge());
this.getUserInformation().setName(userInformation.getName());
this.getUserInformation().setPhoneNumber(userInformation.getPhoneNumber());
}
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public boolean equals(Object obj)
{
return this == obj;
}
}
当用户使用登录ID和密码注册时,我坚持使用此类。
现在我正在尝试检索如下数据:
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
//Retrieving Data
Session session = sessionFactory.openSession();
session.beginTransaction();
SecuredCustomer customerInDB = session.get(SecuredCustomer.class, userLoginId);
Hibernate创建的SQL如下: Hibernate:选择securedcus0_.loginId为loginId1_11_0_,securedcus0_.allocatedRestaurantTableId为allocate2_11_0_,securedcus0_.total为total3_11_0_,securedcus0_.password为password4_11_0_,securedcus0_.address为address5_11_0_,securedcus0_.age为age6_11_0_,securedcus0_.name为name7_11_0_,securedcus0_.phoneNumber为phoneNum8_11_0_来自SecuredCustomer securedcus0_,其中securedcus0_.loginId =?
此SQL语句正在运行并返回正确的值。
当我一步一步地调试时,它将从数据库返回数据。但是在eclipse / tomcat中运行相同的代码时,即使对象在DB的表中可用,它也会返回null对象。我在运行Ubuntu的localhost上测试webapp ... 请帮帮我。 提前谢谢
答案 0 :(得分:0)
解决了...创建了一个类
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
@Provider
public class MoxyThrowable implements javax.ws.rs.ext.ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
exception.printStackTrace();
return Response.status(500).build();
}
}
并查看详细错误。然后将一些相关的fetchtype更改为EAGER。它解决了问题......