-->
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: em.find fails for @OneToOne with FK referencing a non-PK col
PostPosted: Sun Jul 16, 2006 4:28 am 
Newbie

Joined: Wed Mar 29, 2006 3:47 pm
Posts: 17
I have a OneToOne relationship between two entities, but the foreign key does not reference the primary key, instead it reference another unique key in the referenced table. In such a case, I am getting java.lang.ClassCastException: IncidentStatus
while trying to execute a simple find().

Hibernate version: Hibernate EntityManager 3.2.0.CR1, Hibernate Annotations 3.2.0.CR1, Hibernate 3.2 cr3

Mapping documents:
Code:
@Entity public class Incident {
   @Id String id;

   // See FK does *not* reference PK of IncidentStatus table
   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(referencedColumnName="code")
   IncidentStatus incidentStatus;

   Incident(){}

   Incident(String id) {
     this.id = id;
   }
}

@Entity public class IncidentStatus {
   @Id String id;

   @Column(nullable=false)
   String code;

   @OneToOne(mappedBy="incidentStatus")
   Incident incident;

   public IncidentStatus() {}

   public IncidentStatus(String id) {
     this.id = id;
     this.code = "code_" + id;
   }
}


Full stack trace of any exception that occurs:
Exception in thread "main" java.lang.IllegalArgumentException: IncidentStatus
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:190)
at Client.main(Client.java:11)
Caused by: java.lang.ClassCastException: IncidentStatus
at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:103)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:1995)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1371)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1299)
at org.hibernate.loader.Loader.getRow(Loader.java:1197)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:568)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1784)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:2861)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:393)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:374)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:137)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:193)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:101)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:815)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:808)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:173)
... 1 more


Name and version of the database you are using: Apache Derby, version: 10.1.1.0
The generated SQL (show_sql=true):
Hibernate: select incident0_.id as id0_1_, incident0_.incidentStatus_code as incident2_0_1_, incidentst1_.id as id1_0_, incidentst1_.code as code1_0_ from Incident incident0_ left outer join IncidentStatus incidentst1_ on incident0_.incidentStatus_code=incidentst1_.code where incident0_.id=?

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 16, 2006 1:40 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I guess it's in the doc, the associated class has to be serializable when using a FK referencing a non PK. This is probably easily fixable but nobody usually complains.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 17, 2006 2:13 pm 
Newbie

Joined: Wed Mar 29, 2006 3:47 pm
Posts: 17
Making IncodentStatus Serializable did the trick. I wish it gets fixed. That will increase portability of applications.

Thanks,
Sahoo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 4:37 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
sahoo wrote:
Making IncodentStatus Serializable did the trick. I wish it gets fixed. That will increase portability of applications.

Thanks,
Sahoo


Referencing a non pk is not a mandatory feature of the spec, so I'm not sure it will increase portability :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 6:24 am 
Newbie

Joined: Wed Mar 29, 2006 3:47 pm
Posts: 17
emmanuel wrote:
Referencing a non pk is not a mandatory feature of the spec, so I'm not sure it will increase portability :-)


You are right, user's app is already non-portable.

Thanks,
Sahoo


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.