2011-06-30 61 views
1

即时使用中间实体类将附加列映射到连接表,并且只要从两个fkeys生成Id,它就能正常工作从涉及的表中。将附加列映射到复合id,同时保留其属性映射

我想实现从同一个实体类到合成id的第三列“修订”,仍然需要使用它与正常的属性映射。复合标识映射工作正常,但“修订”的正常映射不是。

我havnt找到了一个很好的解决方案,但没有为这个问题使用冗余的数据/列,但我想知道为什么,使用实体类与joincolumns的jointables是一种常见的方式?

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

“初始会话工厂创建failed.org.hibernate.MappingException:从表backlogaufgabe的关联是指未映射的类: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 
); 
将与该映射示出被

回答

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 
); 
+0

你好cascadox,THX的答案。版本是并且必须是多余的,所以它不能成为表格的pKey。 – MrMiyagi