Hibernate
2007/01/22 18:33
问题描述:
hibernate技术中对应数据库中每一个表,都会有一个映射文件与之对应,此文件描述数据库表中每一个字段的类型、长度、是否可空等属性。在进行表中记录的插入(更新)操作时,hibernate会根据映射文件中的描述自动生成一个包含所有字段的插入(更新)sql语句,此时如果映射文件中某字段的值为空(NULL)而其在数据库表中定义的默认值不为空,hibernate会将空值插入到表中,而不会使用此字段的默认值。
解决方法:
在hibernate映射文
hibernate技术中对应数据库中每一个表,都会有一个映射文件与之对应,此文件描述数据库表中每一个字段的类型、长度、是否可空等属性。在进行表中记录的插入(更新)操作时,hibernate会根据映射文件中的描述自动生成一个包含所有字段的插入(更新)sql语句,此时如果映射文件中某字段的值为空(NULL)而其在数据库表中定义的默认值不为空,hibernate会将空值插入到表中,而不会使用此字段的默认值。
解决方法:
在hibernate映射文
2007/01/04 15:00
2006/12/26 22:59
insert或update时中文出现乱码的解决方法:
在连接串后面加上数据库编码。
<property name="connection.url">
jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8
</property>
在连接串后面加上数据库编码。
<property name="connection.url">
jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8
</property>
2006/11/04 23:24
package org.jock.hibernate.model;
import java.util.Date;
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 User.
* @see org.jock.hibernate.model.User
* @author MyEclipse - Hibernate Tools
*/
public class UserDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(UserDAO.class);
//property constants
public static final String FNAME = "fname";
public static final String LNAME = "lname";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String AUTH = "auth";
public static final String GRADEID = "gradeid";
public static final String EMAIL = "email";
public static final String FLAG = "flag";
public static final String TEL = "tel";
public void save(User transientInstance) {
log.debug("saving User instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public User findById( java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getSession()
.get("org.jock.hibernate.model.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(User instance) {
log.debug("finding User instance by example");
try {
List results = getSession()
.createCriteria("org.jock.hibernate.model.User")
.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 User instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from User 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 findByFname(Object fname) {
return findByProperty(FNAME, fname);
}
public List findByLname(Object lname) {
return findByProperty(LNAME, lname);
}
public List findByUsername(Object username) {
return findByProperty(USERNAME, username);
}
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public List findByAuth(Object auth) {
return findByProperty(AUTH, auth);
}
public List findByGradeid(Object gradeid) {
return findByProperty(GRADEID, gradeid);
}
public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
public List findByFlag(Object flag) {
return findByProperty(FLAG, flag);
}
public List findByTel(Object tel) {
return findByProperty(TEL, tel);
}
public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
import java.util.Date;
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 User.
* @see org.jock.hibernate.model.User
* @author MyEclipse - Hibernate Tools
*/
public class UserDAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(UserDAO.class);
//property constants
public static final String FNAME = "fname";
public static final String LNAME = "lname";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
public static final String AUTH = "auth";
public static final String GRADEID = "gradeid";
public static final String EMAIL = "email";
public static final String FLAG = "flag";
public static final String TEL = "tel";
public void save(User transientInstance) {
log.debug("saving User instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public User findById( java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getSession()
.get("org.jock.hibernate.model.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(User instance) {
log.debug("finding User instance by example");
try {
List results = getSession()
.createCriteria("org.jock.hibernate.model.User")
.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 User instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from User 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 findByFname(Object fname) {
return findByProperty(FNAME, fname);
}
public List findByLname(Object lname) {
return findByProperty(LNAME, lname);
}
public List findByUsername(Object username) {
return findByProperty(USERNAME, username);
}
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public List findByAuth(Object auth) {
return findByProperty(AUTH, auth);
}
public List findByGradeid(Object gradeid) {
return findByProperty(GRADEID, gradeid);
}
public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
public List findByFlag(Object flag) {
return findByProperty(FLAG, flag);
}
public List findByTel(Object tel) {
return findByProperty(TEL, tel);
}
public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}
2006/11/02 22:57
设计细颗粒度的持久类并且使用<component>来实现映射。
使用一个Address持久类来封装 street, suburb, state, postcode. 这将有利于代码重用和简化代码重构(refactoring)的工作。
对持久类声明标识符属性( identifier properties)。
Hibernate中标识符属性是可选的,不过有很多原因来说明你应该使用标识符属性。我们建议标识符应该是“人造”的(自动生成,不涉及业务含义)。
使用自然键(natural keys)标识
对所有的实体都标识出自然键,用<natural-id>进行映射。实现equals()和hashCode(),在其中用组成自然键的属性进行比较。
为每个持久类写一个映射文件
不要把所有的持久类映射都写到一个大文件中。把 com.eg.Foo 映射到com/eg/Foo.hbm.xml中, 在团队开发环境中,这一点显得特别有意义。
把映射文件作为资源加载
把映射文件和他们的映射类放在一起进行部署。
考虑把查询字符串放在程序外面
如果你的查询中调用了非ANSI标准的SQL函数,那么这条实践经验对你适用。把查询字符串放在映射文件中可以让程序具有更好的可移植性。
使用绑定变量
就像在JDBC编程中一样,应该总是用占位符"?"来替换非常量值,不要在查询中用字符串值来构造非常量值!更好的办法是在查询中使用命名参数。
不要自己来管理JDBC connections
Hibernate允许应用程序自己来管理JDBC connections,但是应该作为最后没有办法的办法。如果你不能使用Hibernate内建的connections providers,那么考虑实现自己来实现org.hibernate.connection.ConnectionProvider
考虑使用用户自定义类型(custom type)
假设你有一个Java类型,来自某些类库,需要被持久化,但是该类没有提供映射操作需要的存取方法。那么你应该考虑实现org.hibernate.UserType接口。这种办法使程序代码写起来更加自如,不再需要考虑类与Hibernate type之间的相互转换。
使用一个Address持久类来封装 street, suburb, state, postcode. 这将有利于代码重用和简化代码重构(refactoring)的工作。
对持久类声明标识符属性( identifier properties)。
Hibernate中标识符属性是可选的,不过有很多原因来说明你应该使用标识符属性。我们建议标识符应该是“人造”的(自动生成,不涉及业务含义)。
使用自然键(natural keys)标识
对所有的实体都标识出自然键,用<natural-id>进行映射。实现equals()和hashCode(),在其中用组成自然键的属性进行比较。
为每个持久类写一个映射文件
不要把所有的持久类映射都写到一个大文件中。把 com.eg.Foo 映射到com/eg/Foo.hbm.xml中, 在团队开发环境中,这一点显得特别有意义。
把映射文件作为资源加载
把映射文件和他们的映射类放在一起进行部署。
考虑把查询字符串放在程序外面
如果你的查询中调用了非ANSI标准的SQL函数,那么这条实践经验对你适用。把查询字符串放在映射文件中可以让程序具有更好的可移植性。
使用绑定变量
就像在JDBC编程中一样,应该总是用占位符"?"来替换非常量值,不要在查询中用字符串值来构造非常量值!更好的办法是在查询中使用命名参数。
不要自己来管理JDBC connections
Hibernate允许应用程序自己来管理JDBC connections,但是应该作为最后没有办法的办法。如果你不能使用Hibernate内建的connections providers,那么考虑实现自己来实现org.hibernate.connection.ConnectionProvider
考虑使用用户自定义类型(custom type)
假设你有一个Java类型,来自某些类库,需要被持久化,但是该类没有提供映射操作需要的存取方法。那么你应该考虑实现org.hibernate.UserType接口。这种办法使程序代码写起来更加自如,不再需要考虑类与Hibernate type之间的相互转换。





下载文件

