-->
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: Can I map a one-to-many relationship as one-to-one?
PostPosted: Tue Mar 09, 2010 5:01 pm 
Newbie

Joined: Fri Feb 15, 2008 6:24 pm
Posts: 5
Hi,

I have an entity called Material. It can have many Rental, but only one active at a time (Rental has a column named active = 0/1).
Can I map that using @OneToOne? I'm only interested in the active (if there is one) Rental.

I've tried this in Material:
Code:
@OneToOne(fetch=FetchType.LAZY, mappedBy="material")
   public Rental getRental() {
      return rental;
   }

But that has caused problems with some Materials that have many non-active Rental (and none active):
Quote:
More than one row with the given identifier was found


This mapping is important for a query like this:
Code:
select mat from Material mat
join fetch mat.type type
join fetch type.group group
left join fetch mat.rental rental
where ( (mat.creationDate between :startDate and :endDate) or (mat.state = 41 and rental.active = true and rental.authDate between :startDate and :endDate) )

(Basically, this is just retrieving all materials created or rented in a certain month).

I've seen there are @Where, @Filter and @Formula annotations, but I couldn't find good explanation or examples about their usage.


Thanks.


Top
 Profile  
 
 Post subject: Re: Can I map a one-to-many relationship as one-to-one?
PostPosted: Wed Mar 10, 2010 4:05 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I would try with following:

Code:
@Entity
public classMaterial {

@OneToOne(fetch=FetchType.LAZY, mappedBy="material")
@Where(clause="active = 1")
   public Rental getRental() {
      return rental;
   }
}


Anyway it is something weird to map a one-to-many relationship as one-to-one,
I personally would not do it, because of the risk encountering problems in future which were not visible immediately.
Furthermore the schema doesn't change if you map it as one-to-many with the mappedBy attribute,
so in theory any sql-statement should work in identical way.


Top
 Profile  
 
 Post subject: Re: Can I map a one-to-many relationship as one-to-one?
PostPosted: Wed Mar 10, 2010 9:35 am 
Newbie

Joined: Fri Feb 15, 2008 6:24 pm
Posts: 5
I've tried that before, but was not sucessful. As you said, it only acts in the SQL statement. The relationship is still many-to-one. Same for @Filter.
I'll keep it that way.

I didn't know those where clauses in my HQL would be applied to each element in the collection. So the query is still ok.

Thanks.


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.