本文介绍了休眠:@JoinTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我在休眠状态下了解了@joincolumn.现在,我从@JoinTable开始.以下是我的POJO

I have understood @joincolumn in hibernate. Now I have started with @JoinTable. Following is my POJO

@Entity
@Table(name = "person")
public class Person {

    @Id
    @GeneratedValue
    @Column(name = "person_id")
    private long person_id;


    @Column(name = "name")
    private String name;


    @Column(name = "Address")
    private String Address;


    @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
        @JoinTable(name="person_phone",
        joinColumns={@JoinColumn(name="person_id", referencedColumnName="person_id")},
        inverseJoinColumns={@JoinColumn(name="phone_id", referencedColumnName="phone_id")})
    private Phone phone;

        //Getters ande Setters

}


@Entity
@Table(name = "phone")
public class Phone{

    @Id
    @GeneratedValue
    @Column(name = "phone_id")
    private long phone_id;


    @Column(name = "name")
    private String name;


        //Getters ande Setters


}

在我的控制器中,我已完成以下操作

In my controller, I have done the following

 @RequestMapping(value = "/persons", method = RequestMethod.GET)
    public List<person> listPersons() {

        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM person);
        Query query = getSession().createSQLQuery(sql.toString()).addEntity(Person.class);
        return query.list();
    }

,当我执行时,从person中选择*.我收到以下错误

and when I execute as Select * from person . I am getting the following error

我想要实现的是,当我从 person 表中检索记录时,我还想要相应的电话详细信息.

What I want to achieve is , when I retrieve a record from person table I want the corresponding phone details also .

推荐答案

选择Hibernate createQuery方法而不是createSqlQuery来匹配实体更为准确.请尝试以下;

Choosing Hibernate createQuery method instead createSqlQuery to match entity is more accurate.Try below;

    StringBuilder sql = new StringBuilder();
    sql.append("FROM Person");  //using hibernate query
    Query query = getSession().createQuery(sql.toString()); //Hibernate query so use createQuery not createSqlQuery
    return query.list();

这篇关于休眠:@JoinTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 19:23