-->
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: one-to-many to a subclass with discriminator (fk in the sub)
PostPosted: Wed Jul 20, 2005 10:51 am 
Newbie

Joined: Fri May 06, 2005 6:04 pm
Posts: 11
Location: ES - Brazil
Hi,

I tought my problem was the same as in http://forum.hibernate.org/viewtopic.php?t=941990 but it is actually diferent, so I opened a new topic.

In my case, the foreign key is in the subclass (in the other topic it was on the superclass).
I´m using a table per subclass with a discrimanator. I isolated the problem and created a new test case. The model for the test is:

Code:
                      
          C ----------------- B--------------> A
            1               *



As showned , A is a superclass of B, and B has a bidirectional association with C.

When I try to do c.getB() (to get the collection of B´s) the following SQL is executed:

The generated SQL (show_sql=true):
Code:
Hibernate: select b0_.idC as idC1_, b0_.idA as idA1_, b0_.idA as idA0_, b0_.name as name0_0_, b0_1_.idC as idC1_0_ from A b0_ inner join b b0_1_ on b0_.idA=b0_1_.idB where b0_.idC=?


and the following error occur: ERROR: column b0_.idc does not exist

This happens because the sql tryed to acess the column idC from A (b0_ alias) .

I tought that the way I mapped, the sql would try to look for the FK in the subclass.

Mapping documents:

Code:
<hibernate-mapping package ="Teste" >

<class name="A" >
   <id name="idA" >
      <generator class="sequence">
      </generator>   
   </id>
      
   <discriminator column="tipo" not-null="true" type="string">
   </discriminator>   
      
    <property name="name" />       
   
</class>

</hibernate-mapping>


<hibernate-mapping package ="Teste">

<subclass name="B"
     extends="A"
     discriminator-value="B"        
     lazy="true"
     >
      
   <join table="b" fetch="select">
      <key column="idB" ></key>   
      
      <many-to-one name="c"
             column="idC"                 class="C"
             lazy = "true">
      </many-to-one>                                             
   </join>      
</subclass>


<class name="C" >
   <id name="idC" >
      <generator class="sequence">
      </generator>   
   </id>

   <property name="name"></property>
   
   <set name="b"  lazy="true" inverse="true" cascade="all-delete-orphan" >         
      <key column="idC" />   
      <one-to-many class="B" />
   </set>   

</class>   





I looked in the reference, but didn´t find any other parameter to inform that the fk is from the subclass.

Is this supose to happen? Did I miss something?

Thanks in advance
Pedro
PS: Bellow is some other informations


Hibernate version:
3.05

Name and version of the database you are using:
PostgreSQL 8

Full stack trace of any exception that occurs:
Code:
11:29:14,312  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 42703
11:29:14,312 ERROR JDBCExceptionReporter:72 - ERROR: column b0_.idc does not exist
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not initialize a collection: [Teste.C.b#9]
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:59)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.loader.Loader.loadCollection(Loader.java:1441)
   at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:176)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
   at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:222)
   at java.lang.String.valueOf(Unknown Source)
   at java.io.PrintStream.print(Unknown Source)
   at java.io.PrintStream.println(Unknown Source)
   at Teste.B.main(B.java:49)
Caused by: java.sql.SQLException: ERROR: column b0_.idc does not exist
   at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1471)
   at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1256)
   at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:175)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:388)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:329)
   at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:239)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:120)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1272)
   at org.hibernate.loader.Loader.doQuery(Loader.java:391)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
   ... 11 more


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 4:35 pm 
Newbie

Joined: Fri May 06, 2005 6:04 pm
Posts: 11
Location: ES - Brazil
One more information:

looking at the inicialization log, it showed that the collection of B´s in the class C is mapped to the table A insted of table B, even if I inform in the map of C the parameter table="b" :

Code:
17:24:51,203  INFO Configuration:879 - processing collection mappings
17:24:51,203  INFO HbmBinder:2041 - Mapping collection: Teste.C.b -> a


I think if it was mapped to table B, the error wouldn´t occur. Did I miss something in my mapping files?

Thanks again
Pedro


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 12:39 pm 
Newbie

Joined: Thu Feb 12, 2004 6:19 pm
Posts: 10
This is still happening in 3.1 beta 3. Does anyone have a solution? I'm working on a new project that needs a lot of associations to discriminated subclasses like this.

I also tried replacing my <key column="foo_id"/> with a reference to the subclass property holding the other end of the association (<key property-ref="foo">) but got a "property not found" for my trouble. Either it's only looking in the superclass for property-ref matches, or property-ref isn't paying attention to properties specified in a <join>.

Thanks!


Top
 Profile  
 
 Post subject: Found anwer ??
PostPosted: Mon Oct 24, 2005 11:07 am 
Newbie

Joined: Wed Oct 19, 2005 3:45 pm
Posts: 14
Hi you guys..
Did you find an anwer for that issue..
I´m having the same problem in my project!!

Thanks..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 11:55 am 
Newbie

Joined: Thu Feb 12, 2004 6:19 pm
Posts: 10
I filed bug 1015 on this; doesn't look like it'll be fixed any time soon. Luckly I was able to change my object model to use composition rather than inheritance to get around the problem.


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.