-->
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.  [ 2 posts ] 
Author Message
 Post subject: How can I exclude certain records from being objects?
PostPosted: Wed Jul 30, 2008 2:54 pm 
Newbie

Joined: Wed Jul 30, 2008 1:54 pm
Posts: 11
I am using the 3.2.6 release of Hibernate Core, the 3.3.2 release of the EntityManager, and the 3.3.1 release of Annotations.

I have an interesting problem with mapping several objects with single table inheritance:

I have the following objects:


Code:
@Entity
@Table(name="TEST_TABLE")
@DiscriminatorColumn(name="COL_1")
public abstract class ParentObject
{
   @Id
   @Column(name="COL_2")
   private String code = null;
   
   @Column(name="COL_3")
   private String name = null;
   
   .....
}

@Entity
@DiscriminatorValue(value="13")
public class ChildObject
extends ParentObject
{
   @Column(name="COL_4")
   private String extraInfo = null;
   
   .....
}



This works very well, except for one problem: In some cases the "code" value in TEST_TABLE is blank (not null, just a blank string). In those cases, I don't want them to be considered valid ChildObjects and I need a way to discriminate against them.

Can somebody suggest a way that I can do that? I've looked at DiscriminatorFormula, but I can't find enough examples to understand how it might help me.

I'm very willing to go a little outside the box with this and use hibernate propriatary extensions, and the like.

Thank you so much.
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 30, 2008 5:55 pm 
Newbie

Joined: Wed Jul 30, 2008 1:54 pm
Posts: 11
I seem to have solved my own problem. With some experimentation and all the logging turned on for hibernate, I was able to discover more about how the DiscriminatorFormula annotation works.

I solved my problem as follows:

Code:
@Entity
@Table(name="TEST_TABLE")
@DiscriminatorFormula(value="case when COL_2 != '' and COL_2 != null then COL_1 end")
public abstract class ParentObject
{
   @Id
   @Column(name="COL_2")
   private String code = null;
   
   @Column(name="COL_3")
   private String name = null;
   
   .....
}

@Entity
@DiscriminatorValue(value="13")
public class ChildObject
extends ParentObject
{
   @Column(name="COL_4")
   private String extraInfo = null;
   
   .....
}


This seems to work for my case. However, I do think it would be nice is there was an annotation or ability to allow me to add a where clause to the class itself, perhaps using the existing @Where annotaion.


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