如何映射具有额外列的连接 +表列作为 Id 的一部分?
我使用中间实体类将附加列映射到可连接的列。
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
);
public static class Id implements Serializable {
/** fkey Backlog. */
private int backlogId;
/** fkey Aufgabe. */
private int aufgabeId;
/** fkey Revision. */
private int revisionId;
/** Default-Konstruktor. */
public Id() { }
/**
* Konstruktor - Both Id.
*
* @param pAufgabeId Die Aufgaben-Id
* @param pBacklogId Die Backlog-Id
*/
public Id(final int pBacklogId, final int pAufgabeId,
final int pRevisionId) {
this.backlogId = pBacklogId;
this.aufgabeId = pAufgabeId;
this.revisionId = pRevisionId;
}
@Override
public final boolean equals(final Object obj) {
if (obj != null && obj instanceof Id) {
Id that = (Id) obj;
return (this.backlogId == that.backlogId
&& this.aufgabeId == that.aufgabeId
&& this.revisionId == that.revisionId);
} else {
return false;
}
}
@Override
public final int hashCode() {
return this.backlogId + this.aufgabeId + this.revisionId;
}
}
public class BacklogAufgabe {
private Id id = new Id();
private Backlog backlog;
private Aufgabe aufgabe;
private int revision;
private java.sql.Date datum;
private int rang;
private int aufwSchaetzung;
private int aufwMessung;
/** Default-Konstruktor. */
public BacklogAufgabe() { }
....
....
}
Id 类根据 3 个值生成 Id 作为复合 id,来自两个表的 fkey + 1 个来自类的值,该值是且必须是冗余的。
我的问题是:我需要如何使用 xml 正确映射它?我认为映射复合ID不是一个问题,但我只能在映射文件中映射第三个值“修订版”一次,我没有找到任何可以帮助我解决这个问题的东西...
这是我实际的映射尝试:
<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>
因此,如果有人知道如何使用 3 个复合 id 值“fkey1、fkey2、“类中的值”来映射此 sql 表,同时保留“类中的值”的属性映射“ 我会 的任何提示和答案。
换句话说,如何将列映射为复合键和普通属性的一部分,两者一起映射在一个映射中?
感谢您
im using an intermediate entity-class to map additional columns to a jointable.
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
);
public static class Id implements Serializable {
/** fkey Backlog. */
private int backlogId;
/** fkey Aufgabe. */
private int aufgabeId;
/** fkey Revision. */
private int revisionId;
/** Default-Konstruktor. */
public Id() { }
/**
* Konstruktor - Both Id.
*
* @param pAufgabeId Die Aufgaben-Id
* @param pBacklogId Die Backlog-Id
*/
public Id(final int pBacklogId, final int pAufgabeId,
final int pRevisionId) {
this.backlogId = pBacklogId;
this.aufgabeId = pAufgabeId;
this.revisionId = pRevisionId;
}
@Override
public final boolean equals(final Object obj) {
if (obj != null && obj instanceof Id) {
Id that = (Id) obj;
return (this.backlogId == that.backlogId
&& this.aufgabeId == that.aufgabeId
&& this.revisionId == that.revisionId);
} else {
return false;
}
}
@Override
public final int hashCode() {
return this.backlogId + this.aufgabeId + this.revisionId;
}
}
public class BacklogAufgabe {
private Id id = new Id();
private Backlog backlog;
private Aufgabe aufgabe;
private int revision;
private java.sql.Date datum;
private int rang;
private int aufwSchaetzung;
private int aufwMessung;
/** Default-Konstruktor. */
public BacklogAufgabe() { }
....
....
}
The Id-class generates the Id as a composite-id from 3 values, the fkeys from both tables + 1 value from the class which is and must be redundant.
My question is: how do i need to map this correctly with xml? Mapping the composite-id isnt a problem i think, but i only can map the 3rd value "revision" one time in a mapping file, i havnt found anything that helps me with this problem...
Here is my actual mapping-try:
<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>
So, if anyone knows how to map this sql-table with the 3 composite-id values "fkey1, fkey2, "value-from-the-class" while keeping the mapping for the property for the "value-from-the-class" i would be thankfull for any tips and answers.
In other words, how is it possible to map a column as part of the composite-key and as normal property, both together in one mapping?
Thank you for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论