-->
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.  [ 7 posts ] 
Author Message
 Post subject: Beginners help needed on mapping objects.
PostPosted: Mon Aug 22, 2005 3:34 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
Hi i am a beginner when it comes to hibernate, i evaluated using it 2years ago, but due to the fact that i thought that the project i was working on would suffer due to long adaption time i did not pursue it any more.

Now i am working on a much smaller project, still connected to the original one, and i am concidering hibernate again. And one issue came up to me. And i wanted to ask the experts on the best practises when it comes to this specific issue i have.

Let say that i have 3 tables in the database.
1. Competitions
2. Rounds
3. Events

As you might see one competition might have many rounds and one round might have many events.

a. So if i wanted to get all the events for a certain competition i would have to make a join on 3 tables.
b. If i wanted to get the information on the competition i would have to make a query on one table.

Here comes my question.
Because in my application i will query single tables as well as making multiple join tables what is the best practise?
I made one small example to test which is something like this.

I made one xml mapping called TbCompetition.java and Competition.hbm.xml which mappes the class TbCompetition to table Competition, this mapping is a single one, no definitions whatsoever to any other table in the xml

Then i also made another mapping called TbFixture and Fixture.htm.xml and in this file i mapp TbFixture to Competition but i also have
<set name="rounds" lazy="false">
<key column="competition_id" />
<one-to-many class="TbRound" />
</set>

so if i wanted to do a query on the competition just to get the name of the competition i would use the Single object and if i wanted to make a more complex combination i would use the fixture object.

Now this worked just fine... but lets make the whole project a bit more complex and add another... lets say 10 tables, on one to many and many to many connections... everything becomes more unclear to me. Should i make new xml definitions and classes for all my queries in the project or should i define joins and not use them. Or as one recommended to me i should use HQL instead... and define the joins realtime. But HQL dont look that good to me as i would prefer to use getters and setters instead.

So the reason i post this question is to ask what would be the best practise in this case. I have a "BIG" project, alot of tables that are joined back and forth to get the information. What is the best way to do it, not only memory wise...

thanks for your help in advance...

kind regards
Jonny


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 4:04 am 
Newbie

Joined: Wed Dec 03, 2003 9:41 am
Posts: 7
Location: Stockholm, Sweden
I would suggest that you start by mapping your original model as is and use HQL for the queries. Then populate your DB with large amounts of data and only when/if you find any performance issues, try different optimization methods (lazy loading, caching, your fixtures etc.).
I think this will help you from spending too much time doing optimizations that you might not need.

Greetings
///Odd Möller


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 5:04 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
I am doing just this right now... but i do not understand how to loop through the list...
I am sorry to say this to all the hibernate staff but i find your documentation very much incomplete... you never show the all the parts in you examples to see how to proceed from one step to another...

Well here is the code...

StringBuffer buf = new StringBuffer();
buf.append("select c.competitionName, r.roundName, e.eventDate");
buf.append(" from TbCompetition c, TbRound r, TbEvent e");
buf.append(" where c.competitionId = " + arg_competitionId);
buf.append(" and r.competitionId = c.competitionId");
buf.append(" and e.roundId = r.roundId");
Session session = com.xscores.common.util.HibernateUtil.currentSession();
Query c = session.createQuery(buf.toString());

List l = c.list();
com.xscores.common.util.HibernateUtil.closeSession();

if i now want to get the result of this query which is in my List l... how do i do that, what do i cast this object to?

/Jonny


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 6:45 am 
Newbie

Joined: Wed Dec 03, 2003 9:41 am
Posts: 7
Location: Stockholm, Sweden
Without knowing what your domain objects look like, I would try something like:
Code:
String stmt = "select new EventInstance(c.competitionName, r.roundName, e.eventDate)"
    + " from TbCompetition as c"
    + "     left join fetch c.rounds as r"
    + "     left join fetch r.events as e"
    + " where c.competitionId = :competitionId";
...
Query q = session.createQuery(stmt);
List l = query.setLong("competitionId", id).list();

where:
Code:
public class EventInstance {
    ...
    public EventInstance(String competitionName, String roundName, Date eventDate) {
        ...
    }
    ...
}


Greetings
///Odd Möller


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 7:05 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
JonnyB wrote:

if i now want to get the result of this query which is in my List l... how do i do that, what do i cast this object to?

/Jonny


see [url]http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html#queryhql-select
[/url]

You just need a class to hold your results, with an appropriate constructor of the fields in yur result list.

NB you only need to do this technique if you are running a query to get a subset of columns, not if you want the whole object returned.

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 7:35 am 
Newbie

Joined: Mon Aug 22, 2005 3:15 am
Posts: 9
well the sollution i was looking for was...
the the List returned is an Object[] it will be Strings in case there is a String field or the Table itself as an object..

/Jonny


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 26, 2005 8:59 am 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
I would recommend you buy the book "Hibernate in Action". I started with Hibernate last year. This book was very helpful and I still refer back to it all the time. It is based on Hibernate 2, but, it is still a very good resource for beginners.


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