1

我使用中间实体类将其他列映射到可连接的列,只要 Id 将从相关表的两个 fkey 生成,它就可以正常工作。

我想将同一实体类中的第三列“修订”实现到复合 ID 中,并且仍然需要将其与普通属性映射一起使用。复合标识映射工作正常,但“修订”的正常映射不是。

我没有找到一个很好的解决方案,而不使用冗余数据/列来解决这个问题,但我想知道为什么使用带有额外列的可连接实体类是一种常见的方法?

我将不胜感激有关如何正确映射它的信息,或有关它的信息链接,谢谢您的帮助。

“ Initial SessionFactory creation failed.org.hibernate.MappingException: An association from the table backlogaufgabe refer to an unmapped class: int” 将与此映射一起显示:

<hibernate-mapping package="app.domain">
 <class mutable="false" name="app.domain.BacklogAufgabe" table="backlogaufgabe">
  <composite-id class="BacklogAufgabe$Id" name="id">
   <key-property access="field" column="id_backlog" name="backlogId"/>
   <key-property access="field" column="id_aufgabe" name="aufgabeId"/>
   <key-property access="field" column="revision" name="revisionId"/>
  </composite-id>
  <property column="datum" name="datum" not-null="true" type="date"/>
  <property column="rang" name="rang" not-null="true" type="int"/>

  <property column="revision" name="revision" not-null="true" type="int"/>

  <property column="aufw_schaetzung" name="aufwSchaetzung" not-null="true" type="int"/>
  <property column="aufw_messung" name="aufwMessung" not-null="true" type="int"/>
  <many-to-one cascade="save-update" column="id_aufgabe" insert="false" lazy="false"
    name="aufgabe" not-null="true" update="false"/>
  <many-to-one cascade="save-update" column="id_backlog" insert="false" lazy="false"
    name="backlog"  not-null="true" update="false"/>
  <many-to-one cascade="save-update" column="revision" insert="false" lazy="false"
    name="revision" not-null="true" update="false"/>
 </class>
</hibernate-mapping>

SQL:

 CREATE TABLE backlogaufgabe
 (
   id serial NOT NULL,
   id_backlog integer NOT NULL,
   id_aufgabe integer NOT NULL,
   revision integer NOT NULL,
   datum date NOT NULL,
   rang integer NOT NULL,
   aufw_schaetzung integer,
   aufw_messung integer,
   CONSTRAINT backlogaufgabe_pkey PRIMARY KEY (id),
   CONSTRAINT backlogaufgabe_id_aufgabe_fkey FOREIGN KEY (id_aufgabe)
       REFERENCES aufgabe (id) MATCH SIMPLE
       ON UPDATE CASCADE ON DELETE RESTRICT,
   CONSTRAINT backlogaufgabe_id_backlog_fkey FOREIGN KEY (id_backlog)
       REFERENCES backlog (id) MATCH SIMPLE
       ON UPDATE CASCADE ON DELETE RESTRICT
 )
 WITH (
   OIDS=FALSE
 );
4

1 回答 1

0

在创建表时,您应该尝试将修订作为主键。因为正如我在映射文件中注意到的那样,您没有提到ID已经是主键,而是您添加了修订作为主键,而实际表中并非如此。如果你明白我的意思。所以也许你应该把修订作为主键而不是 id。

尝试这个:

CREATE TABLE backlogaufgabe
(
   id_backlog integer NOT NULL,
   id_aufgabe integer NOT NULL,
   revision integer NOT NULL,
   datum date NOT NULL,
   rang integer NOT NULL,
   aufw_schaetzung integer,
   aufw_messung integer,
   CONSTRAINT backlogaufgabe_pkey PRIMARY KEY (revision),
   CONSTRAINT backlogaufgabe_id_aufgabe_fkey FOREIGN KEY (id_aufgabe)
       REFERENCES aufgabe (id) MATCH SIMPLE
       ON UPDATE CASCADE ON DELETE RESTRICT,
   CONSTRAINT backlogaufgabe_id_backlog_fkey FOREIGN KEY (id_backlog)
       REFERENCES backlog (id) MATCH SIMPLE
       ON UPDATE CASCADE ON DELETE RESTRICT
)
WITH (
   OIDS=FALSE
);
于 2011-06-30T12:04:01.250 回答