-->
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.  [ 5 posts ] 
Author Message
 Post subject: Special case of a one-to-one mapping
PostPosted: Mon Aug 25, 2008 11:47 am 
Newbie

Joined: Mon Aug 25, 2008 11:18 am
Posts: 5
Hello,
I have a Servers table with ServerCode as its primary key. There is also a RestoreHistory table which stores all the history of restorations done to the servers, and has a foreign key to the servers table, so that each restoration can be associated to the relevant server. There is also a RestoreDate.

Now what I need to do, is to define an (one-to-one) association from each server to its most recent restoration, which is characterised by having the maximal restore date. The problem is that I just can't figure how to do that. I read section 7.6 of the Hibernate reference which discusses similar situations, but that didn't help me much. In the first case they look for a null date, which is not my case because the RestoreDate field must always have a non-null value. In the second case they use a join table with a subselect, but I don't have a join table (nor do I need it), and can't use subselect inside the one-to-one element.

Does anyone have an idea how to accomplish that? What are the mappings and formulas I need to use?
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 3:32 pm 
Beginner
Beginner

Joined: Fri Aug 05, 2005 3:36 am
Posts: 28
I think I would map your Server so it has a list or set of Restorations, and sort them by the date, in descending order. Then the first item in the list is the one you want.

If you're using annotations, it'd look like the following. (I've left out ids, getters and setters, and other stuff):

Code:
@Entity
class Restoration {

   @ManyToOne
   private Server server;

}

@Entity
class Server {

   @OneToMany(mappedBy="server")
   @OrderBy("date DESC")
   private List<Restoration> restorations;

   public Restoration getLastRestoration() {
      return restorations.get(0);
   }
...
}


Note that if you don't need traverse the relationship from Restoration to Server, you don't need the @ManyToOne mapping in Restoration.

Anyway, would this work for you?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 4:27 pm 
Newbie

Joined: Mon Aug 25, 2008 11:18 am
Posts: 5
Thanks, actually that's what I have now. The problem is that I don't want to retrieve all the restorations from the database when all I need is 1 specific record. I also thought of using the where attribute of the set element but it can't be used with aggregate functions to sort out the maximum date...
Any other ideas?
Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 6:16 pm 
Newbie

Joined: Thu May 31, 2007 3:27 am
Posts: 11
I think you could use a "one-to-one" association in your Server class. Normally, one-to-one are mapped through primary keys, but I saw in the documentation that a one-to-one association may be defined as an sql "formula".

So you could define a formula that retrieve the most recent Restoration...

I don't know the syntax for annotation, but in a mapping file that would give something like for the Server Class:

<one-to-one name="lastRestoration" property-ref="date" formula="max(NAME_OF_THE_DATE_COLUMN_IN_RESTORATION_TABLE)"/>

I took the idea there:

http://www.onjava.com/pub/a/onjava/2005 ... tml?page=3


There is another possibility, which is using a subselect as defined in hibernate documentation:

http://www.hibernate.org/hib_docs/v3/re ... oc-complex

...which gives an example similar to your case.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2008 10:50 am 
Newbie

Joined: Mon Aug 25, 2008 11:18 am
Posts: 5
Yeah, these are the ways suggested in section 7.6 of the reference. Anyway, I found a solution by separating the restorations table to 2 tables: a table containing only current restorations for all the servers, and a table storing the history of restorations (i.e all the rest), and then defining relevant mappings for both. This is not a perfect solution, but it seems preferable to the other ways I have read and thought of.

Thanks for anyone who tried to help. If other ideas come up to you, you are still invited to post them.


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