开发者

HowTo map a jointable with extra columns + table-column as part of the Id?

开发者 https://www.devze.com 2023-03-16 22:00 出处:网络
im using an intermediate entity-class to map additional columns to a jointable. SQL: CREATE TABLE backlogaufgabe

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号