-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to store generic List?
PostPosted: Fri Feb 09, 2007 9:54 am 
Beginner
Beginner

Joined: Tue May 30, 2006 10:55 am
Posts: 21
Hibernate version: 1.2.0 beta 3

Hi,
I am trying to store a generic list but I do not know exactly what to do. I have the following structure:
Code:
internal class Person
{
  string firstName = "Jane";
  string lastName = "Doe";
  int id;
 
  internal Person(string fname, string lname)
  {
    this.firstname = fname;
    this.lastname = lname;
  }

  internal int Id
  {
    get { return this.id; }
    set { this.id = value; }
  }

  internal string FirstName
  {
    get { return this.firstName; }
    set { this.firstName = value; }
  }

  internal string LastName
  {
    get { return this.lastName; }
    set { this.lastName = value; }
  }
}

internal class Broker
{
  internal void GenerateAndStore()
  {
    List<Person> list = new List<Person>();
    list.Add(new Person());
    list.Add(new Person("John", "Smith"));

    PersonDAO dao = new PersonDAO();
    dao.Save(list);
  }
}

internal class PersonDAO
{
  internal void Save(List<Person> list)
  {
    //open session to database
    ISession session = factory.OpenSession();
    //store objects in database
    session.SaveOrUpdateCopy(collection); [color=blue]<-- DOES NOT WORK[/color]
    //close session
    session.Close();
  }

  //this method works like a charme
  internal List<Person> GetAllPersons()
  {
    //open session to database
    ISession session = factory.OpenSession();
    //get objects from database
    List<Person> list = session.CreateCriteria(typeof (Person)).List<Person>();
    //close session
    session.Close();
  }
}


Here the mapping file. What do I have to add for working with a generic list? I could not find any good thing in the documentation. The exampes there are all about collections.
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="MyNameSpace.Person, MyAssembly" table="persons">
      <id name="Id" column="id" type="System.Int32">
         <generator class="increment" />
      </id>
      <property name="FirstName" column="firstname" />
      <property name="LastName" column="lastname" />
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 3:47 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
You cant make a collection of objects persistent like that with NHibernate. However, if that person list has a relationship to another entity such as a team, then the list will get persisted automatically for you by NHibernate due to the concept of persistence by reachability.

If you really want to persist a collection of objects like your PersonDao interface is defined then you would have to do something like this:

Code:
internal void Save(List<Person> list) {
         using(ISession session = factory.OpenSession()){
            foreach(Person lifeForm in list){
               session.Save(lifeForm);
            }
            session.Flush();
         }
      }


But it really looks like you should have a method on your Dao which takes a single object and saves it into the session one at a time. Note that I generally wouldnt have a concrete dependency between my DAO and session factory either, but that is another topic all together.


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