-->
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: How to get a SessionFactory second time?
PostPosted: Tue Oct 12, 2004 9:27 am 
Newbie

Joined: Tue Oct 12, 2004 8:56 am
Posts: 3
when i using the Hibernate i find a little problem and i nearly make me crazy!
The problem is that when i use the following and get a SessionFactory;
Configuration conf= new Configuration().addClass(PersonModel.class);
sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession();

Transaction t = s.beginTransaction();

PersonModel p1=new PersonModel();
p1.setName("soso");
p1.setAddress("china");


s.save(p1);
t.commit();
s.close();

The first time this goes very well,the person i create was put into the database,but the next time i using a Query but the problem goes out:
Configuration conf= new Configuration().addClass(PersonModel.class);
sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession();
PersonModel p = new PersonModel();
Query q = s.createQuery("from PersonModel as p where p.id=1");
p = (PersonModel) q.list().get(0);
System.out.println(p.getName());
s.close();
but i find that the person i create befor was deleted by Configuration,i think that the Configuration delete the Person table which i create befor in the database,and create a null table again.So how can i get the sessionfactory after the table has been created correct?not use the Configuration?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 9:43 am 
Beginner
Beginner

Joined: Tue Jun 22, 2004 7:50 am
Posts: 25
Have you checked the database directly to see whether your Person table contains anything? Has the person you've created actually got an id of 1? What happens if you query for all Persons eg using:

List people = session.find("from PersonModel");

You should re-use SessionFactorys, they're heavy weight and cache lots of stuff, so just use a class attribute: and don't forget to close() the SessionFactory when you've finished with it.

Edward

PS

PersonModel p = new PersonModel(); <-- no need for this
Query q = s.createQuery("from PersonModel as p where p.id=1");
p = (PersonModel) q.list().get(0); <-- when you do this 2 lines later.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 7:25 pm 
Newbie

Joined: Tue Oct 12, 2004 8:56 am
Posts: 3
Thanks!
Yes,i have checked the database to see wether the person has been create and i find i have already cteate the person!
I create the project in Eclipse,the second time means that second time i run the test programe,but i find it seems that every time i use the Configuration().configure().buildSessionFactory() ,it will delete the table create befor.So i ask how can i get the sessonfactory without using the Configuration().configure().buildSessionFactory().

ps:the id filed is the auto build filed i create.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 13, 2004 3:19 am 
Beginner
Beginner

Joined: Tue Jun 22, 2004 7:50 am
Posts: 25
I can pretty much guarantee that Configuration().configure().buildSessionFactory() does not drop your table: it's what I use and AFAIK the only way to get a SessionFactory(): if it did drop the table no-body would be able to do anything with Hibernate!

Have you tried commenting out bits of your code, starting with Configuration().configure().buildSessionFactory().
just to convince yourself, and seeing which bit of code actually is the problem? Are you sure your database URL is correct for example? Is the Configuration the one you are expecting? Are you sure it's not picking up a different configuration file off the classpath? Have you looked at the log output to see what Hibernate is doing (might be worth turning SQL logging on).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 12:39 am 
Newbie

Joined: Tue Oct 12, 2004 8:56 am
Posts: 3
I hava find the problem is that my hibernate.properties was wrong!I change the hibernate.properties with another one and the programe goes very well.


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.