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.  [ 12 posts ] 
Author Message
 Post subject: one-to-many with different IDs
PostPosted: Fri Jul 15, 2005 10:50 pm 
Newbie

Joined: Fri Jul 15, 2005 10:24 pm
Posts: 6
Location: Omaha, NE
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5
Name and version of the database you are using: Oracle 8.1.0.5

I’m writing a J2EE application with a legacy Oracle database design.

I have three tables that all have a one-to-many connection to a properties table. The properties table has a prop_type to tell what kind of property it is.
Code:
--------        -----------     -----------
|Job   |        |Prop     |     |Number   |
|Job_id|------->|Prop_id  |<----|Number_id|
|...   |    |   |Prop_type|     |...      |
--------    |   |...      |     -----------
            |   -----------
---------   |
|Call   |   |
|Call_id|----
|...    |
---------
I need a set in the Job, Number and Call classes that link with the follow conditions.

Job_id = Prop_id and Prop_type=’J’
Number_id = prop_id and prop_type = ‘N’
Call_id = prop_id amd prop_type=’C’

1. How do I write the map entry with different column names?
2. How do I limit the set to include the right property type?

Thanks
James Hayes


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 11:21 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
This seems very much like an inheritance relationship, where Job and Call would all be derivatives of Property. If you can make that distinction, what you have here is basically a table-per-class hierarchy (or more accurately, a table-per-subclass using a descriminator).

The different names for the ID columns may give you a little trouble, though... not sure.

Take a look at this:
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#inheritance-tablepersubclass-discriminator


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 11:28 pm 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
Oops... spoke too soon. This isn't really what you have here, I don't think.

Look at http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#collections-mapping, particularly the "where" attribute for map. You can use this attribute to specify a where clause constraint to apply to the join.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 7:11 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
I just had the same issue here:

http://forum.hibernate.org/viewtopic.php?t=945130

Make a one-to-one relationship with a function for each column of the "composite-id" of the Prop table, because your id can be understood as composite

Prop_id + Prop_type.

code eample :

Code:
<!-- This one-to-one relationship use 2 formula for each composite key of AddressBook.  It mean that the first composite key (AddressType) == 'S' and the second (AddressKey) equal the branchCode column of the Branch Table -->
    <one-to-one name="addressBook"
       class="AddressBook">
        <formula>'S'</formula>
        <formula>branchCode</formula>
    </one-to-one>

hope it helped... rate it.

Etienne


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 17, 2005 12:26 am 
Senior
Senior

Joined: Tue Jun 21, 2005 10:18 am
Posts: 135
Location: South Carolina, USA
I'm a bit confused about what your solution does, etienne, although I agree that the id could be seen as a compound of prop_id + prop_type. Could you post an example mapping for, say, Job?


Top
 Profile  
 
 Post subject: syntax error
PostPosted: Mon Jul 18, 2005 9:56 am 
Newbie

Joined: Fri Jul 15, 2005 10:24 pm
Posts: 6
Location: Omaha, NE
When I try the following I get an XML parsing error.

Code:
<set name="properties" >
   <key column="JOB_ID"/>
   <one-to-many class="Prop">
      <formula>PROP_TYPE_CODE='J'</formula>
   </one-to-many>
</set>



Code:
     [java] 08:51:37,944  INFO Configuration.addResource:444 - Mapping resource: Job.hbm.xml
     [java] 08:51:37,960 DEBUG DTDEntityResolver.resolveEntity:42 - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd in classpath under org/hibernate/
     [java] 08:51:37,960 DEBUG DTDEntityResolver.resolveEntity:53 - found http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd in classpath
     [java] 08:51:38,007 ERROR XMLHelper.error:59 - Error parsing XML: XML InputStream(40) The content of element type "one-to-many" must match "EMPTY".
     [java] 08:51:38,023 ERROR db.<clinit>:16 - Initial SessionFactory creation failed.org.hibernate.MappingException: Error reading resource: Job.hbm.xml
     [java] java.lang.ExceptionInInitializerError
     [java]     at HibernateUtil.<clinit>(HibernateUtil.java:17)
     [java]     at Main.listJobs(Main.java:79)
     [java]     at Main.main(Main.java:19)
     [java] Caused by: org.hibernate.MappingException: Error reading resource: Job.hbm.xml
     [java]     at org.hibernate.cfg.Configuration.addResource(Configuration.java:452)
     [java]     at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1263)
     [java]     at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1235)
     [java]     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1217)
     [java]     at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1184)
     [java]     at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
     [java]     at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
     [java]     at HibernateUtil.<clinit>(HibernateUtil.java:13)
     [java]     ... 2 more
     [java] Caused by: org.hibernate.MappingException: invalid mapping
     [java]     at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:399)
     [java]     at org.hibernate.cfg.Configuration.addResource(Configuration.java:449)
     [java]     ... 9 more
     [java] Caused by: org.xml.sax.SAXParseException: The content of element type "one-to-many" must match "EMPTY".
     [java]     at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
     [java]     at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
     [java]     at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
     [java]     at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
     [java]     at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
     [java]     at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
     [java]     at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
     [java]     at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
     [java]     at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
     [java]     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
     [java]     at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
     [java]     at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
     [java]     at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
     [java]     at org.dom4j.io.SAXReader.read(SAXReader.java:465)
     [java]     at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:398)
     [java]     ... 10 more
     [java] Exception in thread "main"

     [java] Java Result: 1



Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 11:17 am 
Newbie

Joined: Fri Jul 15, 2005 10:24 pm
Posts: 6
Location: Omaha, NE
OK, never mind that last one. I'm trying something like this, but I'm still not having any luck.

Code:
<list name="properties" >
   <key column="PROP_ID" foreign-key="JOB_ID"/>
   <list-index column="PROP_NAME"/>
   <one-to-many class="Prop"/>
</list>


James.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 11:29 am 
Newbie

Joined: Fri Jul 15, 2005 10:24 pm
Posts: 6
Location: Omaha, NE
When I run that it doesn't find any results, but it should. Here are the log lines and the same query in SQLWorkbench.

Code:
     [java] Hibernate: select properties0_.PROP_ID as PROP1_1_, properties0_.PROP_NAME as PROP3_1_, properties0_.PROP_ID as PROP1_0_, properties0_.PROP_TYPE_CODE as PROP2_8_0_, properties0_.PROP_NAME as PROP3_8_0_, properties0_.PROP_VALU as PROP4_8_0_ from CCI_PROP properties0_ where properties0_.PROP_ID=?
     [java] 10:23:44,416 DEBUG LongType.nullSafeSet:59 - binding '7' to parameter: 1


column PROP1_1_ format 0
column PROP3_1_ format a10
column PROP1_0_ format 0
column PROP2_8_0_ format a10
column PROP3_8_0_ format a10
column PROP4_8_0_ format a10

select properties0_.PROP_ID as PROP1_1_,
       properties0_.PROP_NAME as PROP3_1_,
       properties0_.PROP_ID as PROP1_0_,
       properties0_.PROP_TYPE_CODE as PROP2_8_0_,
       properties0_.PROP_NAME as PROP3_8_0_,
       properties0_.PROP_VALU as PROP4_8_0_
from CCI_PROP properties0_
where properties0_.PROP_ID=7;


PROP1_1_ PROP3_1_   PROP1_0_ PROP2_8_0_ PROP3_8_0_ PROP4_8_0_
-------- ---------- -------- ---------- ---------- ----------
       7 SSN               7 J          SSN        123456789

1 row selected.

Any ideas on what I'm doing worng?

James


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 11:30 am 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
hi,

You have to make a composite ID for Prop.


<composite-id name="id" class="PropId">
<key-property name="Prop_id">
<column name="Prop_id" scale="3" precision="0"
not-null="true" sql-type="char" />
</key-property>

<key-property name="prod_type">
<column name="prop_type" not-null="true" sql-type="char" />
</key-property>
</composite-id>


and make your one to relationship on the other class like this:

<one-to-one name="prop"
class="Prop">
<formula>prop_id</formula> (for the first id of composite)
<formula>'J'</formula> (for the 2nd id of composite)
</one-to-one>

did it helped?

Etienne


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 12:23 pm 
Senior
Senior

Joined: Tue Jan 11, 2005 5:03 pm
Posts: 137
Location: Montreal, Quebec
you have also the descriminator strategie:

At Hibernate doc 3.1 (should be there for 3.0 also):

Quote:
6.1.6. discriminator
The <discriminator> element is required for polymorphic persistence using the table-per-class-hierarchy mapping
strategy and declares a discriminator column of the table. The discriminator column contains marker values
that tell the persistence layer what subclass to instantiate for a particular row. A restricted set of types may
be used: string, character, integer, byte, short, boolean, yes_no, true_false.
<discriminator
column="discriminator_column" (1)
type="discriminator_type" (2)
force="true|false" (3)
insert="true|false" (4)
formula="arbitrary sql expression" (5)
/>
(1) column (optional - defaults to class) the name of the discriminator column.
(2) type (optional - defaults to string) a name that indicates the Hibernate type
(3) force (optional - defaults to false) "force" Hibernate to specify allowed discriminator values even when
retrieving all instances of the root class.
(4) insert (optional - defaults to true) set this to false if your discriminator column is also part of a mapped
composite identifier. (Tells Hibernate to not include the column in SQL INSERTs.)
(5) formula (optional) an arbitrary SQL expression that is executed when a type has to be evaluated. Allows
content-based discrimination.
Actual values of the discriminator column are specified by the discriminator-value attribute of the <class>
and <subclass> elements.
The force attribute is (only) useful if the table contains rows with "extra" discriminator values that are not
mapped to a persistent class. This will not usually be the case.
Using the formula attribute you can declare an arbitrary SQL expression that will be used to evaluate the type
of a row:
<discriminator
formula="case when CLASS_TYPE in ('a', 'b', 'c') then 0 else 1 end"
type="integer"/>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 12:50 pm 
Newbie

Joined: Fri Jul 15, 2005 10:24 pm
Posts: 6
Location: Omaha, NE
I saw the discriminator element. But, at this point I'm just trying to tie the two tables together using different ID names with a one-to-many relationship. The one-to-many element doesn't allow for nested formulas like a one-to-one does.

Any Ideas on how to line a job.job_id |--> prop.prop_id ; with one job to 0..n properties?

Thanks!
James


Top
 Profile  
 
 Post subject: Answer
PostPosted: Tue Jul 19, 2005 3:19 pm 
Newbie

Joined: Fri Jul 15, 2005 10:24 pm
Posts: 6
Location: Omaha, NE
Well, first exit or reconnect SQL*Plus Worksheet to see your changes :-)

The following mapping in the Job Class will make the Set I was looking for.

Code:
<set name="properties" where="PROP_TYPE_CODE='J'">
   <key column="PROP_ID" />
   <one-to-many class="Prop"/>
</set>


I still don't know how it maps the job_id = prop_id, but it works.

Thanks
James


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

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.