-->
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: Conditional many-to-one mapping (@Formula? @Where)
PostPosted: Thu Jul 26, 2007 1:51 pm 
Newbie

Joined: Thu Jul 26, 2007 1:12 pm
Posts: 2
Location: New York
Ok, I am trying to do something very simple and seem to be running into some issues with hibernate annotations doing it:

I have two tables in a ManyToOne mapping - a DEGREE table and a DEGREE_COMPONENT table, mapped to Degree and DegreeComponent objects respectively. A degree can have multiple components, each of which can only belong to a single degree, via the DEGREE_COMPONENT.DEGREE_ID foreign-key. So far so good. The mappings are set up like this:

In DegreeComponent.java:
[code]
@Cascade(org.hibernate.annotations.CascadeType.EVICT)
@JoinColumns({ @JoinColumn(name = "DEGREE_ID") })
@Where(clause="parentComponent is null")
// @Formula(value=("select degree_id from c_degree_component where component_id = id and parent_component_id is null"))
@ManyToOne(fetch = FetchType.EAGER)
private Degree degree;
[/code]

In Degree.java:
[code]
@Cascade(CascadeType.EVICT)
@OneToMany(
mappedBy = "degree",
fetch = FetchType.EAGER
)
@Sort(
type = SortType.COMPARATOR,
comparator = DegreeComponentComparator.class
)
private Set<DegreeComponent> components;
[/code]

Now the thing is, there is also a PARENT_COMPONENT_ID in the DEGREE_COMPONENT table, which needs to be checked before loading the "degree" attribute of DegreeComponent. Essentially, it should only load the degree if the parent_component_id (which is mapped to a parentComponent attribute of type DegreeComponent - there is a component tree of sorts) is null (i.e. only "root" components are attached to a degree).

I tried several ways of achieving this via Hibernate annotations without success. Putting an @Where (see code above) did not make a dent - it brought back all components anyway, even those with a non-null parentComponent. The @Formula (commented in above code) just puked with ORA exceptions, because Hibernate generated some pretty wonky column names (like DEGREE_COMPONENT.DEGREE_DEGREE_ID), most definitely because I put it in wrongly.

I found *very little* documentation on the use of @Formula and @Where for use in such situations, so any help would be appreciated:

1. Is this even possible to do? (I'd be shocked if it wasn't).
2. If it is, what am I missing, or doing wrong?

Sadly I have very little control over the data model, so I cannot, for instance, null out all DEGREE_COMPONENT.DEGREE_ID where PARENT_COMPONENT_ID is not null.

Any help greatly appreciated.

Hrishi


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 12:25 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I haven't really understood what you're trying to achieve but @Formula is not yet supported on associations.

_________________
Emmanuel


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.