-->
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.  [ 1 post ] 
Author Message
 Post subject: @OneToOne conditional on a second column and value?
PostPosted: Sun May 31, 2015 8:36 am 
Newbie

Joined: Thu Jul 31, 2008 2:15 am
Posts: 7
Location: LONDON
Hi everyone,

I have 2 entities:
a) Classroom (has many subjects)
b) Subject (has 1 classroom)

public class Classroom {
...
@OneToMany(mappedBy = "classroom")
private Set<Subject> subjects = new HashSet<Subject>();
...
}

public class Subject {

Integer status = 1;

...
@OneToOne
@JoinColumn(name = "classroom_Id")
private Classroom classroom;
...
}

I want to change the Classroom to have only 1 subject or no subject depending if the subject's status = 1 (active vs 0=inactive) :

public class Classroom {
...
@OneToOne(mappedBy = "classroom")
private Subject> subject;
...
}

But, a OneToOne mapping is only by FK. How do I also specify by an additional column and value eg 'subject.status = 1' ?

1. Can I provide HQL on the OneToOne mapping?
2. Or do I have to keep the ManyToOne and iterate manually over the Set<Subject> looking for the one with status=1? My issue with this, is that a Classroom can have 100s of subjects and I'd rather not fetch them all and then waste time iterating over them.
3. My mapping is completely wrong!!?

Many thanks for your time,
J


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.