使用 Hibernate 的外键将对象与另一个表中的数据映射

发布于 2024-12-10 07:15:08 字数 1855 浏览 0 评论 0原文

我正在尝试使用 Hibernate 和 javax 持久性注释从数据库填充此类中的数据。以下是相关的数据库结构。

table Poss_resp     
ID  qst_id  resp_text
int int text

table Responses         
ID  qst_id  usr_id  resp_id
int int int int

我正在尝试使用 Responses.ID 和 Poss_resp.resp_text 填充下面显示的 Response 类。 Poss_resp 保存问题的可能答案。响应保存给出的实际答案。 resp_id 是 Poss_resp 的外键。但是,我只想存储 resp_text 字符串,我不需要一个全新的对象。有什么方法可以实现这一目标吗?我无法弄清楚如何告诉 Hibernate 如何使用 Response 主键以外的其他内容,也没有确定正确的 JOIN 语法。

我的响应类:

@Entity
@Table(name="responses")
public class Response {

    private long id;
    private long qst_id;
    private long resp_id;
    private String resp_text;


    /**
     * 
     */
    public Response() {
        // TODO Auto-generated constructor stub
    }


    /**
     * @return the id
     */
    @Id
    @Generated(value="assigned")
    @Column(name="ID")
    public long getId() {
        return id;
    }


    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }


    /**
     * @return the qst_id
     */
    @Column(name="qst_id")
    public long getQst_id() {
        return qst_id;
    }


    /**
     * @param qst_id the qst_id to set
     */
    public void setQst_id(long qst_id) {
        this.qst_id = qst_id;
    }


    /**
     * @return the resp_id
     */
    @Column(name="resp_id")
    public long getResp_id() {
        return resp_id;
    }


    /**
     * @param resp_id the resp_id to set
     */
    public void setResp_id(long resp_id) {
        this.resp_id = resp_id;
    }


    /**
     * @return the resp_text
     */
    public String getResp_text() {
        return resp_text;
    }


    /**
     * @param resp_text the resp_text to set
     */
    public void setResp_text(String resp_text) {
        this.resp_text = resp_text;
    }

如果可能的话,我更喜欢注释。

I am trying to populate the data within this class from a database using Hibernate and javax persistence annotations. Here are the relevant database structures.

table Poss_resp     
ID  qst_id  resp_text
int int text

table Responses         
ID  qst_id  usr_id  resp_id
int int int int

I am trying to populate the Response class shown below with Responses.ID, and Poss_resp.resp_text. Poss_resp holds possible answers to a question. Responses holds the actual answers given. resp_id is a foreign key for Poss_resp. However, I just want the resp_text string stored, I do not want a whole new object. Is there some way to achieve this? I cannot figure out how to tell Hibernate how to use something other than Response's primary key, nor have I determined the proper JOIN syntax.

My Response class:

@Entity
@Table(name="responses")
public class Response {

    private long id;
    private long qst_id;
    private long resp_id;
    private String resp_text;


    /**
     * 
     */
    public Response() {
        // TODO Auto-generated constructor stub
    }


    /**
     * @return the id
     */
    @Id
    @Generated(value="assigned")
    @Column(name="ID")
    public long getId() {
        return id;
    }


    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }


    /**
     * @return the qst_id
     */
    @Column(name="qst_id")
    public long getQst_id() {
        return qst_id;
    }


    /**
     * @param qst_id the qst_id to set
     */
    public void setQst_id(long qst_id) {
        this.qst_id = qst_id;
    }


    /**
     * @return the resp_id
     */
    @Column(name="resp_id")
    public long getResp_id() {
        return resp_id;
    }


    /**
     * @param resp_id the resp_id to set
     */
    public void setResp_id(long resp_id) {
        this.resp_id = resp_id;
    }


    /**
     * @return the resp_text
     */
    public String getResp_text() {
        return resp_text;
    }


    /**
     * @param resp_text the resp_text to set
     */
    public void setResp_text(String resp_text) {
        this.resp_text = resp_text;
    }

If at all possible, I would prefer annotations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的新欢旧爱 2024-12-17 07:15:08

ResponsesPoss_resp 表的关系是多对一的,您应该在 Response 类中映射这种关系。否则,hibernate 没有足够的知识来获取给定的 Responses.resp_id 的 Poss_resp.resp_text

您的 Poss_resp 表应该映射到实体类(说类PossResp

然后,您可以使用@ManyToOne@JoinColumn声明多对一关系,如下所示:

@Entity
@Table(name="responses")
public class Response {
   ..................
   private PossResp possResp;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "resp_id") 
    public PossResp getPossResp() {
        return possResp;
    }
    ................

}

要获取给定 Response 对象的 resp_text,请调用

 response.getPossResp().getRespText();

我不想要一个全新的对象。有什么办法可以实现这一点吗?

如果你只映射long resp_id而不是PossResp实体中的PossResp对象,恐怕你必须手动编写HQL/SQL /Criteria 使用 long resp_id 获取 resp_text,然后通过setResp_text() 设置器。但这样一来,你肯定无法享受到hibernate IMO提供的好处。

The relationship of Responses and Poss_resp table is many-to-one , you should map this relationship in the Response class . Otherwise , hibernate has not enough knowledge to get Poss_resp.resp_text given the Responses.resp_id

Your Poss_resp table should be mapped to an an entity class (say Class PossResp)

Then , you can declare many-to-one relationship using the @ManyToOne and @JoinColumn , like this:

@Entity
@Table(name="responses")
public class Response {
   ..................
   private PossResp possResp;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "resp_id") 
    public PossResp getPossResp() {
        return possResp;
    }
    ................

}

To get the resp_text given an Response object , call

 response.getPossResp().getRespText();

I do not want a whole new object. Is there some way to achieve this?

If you only map the long resp_id instead of the PossResp object in PossResp entity , I am afraid that you have to manually write the HQL/SQL/Criteria to get the resp_text using the long resp_id and then set it back to the Response via the setResp_text() setter . But in this way, you definitely cannot enjoy the benefits provided by hibernate IMO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文