-->
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: Composite key with query
PostPosted: Fri May 16, 2008 4:05 pm 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
I used Hibernate tools to generate db code.
I have a table called Transaction. The reverse engineering created 3 classes:
1. Transaction.java
2. TransactionId.java for my composite key
3. TranasactionHome.java

My question is how can I retrieve my Transaction objects that include all of the primary key data as well?

Right now I can query for objects, but not for primary keys.
I can already persist an object and query for all Transaction objects.

DoI have to query for the keys then use the keys to retrieve the other items? Seems strange if so.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 5:00 pm 
Newbie

Joined: Tue Apr 25, 2006 1:17 pm
Posts: 19
To load your 'Transaction' class by its composite key, you can use:

Code:
TransactionId id = new TransactionId(/*use the appropriate key values to construct this class*/

Transaction myTransaction = (Transaction) session.get(Transaction.class, id);


This will load your Transaction object by its composite id, provided that you supply the necessary key values.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 8:58 am 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
Thanks that helps alot.

How do I select all data including the keys without 1st providing a key?
Currently I can retreive all data without the keys.

: )


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 10:06 am 
Beginner
Beginner

Joined: Fri May 16, 2008 3:59 pm
Posts: 30
I figured it out:
I can get the burried TransactionId, by using transacton.getId().getCompositeKey(getAccountNumber,getAccountId,getFundName,etc..)

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
//Criteria Query Example
Query q = session.createQuery("from Transaction");
List transactions = q.list();

System.out.println(transactions.size());


for(Iterator it = transactions.iterator();it.hasNext();){
Transaction transaction = (Transaction) it.next();
System.out.println("Cusip: " + transaction.getCusip());
System.out.println("Rep id: " + transaction.getRepid());

//important line....
System.out.println(transaction.getId().getAccountnumber());

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 4:46 pm 
Newbie

Joined: Tue Apr 25, 2006 1:17 pm
Posts: 19
If it helped, could you rate the post?

Thanks,

mc1392 wrote:
Thanks that helps alot.

How do I select all data including the keys without 1st providing a key?
Currently I can retreive all data without the keys.

: )


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.