-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Exclude columns from primary key
PostPosted: Mon Aug 09, 2010 7:55 pm 
Newbie

Joined: Mon Aug 09, 2010 7:08 pm
Posts: 1
Hi,

I need to use the reverse engineering over an existing database (where I can not do changes). All tables have history/release/version fields (which also define the primary key for each table); and there are no Foreign Keys defined between tables.

I would need to exclude some columns from the primary key (the history/version fields and also not to have them into my foreign key).

Here I tried to illustrate a basic sample with two tables: PARENT(1) - (*)CHILD

My hibernate reverse engineering XML file contains:
Code:
<table name="CHILD">
   <foreign-key foreign-table="PARENT">
     <column-ref local-column="PARE_FS_ID" foreign-column="FS_ID" />
     <column-ref local-column="FS_RELEASE_TO" foreign-column="FS_RELEASE_TO" />
     <column-ref local-column="FS_VALID_FROM" foreign-column="FS_VALID_FROM" />
     <column-ref local-column="FS_VALID_TO" foreign-column="FS_VALID_TO" />
     <many-to-one property="parent" exclude="true" cascade="save-update" />
     <set property="childs" />
   </foreign-key>
</table>


The generated *.hbm.xml files are:

Parent.hbm.xml:
Code:
<hibernate-mapping>
    <class name="dao.Parent" table="PARENT">
        <composite-id name="id" class="dao.ParentId">
            <key-property name="fsId" type="big_decimal">
                <column name="FS_ID" precision="22" scale="0" />
            </key-property>
            <key-property name="fsReleaseTo" type="big_decimal">
                <column name="FS_RELEASE_TO" precision="22" scale="0" />
            </key-property>
            <key-property name="fsValidFrom" type="big_decimal">
                <column name="FS_VALID_FROM" precision="22" scale="0" />
            </key-property>
            <key-property name="fsValidTo" type="big_decimal">
                <column name="FS_VALID_TO" precision="22" scale="0" />
            </key-property>
        </composite-id>
        <set name="childs" table="CHILD" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="PARE_FS_ID" precision="22" scale="0" />
                <column name="FS_RELEASE_TO" precision="22" scale="0" not-null="true" />
                <column name="FS_VALID_FROM" precision="22" scale="0" />
                <column name="FS_VALID_TO" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="dao.Child" />
        </set>
    </class>
</hibernate-mapping>


ParentId.java:
Code:
public class ParentId implements java.io.Serializable {
    private BigDecimal fsId;
    private BigDecimal fsReleaseTo;
    private BigDecimal fsValidFrom;
    private BigDecimal fsValidTo;
...
}


Child.hbm.xml:
Code:
<hibernate-mapping>
    <class name="dao.Child" table="CHILD">
        <composite-id name="id" class="dao.ChildId">
            <key-property name="fsId" type="big_decimal">
                <column name="FS_ID" precision="22" scale="0" />
            </key-property>
            <key-property name="fsReleaseTo" type="big_decimal">
                <column name="FS_RELEASE_TO" precision="22" scale="0" />
            </key-property>
            <key-property name="fsValidFrom" type="big_decimal">
                <column name="FS_VALID_FROM" precision="22" scale="0" />
            </key-property>
            <key-property name="fsValidTo" type="big_decimal">
                <column name="FS_VALID_TO" precision="22" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="parent" class="dao.Parent" update="false" insert="false" fetch="select">
            <column name="PARE_FS_ID" precision="22" scale="0" />
            <column name="FS_RELEASE_TO" precision="22" scale="0" not-null="true" />
            <column name="FS_VALID_FROM" precision="22" scale="0" />
            <column name="FS_VALID_TO" precision="22" scale="0" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>


ChildId.java:
Code:
public class ChildId implements java.io.Serializable {
    private BigDecimal fsId;
    private BigDecimal fsReleaseTo;
    private BigDecimal fsValidFrom;
    private BigDecimal fsValidTo;
...
}


Basically, I would need to generate the sources without the FS_VALID_FROM and FS_VALID_TO fields into hbm.xml files and without the fsValidFrom and fsValidTo properties into POJOs (composite Id classes). ... I can not just simply remove their definition from the hibernate reverse engineering XML file, b/c in that case I'll face other mapping exceptions

Wanted generated sources:

Parent.hbm.xml:
Code:
<hibernate-mapping>
    <class name="dao.Parent" table="PARENT">
        <composite-id name="id" class="dao.ParentId">
            <key-property name="fsId" type="big_decimal">
                <column name="FS_ID" precision="22" scale="0" />
            </key-property>
            <key-property name="fsReleaseTo" type="big_decimal">
                <column name="FS_RELEASE_TO" precision="22" scale="0" />
            </key-property>
        </composite-id>
        <set name="childs" table="CHILD" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="PARE_FS_ID" precision="22" scale="0" />
                <column name="FS_RELEASE_TO" precision="22" scale="0" not-null="true" />
            </key>
            <one-to-many class="dao.Child" />
        </set>
    </class>
</hibernate-mapping>


ParentId.java:
Code:
public class ParentId implements java.io.Serializable {
    private BigDecimal fsId;
    private BigDecimal fsReleaseTo;
...
}



Child.hbm.xml:
Code:
<hibernate-mapping>
    <class name="dao.Child" table="CHILD">
        <composite-id name="id" class="dao.ChildId">
            <key-property name="fsId" type="big_decimal">
                <column name="FS_ID" precision="22" scale="0" />
            </key-property>
            <key-property name="fsReleaseTo" type="big_decimal">
                <column name="FS_RELEASE_TO" precision="22" scale="0" />
            </key-property>
        </composite-id>
        <many-to-one name="parent" class="dao.Parent" update="false" insert="false" fetch="select">
            <column name="PARE_FS_ID" precision="22" scale="0" />
            <column name="FS_RELEASE_TO" precision="22" scale="0" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>


ChildId.java:
Code:
public class ChildId implements java.io.Serializable {
    private BigDecimal fsId;
    private BigDecimal fsReleaseTo;
...
}


Thanks,
Marius


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.