如何使用Hibernate通过修剪空间从数据库获取数据? [英] How to get data from DB by trim spaces using Hibernate?

查看:106
本文介绍了如何使用Hibernate通过修剪空间从数据库获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个如下控制器:

  @RequestMapping(value =find / {roleName},method = GET)
public UserRole getByRoleName(@PathVariable(roleName)String roleName){
UserRole userRole = userRoleService.findByRoleName(roleName);
返回userRole;
}
UserRole不过是如下所示,如下所示:

@Entity

@Table(name =uro_user_roles)
public class UserRole {

  / *属性* / 
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name =uro_role_id)
private Integer roleId;

@Column(name =uro_role_name)
private String roleName;

@Column(name =uro_create_user)
private String createUser;

@Column(name =uro_active)
private String createActive;

/ * Getter / Setters * /

现在我得到了DB数据当我通过使用下面的Hibernate函数给出roleName时,例如:

preublic $ UserRole findByRoleName(String roleName){
UserRole userPermission =(UserRole)
criteria()。setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(eq(roleName,roleName))。uniqueResult();
返回userPermission;
}

这里我的问题是当我给出确切的名字时,只有它返回对象因为它的大小写敏感。我的意思是如果表数据在它的值之前有一些空间,那么它不会返回。那么如何通过给定一个没有空间的名字来区分数据并且区分大小写。在休眠中有没有任何选项可以从数据库中获取数据并消除空格?如果有一个选项,那么就不需要写一个y所要求的Trim()方法。您可以使用SQL限制:

<$ p

$ UserName $ {$ b $ UserName $ {$ b $ UserName} $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。 sqlRestriction(TRIM(LOWER({alias} .roleName))=?,roleName.trim()。toLowerCase()),StringType.INSTANCE))
.uniqueResult();
返回userPermission;



$ b $ p
$ b这可以在MySQL中使用,但不是所有的数据库都有一个 TRIM()函数。其他DB有 LTRIM() RTRIM(),所以你必须把它称为 LTRIM(RTRIM(...))


I written a controller such that is following

  @RequestMapping(value="find/{roleName}", method=GET)
    public UserRole getByRoleName(@PathVariable("roleName") String roleName){ 
     UserRole userRole = userRoleService.findByRoleName(roleName);
        return userRole;   
    }
UserRole is nothing but that is given below as shown that

 @Entity

@Table(name = "uro_user_roles") public class UserRole {

/* Properties */
@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "uro_role_id")
private Integer roleId;

@Column(name = "uro_role_name")
private String roleName;

@Column(name = "uro_create_user")
private String createUser;

@Column(name = "uro_active")
private String createActive;

/* Getter / Setters */ 

Now i got the DB data when i give the roleName by using the following Hibernate function such thats is

     public UserRole findByRoleName(String roleName) {
     UserRole userPermission = (UserRole)  
      criteria().setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                .add(eq("roleName", roleName)).uniqueResult();
        return userPermission;
    }

Here my problem is when i give the exact name then only it return the object because its case sensitive.I mean if table data have some space before its value then it doesn't return. So how to get the data by given a name without space and case sensitive.Is there any option in hibernate to get data from DB with eliminating the spaces? If there is a option then no need to write a Trim() method that's y asking. plz anybody help

解决方案

You can use an SQL Restriction:

public UserRole findByRoleName(String roleName) {
    UserRole userPermission = (UserRole) criteria()
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .add(Restrictions.sqlRestriction("TRIM(LOWER({alias}.roleName)) = ?", roleName.trim().toLowerCase()), StringType.INSTANCE))
            .uniqueResult();
    return userPermission;
}

This works in MySQL but not all databases have a TRIM() function. Other DB have LTRIM() and RTRIM() so you'd have to call it like LTRIM(RTRIM(...)).

这篇关于如何使用Hibernate通过修剪空间从数据库获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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