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.  [ 5 posts ] 
Author Message
 Post subject: child with two parents
PostPosted: Tue Jul 18, 2006 11:48 pm 
Newbie

Joined: Mon Jan 23, 2006 8:26 am
Posts: 6
how can i re-use a child definition for two class. i mean two class will have the same child (one-to-many) relationship to i.e. OR.

which i think the problem was hibernate created two constraints(hibernate-tools RunSchema) based on my mapping definition. when we manually removed the constraints the application just run fine.

so my question is how to do child with two parents?

thanks

Hibernate version:
Hibernate 3.1.3

Mapping documents:
<class name="billing.collections.GeneralCollections" table="gc">
<list name="orVat" table="or" lazy="false"
cascade="all" where="type=1 AND form=0 AND flag=1">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>

<list name="orNonVat" table="or" lazy="false"
cascade="all" where="type=0 AND form=0 AND flag=1">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>

<list name="arVat" table="or" lazy="false"
cascade="all" where="type=1 AND form=1 AND flag=1">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>

<list name="arNonVat" table="or" lazy="false"
cascade="all" where="type=0 AND form=1 AND flag=1">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>
</class>

<class name="com.payment.CashierPayment" table="billing_cashier">
<list name="orVat" table="or" lazy="false"
cascade="all" where="type=1 AND form=0 AND flag=0">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>

<list name="orNonVat" table="or" lazy="false"
cascade="all" where="type=0 AND form=0 AND flag=0">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>

<list name="arVat" table="or" lazy="false"
cascade="all" where="type=1 AND form=1 AND flag=0">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>

<list name="arNonVat" table="or" lazy="false"
cascade="all" where="type=0 AND form=1 AND flag=0">
<key column="ref"/>
<index column="lpos" />
<one-to-many class="com.payment.OR" entity-name='or'/>
</list>

<list name="cashierPaymentDtls" table="cashier_payment_dtls" lazy="false" cascade="all">
<key column="cashierPaymentId"/>
<index column="lpos" />
<one-to-many class="com.payment.CashierPaymentDtls"/>
</list>

</class>

<class name="com.payment.OR" table="or" entity-name='or'>
<id name="id" type="string"><generator class="assigned"/></id>
<property name="type" type="string"/>
<property name="orNumber" type="string"/>
<property name="status" type="string"/>
<property name="refOR" type="string"/>
<property name="form" type="string"/>
<property name="flag" type="string"/>
<property name="date" type="string"/>
<property name="bank" type="string"/>
<property name="branch" type="string"/>
<property name="accountType" type="string"/>
<list name="entries" table="or_entry" lazy="false"
cascade="all">
<key column="orId"/>
<index column="lpos" />
<one-to-many class="com.payment.OREntry" entity-name='or_entry'/>
</list>
<list name="checks" table="or_check" lazy="false" cascade="all">
<key column="or_id"/>
<index column="lpos"/>
<many-to-many class="com.billing2.Check" not-found="ignore" column="check_id"/>
</list>
</class>

...
Code between sessionFactory.openSession() and session.close():
using getHibernateTemplate().merge()

Full stack trace of any exception that occurs:
Hibernate operation: Could not execute JDBC batch update; SQL [update ors set ref=?, lpos=? where id=?]; Cannot add or update a child row: a foreign key constraint fails (`billing2/ors`, CONSTRAINT `FK1AEF059D4730C` FOREIGN KEY (`ref`) REFERENCES `gc` (`id`)); nested exception is java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`billing2/ors`, CONSTRAINT `FK1AEF059D4730C` FOREIGN KEY (`ref`) REFERENCES `gc` (`id`))

Name and version of the database you are using:
5.0.22-community-nt

The generated SQL (show_sql=true):
5.0.22-community-nt

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 1:05 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That code is wrong. With a capital wrong. (WRONG)

Here's a few pointers:
  1. The table attribute in a collection element (list, in your mappings) refers to a join table. But you've specified the "or" table as both the join table and the entity table. That can never be right.
  2. You're using the same key and index in each list. That's even wronger (sic).
  3. You are referring to the OR class via both entity-name="" and class="". That might be legal, but it's redundant (and superfluous).
  4. You have a polymorphic table that you haven't implemented polymorphically.
Map the OR class as a polymorphic class, using the table per class hierarchy structure (ref docs sectoin 9.1.1), where the discriminator is type,form,flag.

If you really want a list, index = lpos, shared across all OR subclasses, you must use a single <list> in GeneralCollections. You can add getters in your GeneralCollections class that returns sublists of the superclass list, if you feel that having a separate getting per type is a good idea.

If you must have separate properties for each list of ORs, then you must have a separate index column for each one. Or else map each list to a different table.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 4:47 am 
Newbie

Joined: Mon Jan 23, 2006 8:26 am
Posts: 6
ok i get the part of using table per class heirarchy. thanks

but the ors must be in one table for we have some reports that will query all the ors regardless if its from the two objects namely GeneralCollection & CashieringPayment.

how can you make it (the "OR"/"or") a child of the objects?

thanks a lot.


Top
 Profile  
 
 Post subject: hibernate tools problem
PostPosted: Wed Jul 19, 2006 7:27 am 
Newbie

Joined: Mon Jan 23, 2006 8:26 am
Posts: 6
why can't hibernate tools load this hbm file

<class name="com.payment.OR" table="ors" polymorphism="implicit">
<id name="id" type="string"><generator class="assigned"/></id>
<property name="type" type="string"/>
<property name="orNumber" type="string"/>
<property name="status" type="string"/>
<property name="refOR" type="string"/>
<property name="form" type="string"/>
<property name="flag" type="string"/>
<property name="date" type="string"/>
<property name="bank" type="string"/>
<property name="branch" type="string"/>
<property name="accountType" type="string"/>

<discriminator>
<column name="DISCRIMINATOR"/>
</discriminator>



<list name="entries" table="or_entry" lazy="false"
cascade="all">
<key column="orId"/>
<index column="lpos" />
<one-to-many class="com.payment.OREntry" entity-name='or_entry'/>
</list>
<list name="checks" table="or_check" lazy="false" cascade="all">
<key column="or_id"/>
<index column="lpos"/>
<many-to-many class="com.billing2.Check" not-found="ignore" column="check_id"/>
</list>

<subclass name="com.payment.ORVat" discriminator-value="ORVat"/>
<subclass name="com.payment.ORNonVat" discriminator-value="ORNonVat"/>
<subclass name="com.payment.ARVat" discriminator-value="ARVat"/>
<subclass name="com.payment.ARNonVat" discriminator-value="ARNonVat"/>

</class>


ERROR Worker-133 org.hibernate.util.XMLHelper - Error parsing XML: XML InputStream(LINE NUMBER OF closing tag i.e. </class>) The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 6:33 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
According to the error message, discriminator must be before all properties, etc.

It sounds to me like you need two class hierarchies: one for GeneralCollections/CashieringPayments, and one for all those OR classes. Put the list of ORs into the base class of GeneralCollections/CashieringPayments, that way both of those classes get the same list. And because the entities in the list are all subclasses of your OR base class, you have access to everything.

Assuming that you want to use a single lpos for all OR entities, you cannot separate out the various types to different mapped lists. You can create lists of the various OR subclasses in your GeneralColelctions (or its base class) that are backed by the full OR list, but those lists are not in your mapping.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.