【问题标题】:Java Hibernate Mapping File not workingJava Hibernate 映射文件不起作用
【发布时间】:2017-10-21 05:06:48
【问题描述】:

我得到的错误是 " org.hibernate.MappingException: 实体映射中的重复列:cdd.model.Answer 列:answer_id(应该使用 insert="false" update="false" 进行映射)" 。但是,当我将这些作为属性时,我得到了错误:“必须为元素类型“id”声明属性“插入”。”

任何帮助将不胜感激。

类:

public class Answer {
UUID answerID;
String content;

//constructors and getters and setters

}

表:

    CREATE TABLE IF NOT EXISTS answer (

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

休眠映射文件:

<!-->==== 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>

注意 我有一个问题类,其中一个问题有一组答案。这是我使用的映射。我发布它是因为我遇到的错误可能是因为我如何映射这种 一对多 关系(我不确定)。

<!-- ==== 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>

【问题讨论】:

    标签: java database hibernate orm mapping


    【解决方案1】:

    问题实体:

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

    回答实体:

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

    从 Answr 实体中移除 insert="false" update="false"

    应该是这样的

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

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

    【讨论】:

    • 当我删除它时,我收到此错误:org.hibernate.MappingException:实体映射中的重复列:cdd.model.Answer 列:answer_id(应使用 insert="false" update= 映射“假”)@Ashish451
    猜你喜欢
    • 2016-10-02
    • 1970-01-01
    • 2012-04-17
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多