-->
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.  [ 27 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: primary-key, constraint override
PostPosted: Sat May 20, 2006 11:35 am 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
Hibernate version: 3.1.3

Name and version of the database you are using: DB2 v8.1

hibernatetools version: 3.1.0.beta5

Hi, I want to override existing primary keys.
There are three primary-keys, A_PK1_SEQ, A_PK2_SEQ, A_PK3_SEQ, in TABLE_A, and another table, TABLE_B, has four primary-keys, A_PK1_SEQ, A_PK2_SEQ, A_PK3_SEQ, B_PK1_SEQ.
(The A_PK1_SEQ, A_PK2_SEQ, A_PK3_SEQ keys in TABLE_B are also FK from TABLE_A.)

But, I just want to use one PK(A_PK1_SEQ) for TABLE_A, and one PK(B_PK1_SEQ) with FK(A_PK1_SEQ) for TABLE_B.

This is my reverse engineering file:

Code:
<hibernate-reverse-engineering>
   <type-mapping>
      <sql-type jdbc-type="INTEGER" hibernate-type="java.lang.Integer" />
               .....
   </type-mapping>

   <table-filter match-name="TABLE_A" />
   <table-filter match-name="TABLE_B" />

   <table name="TABLE_A">
      <primary-key>
        <generator class="assigned" />
         <key-column name="A_PK1_SEQ" />
      </primary-key>
   </table>   
   <table name="TABLE_B">
      <primary-key>
        <generator class="assigned" />
         <key-column name="B_PK1_SEQ" />
      </primary-key>
      <foreign-key constraint-name="B_A" foreign-table="TABLE_A">
         <column-ref local-column="A_PK1_SEQ" foreign-column="A_PK1_SEQ" />
         <many-to-one property="a" />
         <set property="b" />
      </foreign-key>
   </table>

</hibernate-reverse-engineering>


Generated table_a.hbm.xml:
There are two sets of table_b. One is new with one PK and the other is using existing constraint with three PKs.

Code:
<hibernate-mapping>
   <class name="Table_a" table="TABLE_A">
      <composite-id name="id"
         class="Table_aId">
         <key-property name="aPk1Seq" type="java.lang.Integer">
            <column name="A_PK1_SEQ" />
         </key-property>
         <key-property name="aPk2Seq" type="java.lang.Integer">
            <column name="A_PK2_SEQ"  />
         </key-property>
         <key-property name="aPk3Seq" type="java.lang.Integer">
            <column name="A_PK3_SEQ" />
         </key-property>
      </composite-id>

    <property ....... />

      <set name="bForFkf2dfd3da76fc2df0" inverse="true">
         <key>
            <column name="A_PK1_SEQ" not-null="true" />
            <column name="A_PK2_SEQ" not-null="true" />
            <column name="A_PK3_SEQ" not-null="true" />
         </key>
         <one-to-many class="table_b" />
      </set>
      
      <set name="b" inverse="true">
         <key>
            <column name="A_PK1_SEQ" not-null="true" />
         </key>
         <one-to-many class="table_b" />
      </set>
   </class>
</hibernate-mapping>


Generated table_b.hbm.xml,

Code:
<hibernate-mapping>
    <class name="Table_b" table="TABLE_B">
        <composite-id name="id" class="Table_bId">
            <key-property name="aPk1Seq" type="java.lang.Integer">
            <column name="A_PK1_SEQ" />
         </key-property>
         <key-property name="aPk2Seq" type="string">
            <column name="A_PK2_SEQ" type="java.lang.Integer" />
         </key-property>
         <key-property name="aPk3Seq" type="java.lang.Integer">
            <column name="A_PK3_SEQ" />
         </key-property>
            <key-property name="bPk1Seq" type="java.lang.Integer">
                <column name="B_PK1_SEQ" />
            </key-property>
        </composite-id>
        <many-to-one name="aByFkf2dfd3da76fc2df0" class="Table_a" update="false" insert="false" fetch="select">
            <column name="A_PK1_SEQ" not-null="true" />
            <column name="A_PK2_SEQ" not-null="true" />
            <column name="A_PK3_SEQ" not-null="true" />
        </many-to-one>
        <many-to-one name="a" class="Table_a" update="false" insert="false" fetch="select">
            <column name="A_PK1_SEQ" not-null="true" />
        </many-to-one>
        <property ..... />
    </class>
</hibernate-mapping>



What I want:

table_a.hbm.xml:
Code:
<hibernate-mapping>
   <class name="table_a" table="TABLE_A">
    <id name="aPk1Seq" type="java.lang.Integer">
        <column name="A_PK1_SEQ" />
        <generator class="assigned" />
    </id>

    <property ....... />

      <set name="b" inverse="true">
         <key>
            <column name="A_PK1_SEQ" not-null="true" />
         </key>
         <one-to-many class="table_b" />
      </set>
   </class>
</hibernate-mapping>


table_b.hbm.xml:
Code:
<hibernate-mapping>
    <class name="Table_b" table="TABLE_B">
    <id name="bPk1Seq" type="java.lang.Integer">
        <column name="B_PK1_SEQ" />
        <generator class="assigned" />
    </id>
        <many-to-one name="a" class="Table_a" update="false" insert="false" fetch="select">
            <column name="A_PK1_SEQ" not-null="true" />
        </many-to-one>

        <property ..... />

    </class>
</hibernate-mapping>



Is it possible to get above hbm.xml without changing real DB schema?
Can I override existing constraints?
I would appreciate any comment and hints.
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 20, 2006 7:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the problem is that the old foreign key is still being picked up.

i don't think we have <foreign-key constraint-name="oldname" exclude="true"/> support yet. put it in jira with a link to this posting.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 1:42 pm 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
Hi Max,

How about the primary key? Is it same reason as foreign-key?
Can't Hibernate ignore the existing primary-key?

If then, I don't know if it is a good idea, but it will make easy to override primary key constraint if we have 'exclude' option.

e.g.,
Code:
<primary-key>
    <key-column name="A_PK1_SEQ" exclude=true />
</primary-key>


Thanks Max.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 1:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
there can be only *one* primary-key and hence if you specify one it will be the one that is defined.

And is that not happening ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 2:10 pm 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
I defined only one primary-key for each table. But, there are still old primary keys as a 'composite-id' in the generated hbm.xml files.
Thanks.


Last edited by okid on Mon May 22, 2006 10:54 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 2:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
..then the table, schema or catalog name is not "matching".

do you have default_schema set ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 2:51 pm 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
max wrote:
..then the table, schema or catalog name is not "matching".

do you have default_schema set ?


I didn't use the schema or catalog name on the 'table' element in reveng.xml.
And table name is correct because there is new 'set' and 'many-to-one' in generated hbm.xml file as like defined in 'foreign-key'.

The 'default_schema set' means DDL?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 2:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
is hibernate.default_schema set ? (in hibernate.properties/hibernate.cfg.xml)

if not then the reveng currently uses the schema name automatically.

try to either set default_schema or set the schema on the table element.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 3:03 pm 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
Thank you Max for helping.

I have the default_schema line in the hibernate.cfg.xml.

Code:
<hibernate-configuration>
    <session-factory name="SessionFactory">
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">COM.ibm.db2.jdbc.app.DB2Driver</property>
        <property name="hibernate.connection.password">test</property>
        <property name="hibernate.connection.url">jdbc:db2:test</property>
        <property name="hibernate.connection.username">test</property>
        <property name="hibernate.default_schema">DBLTEST</property>
        <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
    </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 3:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hmm that should just work then.

is anything at all being picked up from the reveng.xml ?

e.g. try and remove or add a tablefilter to see if it affects the output.

any chance you can run this via ant and enable log4j debug logging then I can see why it fails.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 21, 2006 5:37 pm 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
max wrote:
hmm that should just work then.

is anything at all being picked up from the reveng.xml ?

e.g. try and remove or add a tablefilter to see if it affects the output.


I removed the tablefilter and it affected the output.
e.g. got hbm.xml files for every table in DB.

max wrote:
any chance you can run this via ant and enable log4j debug logging then I can see why it fails.


I am trying to run this using ant and get below error if I use the revengfile="${src.dir}/hibernate.reveng.xml" on the ant build.xml.

Code:
org.hibernate.MappingException: Could not configure overrides from file: D:\MyProject\Hibernate\eclipse\workspace\hibernate\src\hibernate.reveng.xml


The part of build.xml:

Code:
   <target name="codeGenerator" depends="init">
      <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />
      <hibernatetool destdir="${build.dir}/generated">
         <classpath>
            <path location="${build.dir}/classes" />
         </classpath>

         <jdbcconfiguration configurationfile="hibernate.cfg.xml" revengfile="${src.dir}/hibernate.reveng.xml" packagename="com.model" />
         <hbm2java />
         <hbm2hbmxml />
         <hbm2cfgxml />
         <hbm2dao />
         <hbm2ddl drop="true" outputfilename="${build.dir}/schema_exported.ddl" />
      </hibernatetool>
   </target>



I will post the log after I get the full log.
Thanks max.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 2:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
run ant -verbose and you should get a stacktrace explaining the error.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 6:55 am 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
max wrote:
run ant -verbose and you should get a stacktrace explaining the error.


Hello max,

I am getting below exception. Tonight I will try again. Thanks.

Code:
java.lang.NoClassDefFoundError: org/w3c/tidy/Tidy
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1225)
   at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
   at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
   at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
   at org.apache.tools.ant.Main.runBuild(Main.java:668)
   at org.apache.tools.ant.Main.startAnt(Main.java:187)
   at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
   at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Caused by: java.lang.NoClassDefFoundError: org/w3c/tidy/Tidy
   at org.hibernate.tool.hbm2x.XMLPrettyPrinter.getDefaultTidy(XMLPrettyPrinter.java:48)
   at org.hibernate.tool.hbm2x.ArtifactCollector.formatXml(ArtifactCollector.java:69)
   at org.hibernate.tool.hbm2x.ArtifactCollector.formatFiles(ArtifactCollector.java:59)
   at org.hibernate.tool.hbm2x.AbstractExporter.start(AbstractExporter.java:96)
   at org.hibernate.tool.ant.ExporterTask.execute(ExporterTask.java:40)
   at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:160)
   at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
   at org.apache.tools.ant.Task.perform(Task.java:364)
   at org.apache.tools.ant.Target.execute(Target.java:341)
   at org.apache.tools.ant.Target.performTasks(Target.java:369)
   at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
   ... 7 more


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 7:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
have you added the tidy jar to your classpath ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 10:32 pm 
Newbie

Joined: Sat May 20, 2006 9:47 am
Posts: 17
max wrote:
have you added the tidy jar to your classpath ?


Please see page 2 for log.


Last edited by okid on Tue May 23, 2006 6:31 am, edited 4 times in total.

Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 27 posts ]  Go to page 1, 2  Next

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.