Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法解析属性-休眠_Java_Spring_Hibernate_Model View Controller - Fatal编程技术网

Java 无法解析属性-休眠

Java 无法解析属性-休眠,java,spring,hibernate,model-view-controller,Java,Spring,Hibernate,Model View Controller,我对Hibernate有问题。我从昨天开始就在努力解决这个问题,看起来很简单,但我不知道为什么它不起作用 我有entityLogin.java: package offersmanager.model.entity; import org.json.JSONObject; import javax.persistence.*; @Entity public class Login { @Id @GeneratedValue private Integer id;

我对Hibernate有问题。我从昨天开始就在努力解决这个问题,看起来很简单,但我不知道为什么它不起作用

我有entityLogin.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;
    }
}
   public TourOffice findOfficeNameByLogin(String username) {
        Criteria name = createCriteria();
        name.add(Restrictions.eq("login.username", username));
        return (TourOffice) name.uniqueResult();
    }
这些实体通过@OneToOne关系连接。 我要做的是用Login类(username)字段查找我办公室的名称(officeName)

这是我在TourOfficeDAO.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;
    }
}
   public TourOffice findOfficeNameByLogin(String username) {
        Criteria name = createCriteria();
        name.add(Restrictions.eq("login.username", username));
        return (TourOffice) name.uniqueResult();
    }
它通过TourOfficeService到达调用此方法的rest控制器。但这并不重要,因为在DAO中抛出了异常:

无法解析属性:login.username的: offersmanager.model.entity.TourOffice;嵌套异常是 org.hibernate.QueryException:无法解析属性: login.username of:offersmanager.model.entity.TourOffice

它找不到“login.username”,也不知道为什么。。。一切似乎都很好。 我找了一些类似的话题,但我还是没能做到这一点。任何帮助都将不胜感激

编辑1:

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;
    }
}
   public TourOffice findOfficeNameByLogin(String username) {
        Criteria name = createCriteria();
        name.add(Restrictions.eq("login.username", username));
        return (TourOffice) name.uniqueResult();
    }
这是我的抽象类DAO.java,其中是createCriteria()函数

公共抽象类DAO实现了可序列化{
公共抽象类getEntityClass();
@自动连线
受保护的SessionFactory SessionFactory;
受保护会话getSession(){
返回sessionFactory.getCurrentSession();
}
受保护的查询createQuery(字符串查询){
返回getSession().createQuery(查询);
}
受保护的SQLQuery createSQLQuery(字符串查询){
返回getSession().createSQLQuery(查询);
}
受保护的条件createCriteria(){
返回getSession().createCriteria(getEntityClass());
}
@抑制警告(“未选中”)
公共模型findById(整数id){
返回(模型)getSession().get(getEntityClass(),id);
}
公共作废保存(模型实体){
getSession().save(实体);
getSession().flush();
}
公共无效更新(模型实体){
getSession().update(实体);
getSession().flush();
}
公共作废保存或更新(模型实体){
getSession().saveOrUpdate(实体);
getSession().flush();
}
公共作废删除(模型实体){
getSession().delete(实体);
getSession().flush();
}
公开名单(){
Criteria=createCriteria();
@抑制警告(“未选中”)
List=criteria.List();
退货清单;
}
}

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

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

从问题中删除样板getter/setters代码,并提供createCriteria()的代码,您认为“样板getter/setters”是什么?我添加了createCriteria()代码。这意味着它们与您的问题无关,所以您应该删除它们。我粘贴它们是因为在某个主题中有人说getter和setter对Hibernate QueriesOfftopic有影响问题:为什么您的属性不是私有的?是的,现在它可以工作了。非常感谢。但我不知道为什么我必须创建别名。我的朋友有非常相似的代码,但与一个omany(或许多-不记得很清楚)有关,他不必创建别名。当您创建别名时,Hibernate发出“加入”请求(如果您想查看Hibernate生成的sql,可以显示sql)。对于您的好友连接信息,可以在OneToMany或ManyToMany属性上设置(检查@JoinTable信息)