先描述下问题:
我在eclipse中配置好了连接mysql数据库,数据库名:webases,表名:admin;字段:id(auto-increment),name,password;并且通过jsp可以成功插入数据,假设此时id是1000,但用select * from admin;却无法查询出刚添加的数据,手动添加一条数据再select * from admin;查询出来的数据id是10001,这说明从jsp页面向mysql插入数据成功,但是为什么查询不出来呢,迷惑中,恳请各位大侠帮忙,感激不尽!!谢谢
我在eclipse中配置好了连接mysql数据库,数据库名:webases,表名:admin;字段:id(auto-increment),name,password;并且通过jsp可以成功插入数据,假设此时id是1000,但用select * from admin;却无法查询出刚添加的数据,手动添加一条数据再select * from admin;查询出来的数据id是10001,这说明从jsp页面向mysql插入数据成功,但是为什么查询不出来呢,迷惑中,恳请各位大侠帮忙,感激不尽!!谢谢
+-----+------+----------+
| id | name | password |
+-----+------+----------+
| 114 | yu | yu |
| 115 | yuyu | yuyu |
| 117 | zhi | zhi |
+-----+------+----------+
3 rows in set (0.00 sec)
也就是从jsp添加进去的那条数据成功了,因为id 号已经自增到117了,但那条数据去哪了呢,为什么查询不出来呢
如果事务也提交了还不行的话加一行session.flush();//这个尽量不使用吧<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
配置文件加这两行关闭二级缓存.还有每次用完了把session.close()一下
package com.hibernate;import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;/**
* Data access object (DAO) for domain model class Admin.
* @see com.hibernate.Admin
* @author MyEclipse - Hibernate Tools
*/
public class AdminDAO extends BaseHibernateDAO { private static final Log log = LogFactory.getLog(AdminDAO.class); //property constants
public static final String NAME = "name";
public static final String PASSWORD = "password";
public void save(Admin transientInstance) {
log.debug("saving Admin instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Admin persistentInstance) {
log.debug("deleting Admin instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Admin findById( java.lang.Long id) {
log.debug("getting Admin instance with id: " + id);
try {
Admin instance = (Admin) getSession()
.get("com.hibernate.Admin", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Admin instance) {
log.debug("finding Admin instance by example");
try {
List results = getSession()
.createCriteria("com.hibernate.Admin")
.add(Example.create(instance))
.list();
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding Admin instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Admin as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
} public List findByName(Object name) {
return findByProperty(NAME, name);
}
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public Admin merge(Admin detachedInstance) {
log.debug("merging Admin instance");
try {
Admin result = (Admin) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
} public void attachDirty(Admin instance) {
log.debug("attaching dirty Admin instance");
try {
getSession().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 {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
再帮忙看下好吗,谢谢!!!
这是addminAction.java
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AddAdminForm addAdminForm = (AddAdminForm) form;
// TODO Add a new admin
Admin admin = new Admin();
admin.setName(addAdminForm.getName ());
admin.setPassword(addAdminForm.getPassword ());
AdminDAO adminDAO = new AdminDAO ();
adminDAO.save(admin);
return mapping.findForward("success");
}
}
log.debug("saving Admin instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
没有使用事务.....................