-->
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: Parent/Child Relationships
PostPosted: Wed Jan 14, 2004 11:41 am 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
Hello:

I 'm still working with the lists,now bags ,but i have a problem with the Parent/Child Relationships.

This is the problem:

<joined-subclass name="Productos.Producto" table="T_Producto">
<key column="P_IdProducto"/>
<many-to-one name="organizacion" class="Common.Organizacion" column="F_IdOrganizacion"/>

<bag name="suplementos" lazy="false" cascade="all" inverse="true">
<key column="P_IdProducto"/>
<one-to-many class="Productos.Suplemento"/>
</bag>

</joined-subclass>
<joined-subclass name="Productos.Suplemento" table="T_Suplemento">
<key column="P_IdSuplemento"/>
<many-to-one name="producto" class="Productos.Producto" column="P_IdProducto"/>
</joined-subclass>

and the application throws the followong exception
-----------
SEVERE: could not initialize collection: [Productos.Producto.suplementos#2]
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Invalid column name 'P_IdProducto'.
----------
The column is the primary key of producto.

What im doing wrong?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 11:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Enable Hibernate SQL logging and check the generated SQL, then validate your mapping.


Top
 Profile  
 
 Post subject: check
PostPosted: Wed Jan 14, 2004 12:00 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
ok,i ve already checked to true the logging and the following query were shown:(I only write the last that is the erroneous one)

Hibernate: select suplemento0_.P_IdSuplemento as P_IdSupl1___, suplemento0_.P_IdProducto as P_IdProd2___, suplemento0_.P_IdSuplemento as P_IdElem1_0_, suplemento0__1_.A_Nombre as A_Nombre11_0_, suplemento0__1_.A_Habilitado as A_Habili3_11_0_, suplemento0__1_.A_Orden as A_Orden11_0_, suplemento0__1_.A_BajaLogica as A_BajaLo5_11_0_ from T_Suplemento suplemento0_ inner join T_ElementoEditorial suplemento0__1_ on suplemento0_.P_IdSuplemento=suplemento0__1_.P_IdElementoEditorial where suplemento0_.P_IdProducto=?

obviously .P_IdProducto is not a field of suplemento but of producto,what i m doing wrong in the maps.

Thanks for your fast answer.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Take a look at http://www.xylax.net/hibernate/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 12:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In many-to-one, you're refering your object Id as a FK column, this is not allowed.
A one-to-one is suited for that.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: WrongClassException
PostPosted: Wed Jan 14, 2004 1:07 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
following the reference and your answers,i reach a point where the following exception is thrown:

net.sf.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: Productos.Suplemento (loaded object was of wrong class).

the final xml is this:

<joined-subclass name="Productos.Producto" table="T_Producto">
<key column="P_IdProducto"/>
<many-to-one name="organizacion" class="Common.Organizacion" column="F_IdOrganizacion"/>
<bag name="suplementos" lazy="false" cascade="all" inverse="true">
<key column="F_IdProducto"/>
<one-to-many class="Productos.Suplemento"/>
</bag>

</joined-subclass>
<joined-subclass name="Productos.Suplemento" table="T_Suplemento">
<key column="P_IdSuplemento"/>
<many-to-one name="producto" class="Productos.Producto" column="F_IdProducto"/>
</joined-subclass>


The java code is the following:

List losProductos = GuiSGP.session.find("from Producto where F_IdOrganizacion =" + idOrganizacion
+ " and A_Habilitado =" + 1 + " and A_BajaLogica =" + 0);

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 1:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
One of the suplementos elements is refering a class that is not of suplemento class. This isn't expected
Code:
<bag name="suplementos" lazy="false" cascade="all" inverse="true">
  <key column="F_IdProducto"/>
  <one-to-many class="Productos.Suplemento"/>
</bag>

_________________
Emmanuel


Top
 Profile  
 
 Post subject: can be ralationated with bags
PostPosted: Wed Jan 14, 2004 1:36 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
I 'm inspecting the code and i cant find any "reference to a class that is not of suplemento class".

Could it be a problem with bags?.
Should i change the code and maps and set the collections as sets?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 1:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'm pretty sure of me, there is a non supplemento class referenced in F_IdProducto column in your DB.
What is the type of id 1 ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 1:53 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
i dont understand :
"I'm pretty sure of me, there is a non supplemento class referenced in F_IdProducto column in your DB.
What is the type of id 1 ?"

F_idProducto references a Producto not a suplemento.
How can i know what type is id1 ?.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 2:22 pm 
Beginner
Beginner

Joined: Fri Nov 28, 2003 11:21 am
Posts: 49
Location: Buenos Aires, Argentina
Ok,apparently the reason was in the db, i delete all the records from suplemento and create only a new one and it works.
I mean the maps were ok ,but i have to correct a bit the java code.
Anyway i would like that someone answer me the last question,thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2004 2:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Jean Cartier wrote:
F_idProducto references a Producto not a suplemento.
How can i know what type is id1 ?.

Code:
<bag name="suplementos" lazy="false" cascade="all" inverse="true">
<key column="F_IdProducto"/>
<one-to-many class="Productos.Suplemento"/>
</bag>

F_IdProducto is used to find all related Productos.Suplemento?

You know that id = 1 refers to a sub-class if the associated table (the one of the subclass) contains id = 1 in it's key.
In case of broken data, id=1 may be in several subclass tables: Hibernate cannot find the appropriate type.

Have a look at http://www.agiledata.org/essays/mappingObjects.html#MappingInheritance

_________________
Emmanuel


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.