MySQL数据库使用Innodb数据库类型(工具生成Hibernate+Spring代码)
Posted on 2006-12-01 11:16 YCOE 阅读(1138) 评论(1) 编辑 收藏 举报 在Hibernate+Spring项目中,如果将MySQL数据库类型设置成为Innodb类型,如果不进行特殊设置的话,就会出现数据无法插入的问题,但是在控制台上,却可以正确地打印出SQL语句。把此SQL复制到DOS下的MySQL控制台上,又可以正常插入,这是个很纳闷的问题。特别是通过DDL用工具生成Hibernate映射文件和POJO代码时(本人用的是MySQL自带的Hibernate Reverse Engineering...)
DDL代码如下:
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment COMMENT '用户ID',
`name` varchar(20) default NULL COMMENT '昵称',
`password` varchar(20) default NULL COMMENT '用户密码',
`purwiew` int(11) default '0' COMMENT '用户权限',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 COMMENT='用户';
生成的POJO代码:`id` int(11) NOT NULL auto_increment COMMENT '用户ID',
`name` varchar(20) default NULL COMMENT '昵称',
`password` varchar(20) default NULL COMMENT '用户密码',
`purwiew` int(11) default '0' COMMENT '用户权限',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312 COMMENT='用户';
package com.xu.ycoe.pojo;
/**
* Users generated by MyEclipse - Hibernate Tools
*/
public class Users implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private String password;
private Integer purwiew;
// Constructors
/** default constructor */
public Users() {
}
/** minimal constructor */
public Users(Integer id) {
this.id = id;
}
/** full constructor */
public Users(Integer id, String name, String password, Integer purwiew) {
this.id = id;
this.name = name;
this.password = password;
this.purwiew = purwiew;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPurwiew() {
return this.purwiew;
}
public void setPurwiew(Integer purwiew) {
this.purwiew = purwiew;
}
}
生成的DAO:/**
* Users generated by MyEclipse - Hibernate Tools
*/
public class Users implements java.io.Serializable {
// Fields
private Integer id;
private String name;
private String password;
private Integer purwiew;
// Constructors
/** default constructor */
public Users() {
}
/** minimal constructor */
public Users(Integer id) {
this.id = id;
}
/** full constructor */
public Users(Integer id, String name, String password, Integer purwiew) {
this.id = id;
this.name = name;
this.password = password;
this.purwiew = purwiew;
}
// Property accessors
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPurwiew() {
return this.purwiew;
}
public void setPurwiew(Integer purwiew) {
this.purwiew = purwiew;
}
}
package com.xu.ycoe.pojo;
import java.util.Iterator;
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.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Data access object (DAO) for domain model class Users.
*
* @see com.xu.ycoe.pojo.Users
* @author MyEclipse - Hibernate Tools
*/
public class UsersDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UsersDAO.class);
// property constants
public static final String NAME = "name";
public static final String PASSWORD = "password";
public static final String PURWIEW = "purwiew";
protected void initDao() {
// do nothing
}
public void save(Users transientInstance) {
log.debug("saving Users instance");
System.out.println("saving Users instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
System.out.println("save successful");
System.out.println(transientInstance.getName());
} catch (RuntimeException re) {
log.error("save failed", re);
System.out.println(re);
throw re;
}
}
public void delete(Users persistentInstance) {
log.debug("deleting Users instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Users findById(java.lang.Integer id) {
log.debug("getting Users instance with id: " + id);
try {
Users instance = (Users) getHibernateTemplate().get(
"com.xu.ycoe.pojo.Users", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Users instance) {
log.debug("finding Users instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
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 Users instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Users as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} 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 List findByPurwiew(Object purwiew) {
return findByProperty(PURWIEW, purwiew);
}
public Users merge(Users detachedInstance) {
log.debug("merging Users instance");
try {
Users result = (Users) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Users instance) {
log.debug("attaching dirty Users instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Users instance) {
log.debug("attaching clean Users instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {
return (UsersDAO) ctx.getBean("UsersDAO");
}
public List getAllObj(int firstResults, int maxResults) {
String sql = "from Users as u Order by u.id desc";
Query q = this.getSession().createQuery(sql);
q.setFirstResult(firstResults);
q.setMaxResults(maxResults);
return q.list();
}
}
import java.util.Iterator;
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.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
/**
* Data access object (DAO) for domain model class Users.
*
* @see com.xu.ycoe.pojo.Users
* @author MyEclipse - Hibernate Tools
*/
public class UsersDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(UsersDAO.class);
// property constants
public static final String NAME = "name";
public static final String PASSWORD = "password";
public static final String PURWIEW = "purwiew";
protected void initDao() {
// do nothing
}
public void save(Users transientInstance) {
log.debug("saving Users instance");
System.out.println("saving Users instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
System.out.println("save successful");
System.out.println(transientInstance.getName());
} catch (RuntimeException re) {
log.error("save failed", re);
System.out.println(re);
throw re;
}
}
public void delete(Users persistentInstance) {
log.debug("deleting Users instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Users findById(java.lang.Integer id) {
log.debug("getting Users instance with id: " + id);
try {
Users instance = (Users) getHibernateTemplate().get(
"com.xu.ycoe.pojo.Users", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Users instance) {
log.debug("finding Users instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
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 Users instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from Users as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} 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 List findByPurwiew(Object purwiew) {
return findByProperty(PURWIEW, purwiew);
}
public Users merge(Users detachedInstance) {
log.debug("merging Users instance");
try {
Users result = (Users) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(Users instance) {
log.debug("attaching dirty Users instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(Users instance) {
log.debug("attaching clean Users instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static UsersDAO getFromApplicationContext(ApplicationContext ctx) {
return (UsersDAO) ctx.getBean("UsersDAO");
}
public List getAllObj(int firstResults, int maxResults) {
String sql = "from Users as u Order by u.id desc";
Query q = this.getSession().createQuery(sql);
q.setFirstResult(firstResults);
q.setMaxResults(maxResults);
return q.list();
}
}
生成的映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.xu.ycoe.pojo.Users" table="users">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20" />
</property>
<property name="purwiew" type="java.lang.Integer">
<column name="purwiew" />
</property>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.xu.ycoe.pojo.Users" table="users">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="increment" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20" />
</property>
<property name="purwiew" type="java.lang.Integer">
<column name="purwiew" />
</property>
</class>
</hibernate-mapping>
这些都没什么问题,而对于在之前生成的Spring配置文件或Hibernate配置文件,则有要修改和注意的地方:
首先,我用的是三方连接池,所以配置可能和自动生成的有所区别,但不外乎这几方面:
1.要注意数据库的dialect。一般为org.hibernate.dialect.MySQLMyISAMDialect或org.hibernate.dialect.MySQLDialect
这里要把它改成org.hibernate.dialect.MySQLInnoDBDialect。
2.添加一行自动提交属性的设置
<prop key="hibernate.connection.autocommit">true</prop>
(这里是Hibernate3.1的配置,之前版本可能有所出入)3.添加测试代码,在UsersDAO类中添加以下代码:
public %2