[英]Mapping object with data from another table using foreign key with Hibernate
I am trying to populate the data within this class from a database using Hibernate and javax persistence annotations. 我正在尝试使用Hibernate和javax持久性注释从数据库中填充此类中的数据。 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. 我正在尝试用Responses.ID和Poss_resp.resp_text填充下面显示的Response类。 Poss_resp holds possible answers to a question.
Poss_resp包含问题的可能答案。 Responses holds the actual answers given.
答案包含给出的实际答案。 resp_id is a foreign key for Poss_resp.
resp_id是Poss_resp的外键。 However, I just want the resp_text string stored, I do not want a whole new object.
但是,我只想要存储resp_text字符串,而不想要一个全新的对象。 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.
我无法弄清楚如何告诉Hibernate如何使用Response的主键以外的东西,也无法确定正确的JOIN语法。
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. 如果有可能,我希望使用注释。
The relationship of Responses
and Poss_resp
table is many-to-one , you should map this relationship in the Response
class . Responses
与Poss_resp
表的关系是多对一的,您应该在Response
类中映射此关系。 Otherwise , hibernate has not enough knowledge to get Poss_resp.resp_text
given the Responses.resp_id
否则,在给出
Responses.resp_id
,休眠状态没有足够的知识来获取Poss_resp.resp_text
Your Poss_resp
table should be mapped to an an entity class (say Class PossResp
) 您的
Poss_resp
表应该映射到一个实体类(例如Class PossResp
)
Then , you can declare many-to-one relationship using the @ManyToOne
and @JoinColumn
, like this: 然后,您可以使用
@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;
}
................
}
To get the resp_text
given an Response
object , call 要获得给定一个
Response
对象的resp_text
,请调用
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 . 如果只图
long resp_id
代替PossResp
的对象PossResp
实体,恐怕你必须手动写HQL / SQL /标准得到resp_text
使用long resp_id
然后将其设置回Response
通过setResp_text()
二传手。 But in this way, you definitely cannot enjoy the benefits provided by hibernate IMO. 但是以这种方式,您绝对不能享受休眠IMO提供的好处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.