-->
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.  [ 1 post ] 
Author Message
 Post subject: Use the entity itself as IdClass, does it work?
PostPosted: Fri Oct 02, 2009 7:36 pm 
Newbie

Joined: Mon Sep 07, 2009 4:20 pm
Posts: 12
Hello,

I have a huge legacy database with loads of tables which have composite keys.

To avoid creating a separate classPK for all my entity classes (of the quite huge legacy database I work with using hibernate), I came up with a (maybe crazy) idea.

Can I not use the "main" entity as IdClass as well?

Consider this:

Code:
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "MYTEST")
@IdClass(MyTest.class) // this is not a typo, I deliberately declare the entity as IdClass as well

public class MyTest implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   private int ordernr;
   @Id
   private int custnr;
   @Id
   private int orderdate;

   private int itemnr;
   private Integer qty;
   private String name;
   private String otherString1;
   // setters and getters omitted
}



Now when I run the following code:

Code:
      MyTest t = new MyTest();
      t.setOrdernr(2);
      t.setCustnr(2);
      t.setOrderdate(1234);
      
      MyTest myTest = (MyTest) session.load(MyTest.class, t);


I get the following exceptions:

Code:
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [mydomain.MyTest#custnr=2, ordernr=2, orderdate=1234]
   org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:409)
   org.hibernate.proxy.AbstractLazyInitializer.checkTargetState(AbstractLazyInitializer.java:108)
   org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:97)
   org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:140)
   org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
   mydomain.MyTest_$$_javassist_0.getOrderdate(MyTest_$$_javassist_0.java)


Is it at all possible what I try to do?

I have several hundred tables in the DB, the annotated hibernate pojos will be generated, nevertheless I would be glad to avoid doubling the number of classes "just" for adding the "tablePK-classes" for every entity.

Best regards,
yglodt


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.