-->
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: OneToMany for parentless objects.
PostPosted: Tue Feb 23, 2010 10:23 pm 
Newbie

Joined: Tue Feb 23, 2010 10:10 pm
Posts: 2
I have a somewhat complicated schema where I want to do some magic to make a part of it more transparent. Basically I want something like this:

Code:
@Entity
public class Enclosed {
    @ManyToOne
    private Container container;
};

@Entity
public class Container {
    @OneToMany(mappedBy="container")
    private List<Enclosed> enclosed;
}

@Entity
public class UnContained {
    @OneToMany
    @Where(clause="container IS NULL")
    private List<Enclosed> enclosed;
}


Basically, this would allow me to access the Enclosed elements that have no parent (which is a valid state).

I can't figure out how to do this in Hibernate Annotations without doing it by hand (i.e. by custom code that gets the list via an HQL query). Is there any way to do this? It is a database with much history, though it can be changed, I'd rather keep changes to a minimum.


Top
 Profile  
 
 Post subject: Re: OneToMany for parentless objects.
PostPosted: Wed Feb 24, 2010 11:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
From book Hibernate with Persistence:
Quote:
With annotations, you can apply a filter on a many-to-many association with @org.hibernate.annotations.FilterJoin-Table(s)
on the @MantToMany field or getter method.


Top
 Profile  
 
 Post subject: Re: OneToMany for parentless objects.
PostPosted: Wed Feb 24, 2010 12:34 pm 
Newbie

Joined: Tue Feb 23, 2010 10:10 pm
Posts: 2
Sorry, I was not sufficiently detailed in my explanation of what went wrong.

The Annotations I put in actually work, except that they expect an additional join table that links the two classes. What I'm looking for is an implicit join based on the Where clause. That way I don't have to create a new table to hold extra information that is just one more thing to maintain. Any way that I create the OneToMany collection seems to create an extra join table, or need a column in the 'child' table.

I.e. I have lists of objects with a parent, and then I want a collection of the 'orphans' which are explicitly created as orphans...

Maybe I shouldn't do it that way, but that is the way the DB is set up, and I'm trying to make hibernate properly fetch what I need.


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.