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.  [ 2 posts ] 
Author Message
 Post subject: Named query includes collection key column when executed
PostPosted: Thu May 15, 2008 9:51 pm 
Newbie

Joined: Tue May 06, 2008 8:20 am
Posts: 3
Hey there,
I m trying to use a named query from an optional mapping file that declares the entities mappings and is referenced in a persistence.xml file. Everything works fine when I use annotations.

However, when using a mapping file, named queries get warped.
The entities are mapped fine. So are the collections. Everything reads fine during deployment. However, a simple 'select e from example e' named query, throws an exception when executed. It turns out that the key column of a collection is included in the query! e.g. 'select e0.EXAMPLE_ID as e0_1, e0.EXAMPLE_DATE as e0_2, e0.EXAMPLE_1 as e0_3, e0.OTHER_ID as e0_4 from EXAMPLE e0'.

This is the mapped entity:
Code:
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
...
<class
   name="example"
   table="EXAMPLE">
   <composite-id
      name="examplePK"
      class="EXAMPLEPK">
      <key-property
            name="exampleId"
            column="EXAMPLE_ID"
            type="integer"/>
      <key-property
            name="exampleDate"
            column="EXAMPLE_DATE"
            type="date"/>
   </composite-id>
   <version
      name="optLock"
      column="OPT_LOCK"/>
   <property
      name="example1"
      type="double"
      column="EXAMPLE_1"
      not-null="true"/>
   <many-to-one
      name="otherCollection"
      column="OTHER_ID"
      class="OTHER"
      not-null="true"/>
   <query
      name="aQuery">
         <![CDATA[
         SELECT e FROM example e
         ]]>
   </query>
</class>
...

And the corresponding "other" entity mapping:

Code:
<class
   name="other"
   table="OTHER">
   <id
      name="otherId"
      column="OTHER_ID"
      type="string">
   </id>
   <bag name="otherCollection" inverse="true" cascade="all">
      <key column="OTHER_ID"/>
      <one-to-many class="example"/>
   </bag>
</class>

When used with annotations (strict EJB ones, no native Hibernate) everything works fine. Ideas anyone? Do note that it is imperative that I use xml mappings instead of annotations, as I want to enable hibernate's second level cache in a separate persistence unit declared in persistence.xml. Not all entities are declared in the xml file, but the ones that are, are fully defined.

Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 7:05 pm 
Newbie

Joined: Tue May 06, 2008 8:20 am
Posts: 3
I have spent a lot of time trying to solve this. It has to do with the way composite id's get mapped. I have RTFM more than enough times. So I 'm going to post the 3 tables I want to get mapped and if anyone cares to help by drawing up the corresponding hibernate xml mappings (ejb annotations work fine), please do. Hibernate is version 3.2.6GA, Hibernate entity manager is 3.3.2GA and AS is GlassFish V2U2.

Table 1
Name table_1
Column name t1_a, type Integer, Primary Key
Column name t1_b, type String


Table 2
Name table_2
Column name t2_a, type Integer, Primary Key, Foreign Key referencing t1_a
Column name t2_b, type Date, Primary Key
Column name t2_c, type String
Column name t2_d, type String, Foreign Key referencing t3_a

Table 3
Name table_3
Column name t3_a, type String, Primary Key

Entity classes are table1, table2 plus table2PK and table3

Table 1 should have a one to many collection of Table 2
Table 3 should have a one to many collection of Table 2 too
Table 2 therefore must have a field for the composite (embedded) key, a field for column t2_c and another one for column t2_d plus the opposite end (many to one) for the collections.

The ejb-annotated (working) entity for table 2 looks like this:

Code:
@Entity
@Table(name = "table_2")
public class Table2 implements Serializable {
   @EmbeddedId
   protected Table2PK table2PK;
   @Column(name = "t2_c", nullable = false)
   private String t2C;
   @Column(name = "t2_d", nullable = false)
   private String t2D;
   @JoinColumn(name = "t2_a", referencedColumnName = "t1_a", insertable = false, updatable = false)
   @ManyToOne
   private Table_1 table1;
   @JoinColumn(name = "t2_d", referencedColumnName = "t3_a", insertable = false, updatable = false)
   @ManyToOne
   private Table_3 table3;
...

@Embeddable
public class Table2PK implements Serializable {
   @Column(name = "t2_a", nullable = false)
   private int t2A;
   @Column(name = "t2_b", nullable = false)
   @Temporal(TemporalType.DATE)
   private Date t2B;
...


I 'm going to get a beer now and watch Lost.


Cheers,
Andreas


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.