hibernate 在使用 eclipse 生成 hibernate 类时为具有复合主键的表创建问题
我有两张表,如下所示。
Feedback_answer
Question_id 数字(主键)
Answer_ID 数字(主键)
Answer_en varchar2(255)
Answer_it varchar2(255)
user_feedback
verify_id 数字(主键)
Question_id 数字(外键 - Feedback_answer)
answer_id 数字(外键 - Feedback_answer)
我正在关注在 Eclipse 中生成 Hibernate 类后发生 Hibernate 错误
ERROR:: 2009-12-29 14:57:34,093 错误 java.lang.Class (http-8080-2) - 初始 SessionFactory 创建失败.org.hibernate.MappingException:外键 (FK39E53D791A62BD16:USER_FEEDBACK [QUESTION_ID])) 必须具有相同的编号作为引用主键的列数 (FEEDBACK_ANSWER [QUESTION_ID,ANSWER_ID])
以下是 Feedback_answer 的 Java 代码
public class FeedbackAnswer implements java.io.Serializable {
private FeedbackAnswerId id;
private FeedbackQuestion feedbackQuestion;
private String answerEn;
private String answerIt;
private Set<Verification> verifications = new HashSet<Verification>(0);
public FeedbackAnswer() {
}
public FeedbackAnswer(FeedbackAnswerId id, FeedbackQuestion feedbackQuestion) {
this.id = id;
this.feedbackQuestion = feedbackQuestion;
}
public FeedbackAnswer(FeedbackAnswerId id,
FeedbackQuestion feedbackQuestion, String answerEn,
String answerIt, Set<Verification> verifications) {
this.id = id;
this.feedbackQuestion = feedbackQuestion;
this.answerEn = answerEn;
this.answerIt = answerIt;
this.verifications = verifications;
}
public FeedbackAnswerId getId() {
return this.id;
}
public void setId(FeedbackAnswerId id) {
this.id = id;
}
public FeedbackQuestion getFeedbackQuestion() {
return this.feedbackQuestion;
}
public void setFeedbackQuestion(FeedbackQuestion feedbackQuestion) {
this.feedbackQuestion = feedbackQuestion;
}
public String getAnswerEn() {
return this.answerEn;
}
public void setAnswerEn(String answerEn) {
this.answerEn = answerEn;
}
public String getAnswerIt() {
return this.answerIt;
}
public void setAnswerIt(String answerIt) {
this.answerIt = answerIt;
}
public Set<Verification> getVerifications() {
return this.verifications;
}
public void setVerifications(Set<Verification> verifications) {
this.verifications = verifications;
}
}
以下是 Feedback_answer 的 Hibernate 代码
<hibernate-mapping>
<class name="com.certilogo.inspect.backend.FeedbackAnswer" table="FEEDBACK_ANSWER" schema="SCOTT">
<composite-id name="id" class="com.certilogo.inspect.backend.FeedbackAnswerId">
<key-property name="answerId" type="long">
<column name="ANSWER_ID" precision="22" scale="0" />
</key-property>
<key-property name="questionId" type="long">
<column name="QUESTION_ID" precision="22" scale="0" />
</key-property>
</composite-id>
<many-to-one name="feedbackQuestion" class="com.certilogo.inspect.backend.FeedbackQuestion" update="false" insert="false" fetch="select">
<column name="QUESTION_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="answerEn" type="string">
<column name="ANSWER_EN" />
</property>
<property name="answerIt" type="string">
<column name="ANSWER_IT" />
</property>
<set name="verifications" inverse="true" table="USER_FEEDBACK">
<key>
<column name="ANSWER_ID" precision="22" scale="0" />
<column name="QUESTION_ID" precision="22" scale="0" />
</key>
<many-to-many entity-name="com.certilogo.inspect.backend.Verification">
<column name="VERIFICATION_ID" precision="22" scale="0" not-null="true" unique="true" />
</many-to-many>
</set>
</class>
有人能帮我解决这个问题吗?
谢谢。
阿玛尔4金图
I have 2 tables as given below.
feedback_answer
question_id number(primary key)
answer_id number (primary key)
answer_en varchar2(255)
answer_it varchar2(255)
user_feedback
verification_id number(primary key)
question_id number(foreign key - feedback_answer)
answer_id number(foreign key - feedback_answer)
I am getting following error for hibernate after hibernate class generation in eclipse
ERROR ::
2009-12-29 14:57:34,093 ERROR java.lang.Class (http-8080-2) - Initial SessionFactory creation failed.org.hibernate.MappingException: Foreign key (FK39E53D791A62BD16:USER_FEEDBACK [QUESTION_ID])) must have same number of columns as the referenced primary key (FEEDBACK_ANSWER [QUESTION_ID,ANSWER_ID])
Following is the java code for feedback_answer
public class FeedbackAnswer implements java.io.Serializable {
private FeedbackAnswerId id;
private FeedbackQuestion feedbackQuestion;
private String answerEn;
private String answerIt;
private Set<Verification> verifications = new HashSet<Verification>(0);
public FeedbackAnswer() {
}
public FeedbackAnswer(FeedbackAnswerId id, FeedbackQuestion feedbackQuestion) {
this.id = id;
this.feedbackQuestion = feedbackQuestion;
}
public FeedbackAnswer(FeedbackAnswerId id,
FeedbackQuestion feedbackQuestion, String answerEn,
String answerIt, Set<Verification> verifications) {
this.id = id;
this.feedbackQuestion = feedbackQuestion;
this.answerEn = answerEn;
this.answerIt = answerIt;
this.verifications = verifications;
}
public FeedbackAnswerId getId() {
return this.id;
}
public void setId(FeedbackAnswerId id) {
this.id = id;
}
public FeedbackQuestion getFeedbackQuestion() {
return this.feedbackQuestion;
}
public void setFeedbackQuestion(FeedbackQuestion feedbackQuestion) {
this.feedbackQuestion = feedbackQuestion;
}
public String getAnswerEn() {
return this.answerEn;
}
public void setAnswerEn(String answerEn) {
this.answerEn = answerEn;
}
public String getAnswerIt() {
return this.answerIt;
}
public void setAnswerIt(String answerIt) {
this.answerIt = answerIt;
}
public Set<Verification> getVerifications() {
return this.verifications;
}
public void setVerifications(Set<Verification> verifications) {
this.verifications = verifications;
}
}
Following is the hibernate code for feedback_answer
<hibernate-mapping>
<class name="com.certilogo.inspect.backend.FeedbackAnswer" table="FEEDBACK_ANSWER" schema="SCOTT">
<composite-id name="id" class="com.certilogo.inspect.backend.FeedbackAnswerId">
<key-property name="answerId" type="long">
<column name="ANSWER_ID" precision="22" scale="0" />
</key-property>
<key-property name="questionId" type="long">
<column name="QUESTION_ID" precision="22" scale="0" />
</key-property>
</composite-id>
<many-to-one name="feedbackQuestion" class="com.certilogo.inspect.backend.FeedbackQuestion" update="false" insert="false" fetch="select">
<column name="QUESTION_ID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="answerEn" type="string">
<column name="ANSWER_EN" />
</property>
<property name="answerIt" type="string">
<column name="ANSWER_IT" />
</property>
<set name="verifications" inverse="true" table="USER_FEEDBACK">
<key>
<column name="ANSWER_ID" precision="22" scale="0" />
<column name="QUESTION_ID" precision="22" scale="0" />
</key>
<many-to-many entity-name="com.certilogo.inspect.backend.Verification">
<column name="VERIFICATION_ID" precision="22" scale="0" not-null="true" unique="true" />
</many-to-many>
</set>
</class>
Can anyone help me regarding the matter?
Thanks.
amar4kintu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决上述问题..我更改了表结构以仅采用一个主键..它解决了我的问题..我必须这样做,因为我没有太多时间,因为我需要及时完成工作...
所以感谢所有试图在这里帮助我的人..
to solve above problem.. I changed my table structure to take one primary key only.. and it solved my problem.. I had to do that because I do not have much time as I need to complete work in time...
so thanks to all who tried to help me out here..