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
|