hibernate session.get(class,serialid) 方法为空值的原因?

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的容错能力稍微弱一些。

转载于:https://my.oschina.net/u/2308739/blog/405668

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值