第二次调用同一个方法时session关闭

数1000羊的星星 2017-09-28 06:32:44

package com.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateConnection2 {
private static SessionFactory sessionfactory=null;
private static Transaction ctx=null;
static{
Configuration config=new Configuration().configure();
ServiceRegistry sr=new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
sessionfactory=config.buildSessionFactory(sr);
}
public Session getsession(){
return sessionfactory.getCurrentSession();
}

public void gettransaction(){

ctx=this.getsession().beginTransaction();
}
public void commitTransaction()
{

try
{
ctx.commit();
}
catch (Exception e)
{
if(ctx!=null)
{
ctx.rollback();
}
}
}
}


这是四大接口


package com.dao;

import java.util.List;

import org.hibernate.Session;

import com.entity.Dept1;
import com.util.HibernateConnection2;

public class Deptdao extends HibernateConnection2 {
private Session session=getsession();
// public Session set(){
// System.out.println("safdasfasfd");
// session=getsession();
// return session;
// }
public List<Dept1> getall() {
this.gettransaction();
@SuppressWarnings("unchecked")
List<Dept1> list =session.createQuery("from Dept1 order by pid").list();
this.commitTransaction();
return list;
}

public void update(Dept1 dept) {
this.gettransaction();
this.getsession().update(dept);
commitTransaction();

}

public Dept1 getone(int pid) {
this.gettransaction();
Dept1 dept = (Dept1) this.getsession().get(Dept1.class, pid);
commitTransaction();
return dept;
}

public void delete(int pid) {
this.gettransaction();
this.getsession().delete(this.getsession().get(Dept1.class, pid));
commitTransaction();
}

public void add(Dept1 dept) {
this.gettransaction();
this.getsession().save(dept);
commitTransaction();
}
}


这是dao层


package com.action;

import java.util.ArrayList;
import java.util.List;

import com.entity.Dept1;
import com.imple.DeptImple;
import com.service.Deptservice;

public class DeptAction {
private List<Dept1> list = new ArrayList<Dept1>();
private int pid;
private Dept1 dept=new Dept1();
private Deptservice action = new DeptImple();

public Dept1 getDept() {
return dept;
}

public void setDept(Dept1 dept) {
this.dept = dept;
}

public List<Dept1> getList() {
return list;
}

public void setList(List<Dept1> list) {
this.list = list;
}

public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String all(){
this.list=action.getall();
return "all";
}
public List<Dept1> getall(){
this.list=action.getall();
return list;
}


public String add(){
action.add(dept);
return this.all();
}
public String one(){
this.dept=action.getone(pid);
return "one";
}
public String delete(){
this.action.delete(pid);
return this.all();
}
public String update(){
this.action.update(dept);
return this.all();
}

}


比如说当我第一次进全查询后,再点击删除调用action中delete方法,执行完后再返回到全查询getall方法就会出现




...全文
183 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
大神们,别沉呀
  • 打赏
  • 举报
回复
在dao里面调用注释的那个方法,又不会出问题。这是为什么?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧