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?
|