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.NullPointerException
at hibernate.test.Test.main(Test.java:23)
问题的原因:这个问题的原因是
Person person=(Person)session.get(Person.class, 12)
指定了要修改id号,如果id不存在就为空,否则就返回数据库中的该对象。
person.java
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()方法,来对数据库进行插入或者更新删除。请看如下的例子
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的容错能力稍微弱一些。