【发布时间】:2016-02-26 21:05:38
【问题描述】:
我遇到了休眠问题。从昨天开始我一直在努力解决这个问题,这似乎很容易,但我不知道为什么它不起作用......
我有实体Login.java:
package offersmanager.model.entity;
import org.json.JSONObject;
import javax.persistence.*;
@Entity
public class Login {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false, unique = true)
String username;
@Column(nullable = false)
String password;
public Login(){
}
public Login(String username, String password){
this.username = username;
this.password = password;
}
public Login(JSONObject jsonObject) {
this.id = (Integer) jsonObject.get("id");
this.username = (String) jsonObject.get("username");
this.password = (String) jsonObject.get("password");
}
public JSONObject toJsonObject() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", this.id);
jsonObject.put("username", this.username);
jsonObject.put("password", this.password);
return jsonObject;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
和实体TourOffice.java:
package offersmanager.model.entity;
import org.json.JSONObject;
import javax.persistence.*;
@Entity
public class TourOffice {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false)
String officeName;
@Column(nullable = false)
String eMail;
@Column(nullable = false)
String phoneNumber;
@Column(nullable = false)
String city;
@Column(nullable = false)
String zipCode;
@Column(nullable = false)
String address;
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "login_id")
Login login;
public TourOffice(){
}
public TourOffice(String officeName, String eMail, String phoneNumber, String city, String zipCode, String address) {
this.officeName = officeName;
this.eMail = eMail;
this.phoneNumber = phoneNumber;
this.city = city;
this.zipCode = zipCode;
this.address = address;
}
public TourOffice(JSONObject jsonObject) {
this.id = (Integer) jsonObject.get("id");
this.officeName = (String) jsonObject.get("officeName");
this.eMail = (String) jsonObject.get("eMail");
this.phoneNumber = (String) jsonObject.get("phoneNumber");
this.city = (String) jsonObject.get("city");
this.zipCode = (String) jsonObject.get("zipCode");
this.address = (String) jsonObject.get("address");
this.login = (new Login((JSONObject) jsonObject.get("login")));
}
public JSONObject toJsonObject() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", this.id);
jsonObject.put("officeName", this.officeName);
jsonObject.put("eMail", this.eMail);
jsonObject.put("phoneNumber", this.phoneNumber);
jsonObject.put("city", this.city);
jsonObject.put("zipCode", this.zipCode);
jsonObject.put("address", this.address);
jsonObject.put("login", this.login == null? null : login.toJsonObject());
return jsonObject;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOfficeName() {
return officeName;
}
public void setOfficeName(String officeName) {
this.officeName = officeName;
}
public String geteMail() {
return eMail;
}
public void seteMail(String eMail) {
this.eMail = eMail;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Login getLogin() {
return login;
}
public void setLogin(Login login) {
this.login = login;
}
}
这些实体通过@OneToOne 关系连接。 我要做的是使用登录类(用户名)字段找到我办公室的名称(officeName)。
这是我在 TourOfficeDAO.java 中的函数:
public TourOffice findOfficeNameByLogin(String username) {
Criteria name = createCriteria();
name.add(Restrictions.eq("login.username", username));
return (TourOffice) name.uniqueResult();
}
它通过 TourOfficeService 到达调用此方法的我的休息控制器。但没关系,因为在 DAO 中抛出异常:
无法解析属性:login.username: 提供manager.model.entity.TourOffice;嵌套异常是 org.hibernate.QueryException:无法解析属性: login.username of: offermanager.model.entity.TourOffice
它找不到“login.username”,也不知道为什么……一切似乎都很好。 我寻找了类似的主题,但我仍然没有设法使这个工作。任何帮助将不胜感激。
编辑 1:
这是我的抽象类DAO.java函数createCriteria()在哪里
public abstract class DAO<MODEL> implements Serializable {
public abstract Class<MODEL> getEntityClass();
@Autowired
protected SessionFactory sessionFactory;
protected Session getSession(){
return sessionFactory.getCurrentSession();
}
protected Query createQuery(String query){
return getSession().createQuery(query);
}
protected SQLQuery createSQLQuery(String query){
return getSession().createSQLQuery(query);
}
protected Criteria createCriteria(){
return getSession().createCriteria(getEntityClass());
}
@SuppressWarnings("unchecked")
public MODEL findById(Integer id) {
return (MODEL) getSession().get(getEntityClass(), id);
}
public void save(MODEL entity) {
getSession().save(entity);
getSession().flush();
}
public void update(MODEL entity) {
getSession().update(entity);
getSession().flush();
}
public void saveOrUpdate(MODEL entity) {
getSession().saveOrUpdate(entity);
getSession().flush();
}
public void delete(MODEL entity) {
getSession().delete(entity);
getSession().flush();
}
public List<MODEL> list(){
Criteria criteria = createCriteria();
@SuppressWarnings("unchecked")
List<MODEL> list = criteria.list();
return list;
}
}
【问题讨论】:
-
从问题中移除样板的 getters/setters 代码并提供 createCriteria() 的代码
-
“样板的 getter/setter”是什么?我添加了 createCriteria() 代码。
-
这意味着它们与您的问题无关,因此您应该删除它们。
-
我已将它们粘贴在某个主题中,因为有人说 getter 和 setter 对 Hibernate 查询有影响
-
题外话:为什么你的属性不是私有的?
标签: java spring hibernate model-view-controller