package hibernate.test; import hibernate.test.pojo.Person; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Configuration config=new Configuration().configure(); ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sFactory=config.buildSessionFactory(sr); Session session=sFactory.openSession(); Transaction tx=session.beginTransaction(); Person person=(Person)session.get(Person.class, 12);//总是说空值,缘由是数据库中该id不存在。 //Person pp=(Person)session.load(Person.class, 12); //也能够取得数据库中的某个对象。 person.setName("oscasfsfsfr"); person.setPassword("sfasf1ddd"); //Person person=new Person();//临时对象 session.merge(person); tx.commit(); session.close(); } }
Exception in thread "main" java.lang.NullPointerExceptionjava
at hibernate.test.Test.main(Test.java:23)数据库
问题的缘由:这个问题的缘由是session
Person person=(Person)session.get(Person.class, 12)
指定了要修改id号,若是id不存在就为空,不然就返回数据库中的该对象。app
person.javaui
package hibernate.test.pojo; public class Person { private Integer id; private String name; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
能够用session.merger()或者session.saveorupdate()或者session.delete()方法,来对数据库进行插入或者更新删除。请看以下的例子this
package hibernate.test; import hibernate.test.pojo.Person; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Configuration config=new Configuration().configure(); ServiceRegistry sr=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sFactory=config.buildSessionFactory(sr); Session session=sFactory.openSession(); Transaction tx=session.beginTransaction(); //Person person=(Person)session.get(Person.class, 7);//持久化对象将会在session关闭的时候自动更新数据 //库对象,不须要显示申明。 Person person=new Person();//临时对象 person.setId(18);//若是不设置id hibernate就直接将数据插入,若是设置了id 则会看看此id是否存在,若是存在 //就更新。 person.setName("1234567"); person.setPassword("sfasf1ddd"); session.merge(person);//若是id给了可是数据库中不存在就插入 //session.saveOrUpdate(person);//若是id给了,可是数据库不存在就报错。 // Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpecte //row count from update [0]; actual row count: 0; expected: 1 /** 删除对象,两种方式。临时对象或者持久化对象 **/ Person person=new Person();//临时对象 person.setId(11); session.delete(person); Person person=(Person)session.get()or session.load()//持久化对象 session.delete(person); tx.commit(); session.close(); } }
总结:从上面的例子能够看出,session.merge()和session.saveorupdate()方法的区别是在是否给id这一块。saveorupdte的容错能力稍微弱一些。spa