gpt4 book ai didi

java - 无法解析属性 - hibernate

转载 作者:行者123 更新时间:2023-11-29 04:55:38 26 4
gpt4 key购买 nike

我有 Hibernate 的问题。从昨天开始我就在努力解决这个问题,这似乎很容易,但我不知道为什么它不起作用......

我有实体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 到达调用此方法的其余 Controller 。但这并不重要,因为在 DAO 中抛出了异常:

could not resolve property: login.username of: offersmanager.model.entity.TourOffice; nested exception is org.hibernate.QueryException: could not resolve property: login.username of: offersmanager.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;
}
}

最佳答案

我认为您首先需要创建一个这样的别名:

  public TourOffice findOfficeNameByLogin(String username) {
Criteria name = createCriteria();
name.createAlias("login", "login");
name.add(Restrictions.eq("login.username", username));
return (TourOffice) name.uniqueResult();
}

关于java - 无法解析属性 - hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33893398/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com