Hibernate中的小写注释 [英] Lowercase annotation in Hibernate

查看:119
本文介绍了Hibernate中的小写注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法在hibernate中注释实体的字符串以小写字母?我的意思是例如

  @Entity 
public class User {
@Column
private String用户名;

$ / code>

我希望hibernate能够将所有查询中的用户名转换为小写一个特定的数据库。

解决方案

您可以编写自己的 UserType string

  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)
私人字符串用户名;
}


Is there any way in hibernate to annotate the column of the entity to string lowercase? I mean for example

@Entity  
public class User {  
    @Column  
    private String username;  
}

I want the hibernate to convert the username to lowercase in all queries not tied to a specific database.

解决方案

You can write your own UserType for this string

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);
         }  
    ///....  
}  

you entity

@Entity  
public class User {  
    @Column  
    @Type(type="com.youcompany.LowerCaseString")
    private String username;  
}

这篇关于Hibernate中的小写注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆