Hibernate中的小写注释
在hibernate中是否有任何方法可以将实体的列注释为字符串小写? 我的意思是例如
@Entity public class User { @Column private String username; }
我希望hibernate在没有绑定到特定数据库的所有查询中将用户名转换为小写。
您可以为此字符串编写自己的UserType
public class LowerCaseString implements UserType { //.... public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException { Hibernate.STRING.nullSafeSet(preparedStatement, (value != null) ? ((String)value).toLowerCase() : null, index); } ///.... }
你的实体
@Entity public class User { @Column @Type(type="com.youcompany.LowerCaseString") private String username; }
例如,您可以在setter中创建它:
public void setUsername(String username) { this.username = username.toLowerCase(); }
或者使用interseptor:
public boolean onSave(Object entity,Serializable id, Object[] state,String[] propertyNames,Type[] types) throws CallbackException { if (entity instanceof User ){ entity.username = usename.toLowerCase(); } return false; }
你可以在这里阅读更多关于拦截器的信息: http : //www.mkyong.com/hibernate/hibernate-interceptor-example-audit-log/
- LDAP:错误代码50 – 由于访问权限不足,无法添加条目uid = test @ info.com,ou = People,o = Drive,dc = company,dc = com
- 通过JPA注释更改类层次结构分支中的inheritance策略
- 如何将用户创建的连接传递给hibernate
- JPAinheritanceentitymanager.find生成ClassCastException
- IllegalArgumentException:Type不能为null
- Hibernate事务未成功启动
- Hibernate版本?
- 将Excel数据转换为MySql表
- JPA实体inheritance与抽象类 – ConstrainViolationException