-->
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.  [ 6 posts ] 
Author Message
 Post subject: OneToMany but not to All
PostPosted: Tue May 27, 2008 1:20 pm 
Newbie

Joined: Mon Dec 31, 2007 4:44 pm
Posts: 4
Location: Saint-Petersburg
A Person owns several credit cards. I want the set Person.cards to contain only valid cards, which are not yet expired.
Can I do this annotating the Person.cards field?

@Entity
public class Person {

@id
Integer customerId;

String name;

@OneToMany
@JoinColumn(name = "CUSTOMER_ID")
// where card.exp_date>current_date ???
Set<Card> cards;

}

@Entity
public class Card {

Column(name = "CUSTOMER_ID")
Integer customerId; // the owner's ID

Date exp_date; // expiration date

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 4:21 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I'm not sure I follow.

So, you're saying the database will contain expired cards, but you want to annotate them out at the object model level, so when we say getCards, only valid cards are returned?

Just map your objects naturally, and provide a method that gets only valid credit cards. That's one way. This could easily be done using the Criteria API. Here's a little tutorial on it if you like:

http://www.thebookonhibernate.com/HiberBookWeb/learn.jsp?tutorial=09howtousethecriteriaapi

Maybe explore @Where.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Last edited by Cameron McKenzie on Wed Jun 11, 2008 10:01 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 6:17 pm 
Newbie

Joined: Mon Dec 31, 2007 4:44 pm
Posts: 4
Location: Saint-Petersburg
Cameron,
you understand the situation right.
This is a legacy database ...
What is dangerous in doing this way?
Thanks for the references, though.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 5:56 am 
Newbie

Joined: Thu May 29, 2008 5:32 am
Posts: 1
Well even if it is not dangerous what you want is a filter. And filtering is not the job of the annotations but the job of queries/criteria.

If you do this filtering by annotations it means that you will never be able to get the expired cards from the database. So you will not be able to delete them through the Person entity.
Moreover if you can't have them in your Person entity it means that you will also not be able to tell the user that one of his cards has expired, no ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 29, 2008 9:08 am 
Newbie

Joined: Mon Dec 31, 2007 4:44 pm
Posts: 4
Location: Saint-Petersburg
I am new to Hibernate and did not know (though suspended) about existence of @Where annotation. Now I have learned about it and use it and it works well. Actually my application deals not with credit cards but other object, so concerns about access to expired cards do not apply. You guys recommend criteria and filters instead - is it radically different (and better) from the @Where annotation?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 9:37 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Like filters and criteria, @Where is just another tool in your data persistence toolkit.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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