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 existThis 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