hibernate自动生成的dao怎么用啊,这里面也没写Transaction trans = session.beginTransaction();求教 150
publicclassAdminHome{privatestaticfinalLoglog=LogFactory.getLog(AdminHome.class);priv...
public class AdminHome {
private static final Log log = LogFactory.getLog(AdminHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public void persist(Admin transientInstance) {
log.debug("persisting Admin instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(Admin instance) {
log.debug("attaching dirty Admin instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Admin instance) {
log.debug("attaching clean Admin instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
} 展开
private static final Log log = LogFactory.getLog(AdminHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public void persist(Admin transientInstance) {
log.debug("persisting Admin instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(Admin instance) {
log.debug("attaching dirty Admin instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Admin instance) {
log.debug("attaching clean Admin instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
} 展开
展开全部
hibernate自动生成的dao一般都是不全的,而且版本不一样生成的也是有好多差异,楼主生成的这个和我的就有好多不一样,既然这样那可以自己加上去丰富它的Dao,这里我就不多说了。另外至于那个开启事物的代码,没有也只能自己写了,我想它的想法可能是如果自己生成的话可能会造成资源浪费也就没有生成。就楼主生成的这个Dao类来说,我就举个添加功能的例子,即,使用生成的attachDirty()方法。
Transaction trans = session.beginTransaction();
AdminHome adminhome=new AdminHome();
adminhome.attachDirty(admin);//admin是我自己取的名,就是需要保存的类对象
trans.commit();
Transaction trans = session.beginTransaction();
AdminHome adminhome=new AdminHome();
adminhome.attachDirty(admin);//admin是我自己取的名,就是需要保存的类对象
trans.commit();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询