无法解析属性 - Hibernate

时间:2015-11-24 12:10:24

标签: java spring hibernate model-view-controller

我遇到了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到我的休息控制器,调用此方法。但是没关系导致DAO抛出异常:

  

无法解析属性:login.username:   offersmanager.model.entity.TourOffice;嵌套异常是   org.hibernate.QueryException:无法解析属性:   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;
    }
}

1 个答案:

答案 0 :(得分:3)

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

func playVideo() {
        let path = NSBundle.mainBundle().pathForResource("WelcomeVideo", ofType: "mp4")
        let pathURL = NSURL.fileURLWithPath(path!)

        let myPlayer = MPMoviePlayerViewController(contentURL: pathURL)
        myPlayer.moviePlayer.repeatMode = .One

        presentMoviePlayerViewControllerAnimated(myPlayer)
    }