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