Java Hibernate映射文件无法正常工作 [英] Java Hibernate Mapping File not working

查看:135
本文介绍了Java Hibernate映射文件无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的错误是org.hibernate.MappingException:实体映射中的重复列:cdd.model.Answer column:answer_id(应该用insert =falseupdate =false映射) 即可。但是,当我把这些作为属性,我得到的错误:必须为元素类型ID声明属性插入。



任何帮助将不胜感激。



Class:

  public class Answer {
UUID answer ID;
字符串内容;

//构造函数和getter和setters

}

Table:

  CREATE TABLE IF NOT EXISTS回答(

answer_id uuid NOT NULL,
content text NOT NULL,
主键(answer_id)
);

Hibernate映射文件:

 <! - > ====回答====<! - > 
< class name =cdd.model.Answertable =answer>
< id column =answer_idname =answerID
type =org.hibernate.type.PostgresUUIDType
insert =falseupdate =false>
< generator class =org.hibernate.id.UUIDGenerator/>
< / id>

< property column =contentname =contenttype =org.hibernate.type.TextType/>
< / class>

注意
我有一个问题类,一组答案。这是我使用的映射。我张贴它,因为我gettng错误可能是因为我如何映射此一对多关系(我不确定)。

 <! -  ====问题====  - > 
< class name =cdd.model.Questiontable =question>
< id column =question_idname =questionIDtype =org.hibernate.type.PostgresUUIDType>
< generator class =org.hibernate.id.UUIDGenerator/>
< / id>
<多对一列=parentCategoryname =parentCategorynot-null =true/>

< property column =titlename =titletype =org.hibernate.type.TextType/>
< property column =correct_answername =correctAnswertype =org.hibernate.type.TextType/>
< property column =date_submittedname =dateSubmittedtype =org.hibernate.type.TimestampType/>

< key column =answer_idnot-null =true/>
< / set>

< join inverse =trueoptional =truetable =category_questions>
< key column =question_id/>
< / join>
< join inverse =trueoptional =truetable =accepted_questions_by_user>
< key column =question_id/>
< / join>
< / class>


解决方案

问题实体:

 < id column =question_idname =questionIDtype =org.hibernate.type.PostgresUUIDType> 
< generator class =org.hibernate.id.UUIDGenerator/>
< / id>

答案实体:

 < id column =answer_idname =answerIDtype =org.hibernate.type.PostgresUUIDType
insert =falseupdate =false>
< generator class =org.hibernate.id.UUIDGenerator/>
< / id>

删除 insert =falseupdate =false

应该看起来像这样

 来自Answr实体的$ c> < id column =answer_idname =answerIDtype =org.hibernate.type.PostgresUUIDType> 
< generator class =org.hibernate.id.UUIDGenerator/>
< / id>

update,insert(可选 - 默认为true):指定映射列应包含在SQL UPDATE和/或INSERT语句。将两者都设置为false允许一个纯粹的派生属性,其值由映射到相同列的其他属性或触发器或其他应用程序初始化。


The error I get is " org.hibernate.MappingException: Repeated column in mapping for entity: cdd.model.Answer column: answer_id (should be mapped with insert="false" update="false") ". However when I put those as attributes I get the error: "Attribute "insert" must be declared for element type "id"."

Any help would be appreciated.

Class:

public class Answer {
UUID answerID;
String content;

//constructors and getters and setters

}

Table:

    CREATE TABLE IF NOT EXISTS answer (

    answer_id uuid NOT NULL ,
    content text NOT NULL,
    primary key(answer_id)
    );

Hibernate Mapping File:

<!-->==== Answer ====<!-->
<class name="cdd.model.Answer" table="answer" >
<id column="answer_id" name="answerID" 
    type="org.hibernate.type.PostgresUUIDType"  
    insert="false" update="false">
  <generator class="org.hibernate.id.UUIDGenerator"/>
</id>

<property column="content" name="content" type="org.hibernate.type.TextType"/>
</class>

Note I have a Question class where a question has a set of answers. This is the mapping I used. I am posting it because the error I'm gettng may be because of how I mapped this one-to-many relationship (I'm not sure).

<!-- ==== Question ==== -->
<class name="cdd.model.Question" table="question">
<id column="question_id" name="questionID" type="org.hibernate.type.PostgresUUIDType">
  <   generator class="org.hibernate.id.UUIDGenerator"/>
</id>
<many-to-one column="submitted_by" name="submittedBy" not-null="true"/>
<many-to-one column="parentCategory" name="parentCategory" not-null="true"/>

<property column="title" name="title" type="org.hibernate.type.TextType"/>
<property column="correct_answer" name="correctAnswer" type="org.hibernate.type.TextType"/>
<property column="date_submitted" name="dateSubmitted" type="org.hibernate.type.TimestampType"/>

<set cascade="all" name="answers" table="answer">
  <key column="answer_id" not-null="true"/>
  <one-to-many class="cdd.model.Answer"/>
</set>

<join inverse="true" optional="true" table="category_questions">
  <key column="question_id"/>
</join>
<join inverse="true" optional="true" table="accepted_questions_by_user">
  <key column="question_id"/>
</join>
</class>

解决方案

Question Entity:

<id column="question_id" name="questionID" type="org.hibernate.type.PostgresUUIDType">
  <generator class="org.hibernate.id.UUIDGenerator"/>
</id>

Answer Entity:

<id column="answer_id" name="answerID" type="org.hibernate.type.PostgresUUIDType"  
    insert="false" update="false">
  <generator class="org.hibernate.id.UUIDGenerator"/>
</id>

remove insert="false" update="false" from Answr entity

should look someting like this

<id column="answer_id" name="answerID" type="org.hibernate.type.PostgresUUIDType" >
  <generator class="org.hibernate.id.UUIDGenerator"/>
</id>

update, insert (optional - defaults to true): specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" property whose value is initialized from some other property that maps to the same column(s), or by a trigger or other application.

这篇关于Java Hibernate映射文件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆