我是 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_,securecus0_.phoneNumber 作为phoneNum8_11_0_来自 SecuredCustomer securecus0_,其中 securecus0_.loginId=?
此 SQL 语句正在运行并返回正确的值。
当我逐步调试时,它正在从数据库返回数据。但是在 eclipse/tomcat 中运行相同的代码时,即使该对象在数据库表中可用,它也会返回 null 对象。我正在运行 Ubuntu 的本地主机上测试 web 应用程序... 请帮帮我。 预先感谢您
最佳答案
解决了...创建了一个类
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。它解决了这个问题...
关于java - Session.get 在 hibernate 中工作,同时逐步调试,但在 eclipse 中运行时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46099773/