使用 Hibernate 的外键将对象与另一个表中的数据映射
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Responses
和Poss_resp
表的关系是多对一的,您应该在Response
类中映射这种关系。否则,hibernate 没有足够的知识来获取给定的 Responses.resp_id 的 Poss_resp.resp_text您的 Poss_resp 表应该映射到实体类(说类
PossResp
)然后,您可以使用
@ManyToOne
和@JoinColumn
声明多对一关系,如下所示:要获取给定
Response
对象的resp_text
,请调用如果你只映射
long resp_id
而不是PossResp
实体中的PossResp
对象,恐怕你必须手动编写HQL/SQL /Criteria 使用long resp_id
获取resp_text
,然后通过setResp_text()
设置器。但这样一来,你肯定无法享受到hibernate IMO提供的好处。The relationship of
Responses
andPoss_resp
table is many-to-one , you should map this relationship in theResponse
class . Otherwise , hibernate has not enough knowledge to getPoss_resp.resp_text
given theResponses.resp_id
Your
Poss_resp
table should be mapped to an an entity class (say ClassPossResp
)Then , you can declare many-to-one relationship using the
@ManyToOne
and@JoinColumn
, like this:To get the
resp_text
given anResponse
object , callIf you only map the
long resp_id
instead of thePossResp
object inPossResp
entity , I am afraid that you have to manually write the HQL/SQL/Criteria to get theresp_text
using thelong resp_id
and then set it back to theResponse
via thesetResp_text()
setter . But in this way, you definitely cannot enjoy the benefits provided by hibernate IMO.