-->
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: Required help on Criteria, Detached Criteria.
PostPosted: Wed Jan 20, 2010 11:50 am 
Newbie

Joined: Wed Jan 20, 2010 11:12 am
Posts: 15
I am having a mapping table called mapping_user_account, which is having a primary key as pk_user_account_id, fk_user_id, fk_account_id. and i have made hibernate-mapping file for this table as
Code:
<hibernate-mapping>
<id name="pkUserAccountId" type="int">
      <column name="pk_user_account_id"/>
      <generator class="assigned"/>
</id>

<many-to-one class="model.Account" fetch="select" name="luAccount">
      <column name="fk_account_id" not-null="true"/>
</many-to-one>

<many-to-one class="model.User" fetch="select" name="luUser">
      <column name="fk_user_id" not-null="true"/>
</many-to-one>
</hibernate-mapping>


and Model is look like
Code:
class MapUserAccount{
    private int pkUserAccountId;
    private Account luAccount;
    private User luUser;
// getter setter
}


now I am trying to fetch User Object by using DetachedCriteria, but i am confuse that how should i Use that for scenario like:
I want to fetch the User, where fk_user_id =1 and fk_account_id=1 from mapping_user_account table.

I am using user table to store the user data, and account table for storing account information.
and this user_id belongs to user table as pk_user_id, and account_id belongs to
account tabe as pk_account_id

Let me know if you guys want more detail
Please help me out, as i am a new user of hibernate


Top
 Profile  
 
 Post subject: Re: Required help on Criteria, Detached Criteria.
PostPosted: Thu Jan 21, 2010 1:08 pm 
Newbie

Joined: Wed Sep 10, 2008 2:31 pm
Posts: 16
It would probably go something like this:

Code:
DetachedCriteria detached= DetachedCriteria.forClass(MapUserAccount.class);
//I believe you have to create an alias to get to the classes linked in the mapping file
detached.createAlias("luUser", "user");
detached.createAlias("luAccount", "account");

detached.add(Restrictions.eq("user.fk_user_id", 1));
detached.add(Restrictions.eq("account.fk_account_id", 1));

Criteria crit = detached.getExecutableCriteria(session);
List allMapUserAccounts = crit.list();


Top
 Profile  
 
 Post subject: Re: Required help on Criteria, Detached Criteria.
PostPosted: Tue Feb 16, 2010 2:45 am 
Newbie

Joined: Wed Jan 20, 2010 11:12 am
Posts: 15
Is this approach is good for storing Data.
i.e
1. Instead of using the whole object Of Account and User to set into the map_user_account?
2. or should i only use account_id or User_id to store into the database?

In first Approach what i need to do is, when i want to store the Map_user_account then I have to set the Account Object as
Code:
mapUserAccObj.setAccount(accountObj)
, as well as
Code:
mapUserAccObj.setUser(userObj)
, and for doing this what i have to do is i have to fetch the accountObj and userObj by two queries.


And in second approach I only need to set the account_id and user_id to the mapUserAccObj, isn't it?

So i am confused which one to use?Please help me out


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.