java 如何使用 Hibernate 通过修剪空间从数据库获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25075281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from DB by trim spaces using Hibernate?
提问by Md Aslam
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 {
@Table(name = "uro_user_roles") 公共类 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
现在,当我使用以下 Hibernate 函数提供 roleName 时,我获得了数据库数据,即
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
这里我的问题是当我给出确切的名称时,它只返回对象,因为它区分大小写。我的意思是如果表数据在其值之前有一些空间,那么它不会返回。那么如何通过给定没有空格和大小写敏感的名称来获取数据。在休眠中是否有任何选项可以通过消除空格从数据库获取数据?如果有一个选项,则无需编写您要求的 Trim() 方法。请任何人帮忙
回答by Vlad Mihalcea
You can use an SQL Restriction:
您可以使用 SQL 限制:
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(...))
.
这适用于 MySQL,但并非所有数据库都有TRIM()
功能。其他数据库有LTRIM()
,RTRIM()
所以你必须像LTRIM(RTRIM(...))
.
回答by Alexandre Santos
Do a lower case on the field before comparing. I am assuming the field is never null.
在比较之前在字段上做一个小写。我假设该字段永远不会为空。
public UserRole findByRoleName(String roleName) {
UserRole userPermission = (UserRole)
criteria().setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.add(eq("rolename", roleName.trim().toLowerCase())).uniqueResult();
return userPermission;
}