将附加列映射到复合 ID,同时保留其属性映射
我使用中间实体类将附加列映射到可连接的列,只要从相关表的两个 fkey 生成 Id,它就可以正常工作。
我想将第三列“修订版”从同一实体类实现到复合 ID 中,并且仍然需要将其与普通属性映射一起使用。复合 ID 映射工作正常,但“修订版”的法线映射则不然。
我还没有找到一个好的解决方案,而不使用冗余数据/列来解决这个问题,但我想知道为什么,使用实体类与额外列进行连接是一种常见的方法?
我将不胜感激有关如何正确映射此信息的信息,或有关它的信息链接,谢谢您的帮助。
“ 初始 SessionFactory 创建失败。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
);
im using an intermediate entity-class to map additional columns to a jointable and it works fine as long as the Id will be generated from the both fkeys from the involved tables.
I want to implement a 3rd column "revision" from the same entity-class into the composite-id, and still need to use it with normal property-mapping. The composite-id mapping works fine, but the normal mapping for "revision" isnt.
I havnt found a good solution without using redundant data/columns for this problem, but i wonder why ,using entity-classes for jointables with extracolumns is a common way?
I would be gratefull for informations about how to map this correctly, or for informative links about it, thank you for help.
" Initial SessionFactory creation failed.org.hibernate.MappingException: An association from the table backlogaufgabe refers to an unmapped class: int " will be shown with this mapping:
<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
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在创建表时,您应该尝试将修订版作为主键。因为正如我在映射文件中注意到的那样,您没有提到已经是主键的
ID
,而是添加了修订版作为主键,而实际表上的情况并非如此。如果你明白我的意思。因此,也许您应该将修订版本而不是 id 作为主键。试试这个:
You should try to put revision as a primary key while creating your table. Because as I've notice on the mapping file you didn't mentioned the
ID
that was already a primary key, and instead you added revision as a primary key which is not the case on the real table. If you see what I mean. So maybe you should put revision as primary key instead of the id.Try this: