-->
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.  [ 4 posts ] 
Author Message
 Post subject: Saving a vector of object
PostPosted: Sun Jan 04, 2004 1:06 am 
Beginner
Beginner

Joined: Sun Dec 21, 2003 9:18 pm
Posts: 21
Hi,

I have read the "Collection" chapter in hibernate doc. And I still dont get how to save a vector / list of objects into a table.

OKay basically I have this table, this is just a mock SQL create statement to show how the table look like so the SQL statement might not work

Create table address (
ADDRESS_ID BIGINT AUTO_INCREMENT
USER_ID varchar2(50),
STREET VARCHAR2(300),

FOREIGN KEY USER_ID REFERENCES USER(USER_ID)
)

My user may have more than 1 addresses. And from my Form Class I will return a vector/list/arrray of addresses to be saved to the table.

Do I need to loop the vector/list and save the object 1 by 1 or Can I use HIbernate to persist the vector ?

My Address object will be something like this:

Code:
public class UserAddress implements AbstractPersister {

   ArrayList addrList = new ArrayList();
   
   /* (non-Javadoc)
    * @see AbstractPersister#getPersisterClass()
    */
   public Class getPersisterClass() {
      
      return UserAddress.class;
   }
         

   /**
    * @return
    */
   public ArrayList getAddrList() {
      return addrList;
   }

   /**
    * @param list
    */
   public void setAddrList(ArrayList list) {
      addrList = list;
   }

}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 6:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
First - AbstractPersister seems like a bad name for an interface ? Is it part of your own persistenceengine or something ? (just curious)

Secondly, always use interface for collections (as per the docs)...use List, Set, SortedSet instead of ArrayList, HashSet, TreeSet.

Thirdly, what objects are you storing in addresslist ?

And why don't User's just have a List of Address's ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 9:49 am 
Beginner
Beginner

Joined: Sun Dec 21, 2003 9:18 pm
Posts: 21
Hmm well abstract persister is just an Interface with one method getClass(). The reason I created this interface is because I am passing all the object that I want to save into a separate class call DBManager.

This class accepts any instance of AbstractPersister and save those instances using session.save(). Here is the code:

Code:
public static void save(AbstractPersister persister)
throws MappingException, HibernateException {

    Session s = getSession(persister);

    Transaction tx = s.beginTransaction();
    s.save(persister);
   
    tx.commit();
      
    closeSession();

}

private static Session getSession(AbstractPersister persister)
throws MappingException, HibernateException {

Configuration config = new Configuration().addClass(persister.getClass());

sessionFactory = config.buildSessionFactory();
session = new ThreadLocal();
Session s = (Session) session.get();

if (s == null) {
s = sessionFactory.openSession(getConnection());

}

return s;

}


Well I guess I can make the user class contains a vector/list of addresses.

Thanks for the hints ^__^


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
You are building a sessionfactory EVERY time you do a save ? That's a biiiig performance killer!

I would suggest that you go look at http://www.hibernate.org/152.html - just to get the basics in place.

_________________
Max
Don't forget to rate


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