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);//老是说空值,也就是没有获得对象,为什么呢??? 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.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; } }
hibernate session.get(class,serialid) 方法为空值的原因?
原文:http://my.oschina.net/u/2308739/blog/405668