-->
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.  [ 3 posts ] 
Author Message
 Post subject: OneToOne composite key and org.hibernate.TypeMismatchEx
PostPosted: Fri Jan 16, 2009 8:16 pm 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
I get this failure by mapping a OneToOne-Relationship:

Code:
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: myEntities.Product$ProductID, got class myEntities.Item$ItemID
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)


The ProductID of table Product is a composite key which has the SAME Columns than the composite key ItemID in table Item:

Code:
public class Product implements Serializable {
   
public static class ProductID implements Serializable
{
@Column(name = "ID_A", nullable = false, columnDefinition = "integer")
private Integer idA;

@Column(name = "ID_B", nullable = false, columnDefinition = "integer")
private Integer idB;
}

@EmbeddedId
private ProductID productID = new DocumentID();
getter/setter


@OneToOne(fetch = FetchType.EAGER, optional = true)
@JoinColumns({
@JoinColumn(name="ID_A", referencedColumnName="ID_A", nullable = false, insertable = false, updatable = false),   
       @JoinColumn(name="ID_B", referencedColumnName="ID_B", nullable = false, insertable = false, updatable = false) })
private Item item;
//getter/setter

}



Code:
public class Item implements Serializable {
   
public static class ItemID implements Serializable
{
@Column(name = "ID_A", nullable = false, columnDefinition = "integer")
private Integer idA;

@Column(name = "ID_B", nullable = false, columnDefinition = "integer")
private Integer idB;
}

@EmbeddedId
private ItemID itemID = new ItemID();
getter/setter


@OneToOne(fetch = FetchType.EAGER, optional = true, mappedBy="item")
private Product product;
//getter/setter


}



This HQL works and returns the item-instance of a product:

Code:
select a.item from Product a


This HQL does not work and returns the exception:
Code:
select a from Product a


Code:
org.hibernate.TypeMismatchException: Provided id of the wrong type. Expected: myEntities.Product$ProductID, got class myEntities.Item$ItemID
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:86)





Instead of JoinColumn, I have tried it with PrimaryKeyJoinColumns (the same exception occurs):

Code:
@OneToOne(fetch = FetchType.EAGER, optional = true)
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name="ID_A", referencedColumnName="ID_A"),   
@PrimaryKeyJoinColumn(name="ID_B", referencedColumnName="ID_B", nullable = false, insertable = false, updatable = false) })
private Item item;


(Another point is, when I annotate the @OneToOne above the getter instead above the field, then Hibernate totally ignores my OneToOne-Mapping and recognices the Item-Instance (or Product-Instance) as a column instead of a relationship, hence it produces false sql-selects)

What is wrong with my mapping?


Top
 Profile  
 
 Post subject: too hard to annotate ?
PostPosted: Sat Jan 17, 2009 6:54 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
It is really hard to believe that such a OneToOne-Mapping with a composite key is so hard to annotate by using ORM (Hibernate).


Such relationships are very common.

Can anyone provide me with help?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 7:48 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
I have tried it also with SecondaryTable-Annotation, but it is not possible to declare a composite key in the @AttributeOverrides-Annotation.

Any suggestions how to solve such a problem with OneToOne-Annotation?


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