-->
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.  [ 9 posts ] 
Author Message
 Post subject: Mapping of Composite Keys
PostPosted: Thu Aug 25, 2011 2:41 pm 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
Mapping of Composite Keys

I have two tables with the below definition

CREATE TABLE VYA6CPP (
A6VN9K VARCHAR(9),
A6VN8K VARCHAR(8),
A6J2CD VARCHAR(20),
PRIMARY KEY(A6VN9K, A6VN8K)
)

CREATE TABLE MAMAM1 (
M1VIN1 VARCHAR(9),
M1VIN2 VARCHAR(8),
M1SHPR VARCHAR(4),
PRIMARY KEY(M1VIN1, M1VIN2),
FOREIGN KEY(M1VIN1, M1VIN2) REFERENCES VYA6CPP(A6VN9K, A6VN8K)
)

Below are the entity definition.

Code:
@Entity
@Table(name="MAMAM1")
public class Mamam1 implements Serializable {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   private Mamam1PK id;

   @Column(name="M1SHPR")
   private String m1shpr;

   //bi-directional one-to-one association to Vya6cpp
   @OneToOne
   @JoinColumns({
      @JoinColumn(name="M1VIN1", referencedColumnName="A6VN9K"),
      @JoinColumn(name="M1VIN2", referencedColumnName="A6VN8K"),
      })
   private Vya6cpp vya6cpp;
}

Code:
@Embeddable
public class Mamam1PK implements Serializable {
   //default serial version id, required for serializable classes.
   private static final long serialVersionUID = 1L;

   @Column(name="M1VIN1")
   private String m1vin1;

   @Column(name="M1VIN2")
   private String m1vin2;
}


Code:
@Entity
@Table(name="VYA6CPP")
public class Vya6cpp implements Serializable {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   private Vya6cppPK id;

   @Column(name="A6J2CD")
   private String a6j2cd;

   //bi-directional one-to-one association to Mamam1
   @OneToOne(mappedBy="vya6cpp")
   private Mamam1 mamam1;
}


Code:
@Embeddable
public class Vya6cppPK implements Serializable {
   //default serial version id, required for serializable classes.
   private static final long serialVersionUID = 1L;

   @Column(name="A6VN9K")
   private String a6vn9k;

   @Column(name="A6VN8K")
   private String a6vn8k;
}


If the query <Select c from Mamam1 c> is executed, i get the below exception.

Hibernate: select mamam1x0_.M1VIN1 as M1_0_, mamam1x0_.M1VIN2 as M2_0_, mamam1x0_.M1SHPR as M3_0_ from MAMAM1 mamam1x0_ fetch first 10 rows only
java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: Provided id of the wrong type for class Vya6cpp. Expected: class Vya6cppPK, got class Mamam1PK
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:247)
at Main.main(Main.java:26)
Caused by: org.hibernate.TypeMismatchException: Provided id of the wrong type for class Vya6cpp. Expected: class Vya6cppPK, got class Mamam1PK
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:135)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1080)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1028)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:623)
at org.hibernate.type.EntityType.resolve(EntityType.java:431)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:140)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:898)
at org.hibernate.loader.Loader.doQuery(Loader.java:773)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
at org.hibernate.loader.Loader.list(Loader.java:2187)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
... 1 more


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Fri Sep 02, 2011 1:00 pm 
Newbie

Joined: Fri Sep 02, 2011 12:55 pm
Posts: 4
I'm facing a similar issue. I generated all of my classes using Hibernate tools and am using mapping files instead of annotations. In my scenario I have a composite ids for different classes that are made from the exact same table columns. Therefore the ids are the same when the load occurs.

Question: are the string values in both composite keys the same when you get this load error?


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Fri Sep 02, 2011 2:31 pm 
Newbie

Joined: Fri Sep 02, 2011 12:55 pm
Posts: 4
Not sure if this will help you, but I fixed my problem using the same composite id class for each one-to-one. Since they are all using the same table and columns, it makes sense. The generator missed it.

Good luck.


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Sat Sep 03, 2011 8:43 am 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
rashdown wrote:
I'm facing a similar issue. I generated all of my classes using Hibernate tools and am using mapping files instead of annotations. In my scenario I have a composite ids for different classes that are made from the exact same table columns. Therefore the ids are the same when the load occurs.

Question: are the string values in both composite keys the same when you get this load error?

Sorry for the late reply. The entities were generated using Hibernate tools in eclipse. I tried running the mentioned query and i am still getting the error. I beleive in your case, the columns names of the composite keys were the same in both the tables. Regarding your question, I am not able to get it? To re-iterate my point, the columns names of the composite keys are different in both the tables.


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Tue Sep 06, 2011 12:48 pm 
Newbie

Joined: Fri Sep 02, 2011 12:55 pm
Posts: 4
That's a similar table structure to what I have. I don't have an explanation for it other than the 2 composite keys technically represent the same key (both tables share the same primary key)... so maybe Hibernate is tracking the keys by value and is finding a different class, but expecting the same class?

Anyway, I'm pretty sure if you change your code to use the class "Mamam1PK" as the identifier in your "Vya6cpp" class, you'll find things working. I updated my mapping and class files to have both classes use the same component id class and that resolved my problem.

It would be nice to get an official explanation for the issue though, in particular to understand if this is a bug or expected behavior. Did you log an incident on it yet?


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Tue Sep 06, 2011 2:23 pm 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
rashdown wrote:
Anyway, I'm pretty sure if you change your code to use the class "Mamam1PK" as the identifier in your "Vya6cpp" class, you'll find things working. I updated my mapping and class files to have both classes use the same component id class and that resolved my problem.
It would be nice to get an official explanation for the issue though, in particular to understand if this is a bug or expected behavior. Did you log an incident on it yet?

If i have to use the same key class Mamam1PK in Vya6cpp, then it means that i have to change the columns names in the table VYA6CPP. But i need the mapping to work without changing the column names. Thanks for ur valuable comments and I will try to log an issue in JIRA.


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Thu Sep 08, 2011 2:11 am 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
I have raised an bug in JIRA https://hibernate.onjira.com/browse/HHH-6638. I have attached the test case and the table creation scripts, but i am not sure if it will get noticed by the official hibernate team.


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Sat Oct 01, 2011 8:57 am 
Newbie

Joined: Sat Oct 01, 2011 8:21 am
Posts: 2
I have been experience the exactly same problem... I make a query using HQL:

Quote:
Query query1 = session.createQuery("from Sessions where sessionName = :sessionNameResult");
query1.setString("sessionNameResult", sessionNameReq);
List queryResults = query1.list();

I used another mean of querying, namely:

Quote:
List sessionTest = session.createCriteria(Sessions.class).add(Restrictions.eq("sessionName",sessionNameReq)).list();
Sessions test = (Sessions) sessionTest.iterator().next();

But in both got the error mentioned (org.hibernate.TypeMismatchException: Provided id of the wrong type for class).

After some experiments, I ended up using "raw" SQL query:

Quote:
Query query1 = session.createSQLQuery("select * from Sessions where sessionName = :sessionNameResult");
query1.setString("sessionNameResult", sessionNameReq);
List queryResults = query1.list();

and it worked as I wanted... i.e., I get the results I expect... however, you have to handle the results differently, cannot directly cast it to an hibernate object (obtained from the reverse engineering)... I had to work with the "Object", which basically contains the columns of the DB table. See http://wiki.webratio.com/index.php/Quer ... _Hibernate for a good explanation on querying with HQL and SQL...

...any ways, it would be nice to get it working with hibernate functionality (and objects)...


Top
 Profile  
 
 Post subject: Re: Mapping of Composite Keys
PostPosted: Sat Oct 01, 2011 9:05 am 
Newbie

Joined: Thu Jun 23, 2011 5:08 am
Posts: 9
emgsilva wrote:
After some experiments, I ended up using "raw" SQL query:

Quote:
Query query1 = session.createSQLQuery("select * from Sessions where sessionName = :sessionNameResult");
query1.setString("sessionNameResult", sessionNameReq);
List queryResults = query1.list();

and it worked as I wanted... i.e., I get the results I expect... however, you have to handle the results differently, cannot directly cast it to an hibernate object (obtained from the reverse engineering)... I had to work with the "Object", which basically contains the columns of the DB table. See http://wiki.webratio.com/index.php/Quer ... _Hibernate for a good explanation on querying with HQL and SQL...

...any ways, it would be nice to get it working with hibernate functionality (and objects)...


If native queries are used, it will return the List of object arrays which is kinda redundant and totally defeats the purpose and advantages of ORM, and a wastage of the Entity classes created. Thanks for your reply man and I have not received any kind of response to the JIRA bug raised. Is there any way for it to get noticed..?


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