-->
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.  [ 4 posts ] 
Author Message
 Post subject: load(), implicit polymorphism, and identity key generation
PostPosted: Fri Jul 15, 2005 11:19 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
-My mappings specify identity key generation.

-I rely on load() to return proxies, so I can establish associations without doing an actual query.

-My classes are interface/implementation pairs, mapped using implicit polymorphism. I don't specify the interface in the mapping file, but I do run queries and loads against the interface, expecting hibernate to return instances of the implementation.

-You can't load(Interface.class) when using implicit polymorphism.

-union-subclass, which would make load(Interface.class) work, is not compatible with identity key generation.

-oh snap

he;lp[


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 1:15 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
I kind of sidestepped this by using the Restrictions.idEq() idiom as outlined in the reference docs, and then caching that query. Kind of lame, I'm still looking for a better solution.

Code:
   public E getById(Long id)
   {
      Session session = HibernateUtil.getSession();         
      Criteria crit = session.createCriteria(entityName)
         .add(Restrictions.idEq(id))
         .setCacheable(true);
      
      @SuppressWarnings("unchecked")
      E entity = (E)crit.uniqueResult();      
      
      return entity;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 11:08 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Some more things I've tried:

I reasoned that since I only have one unioned subclass, representing the only actual table in the database for that hierarchy, it is not an error to use identity key generation. I copied the code from IdentityGenerator.java into my own class in order to avoid Hibernate's instanceof check and used that as my generator. Unfortunately, some of my <class> mappings have <joined-subclass> mappings, which you can't nest inside a <union-subclass>. You can nest a <subclass><join> in a <union-subclass>, but you can't nest a <set> in turn within a <subclass><join> like you can in a <joined-subclass>.

As a result I'm kind of stuck. How is this actually done? I cannot be the only person encountering this problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 9:02 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Code:
   //FIXME: this is so stupid it hurts
   public E getById(Long id)
   {
      Session session = HibernateUtil.getSession();         
      
      int lpkg = entityName.lastIndexOf('.') + 1;
      String implementationName =
         entityName.substring(0, lpkg) + "impl." +
         entityName.substring(lpkg) + "Impl";
      
      @SuppressWarnings("unchecked")      
      E entity = (E)session.load(implementationName, id);      
      return entity;
   }


Look what you've reduced me to. Are you really going to let me get away with this crime against nature? Where's your sense of decency man!


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