weixin_42361296
weixin_42361296
2010-06-16 21:46

hibernate 问题

  • hibernate

public class LoginService {
private Login mb;
private LoginImpl loginImpl;
public LoginImpl getLoginImpl() {
return loginImpl;
}

public void setLoginImpl(LoginImpl loginImpl) {
    this.loginImpl = loginImpl;
}

public Login getMb() {
    return mb;
}

public void setMb(Login mb) {
    this.mb = mb;
}

public String creat(){
    mb.getPwd();
    mb.getUsername();
    System.out.println(mb.getPwd()+"++++++++++++++++++");//确实得到值了从前台
    System.out.println(mb.getUsername()+"-------------------");//确实得到值了从前台
    loginImpl.creatMember(mb);//这里是null  为什么??
    return "success";
 }

public class LoginImpl {

static Session session = null;

public void creatMember(Object o){
    try{
        session = sessionFactory.opensession();
        Transaction tx = session.beginTransaction();
        session.save(o);
        tx.commit();
    }catch(HibernateException ex){
        ex.printStackTrace();
    }
}

}

下面是sessionfactory的方法

package util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class sessionFactory {
private static SessionFactory sf =null;

public static void Session(){
    try{
        sf = new Configuration().configure().buildSessionFactory();
    }catch(HibernateException ex){
        throw new RuntimeException("Exception is"+ex.getMessage());
    }
}

public static Session opensession (){
    Session s = sf.openSession();
    return s;
}

public static void closeSession(Session s){
    if(s!=null)
        s.close();
}

}

请问各位高手们 我想传过去一个对象 mb 然后save(mb)入库,进入MyEclipse调试看见 为啥loginImpl.creatMember(mb)为null啊??还有就是请问 我这么用hibernate 对嘛?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换