hibernate detached criteria - How to add a native sql statement - mysql

Hi I have very big problem. I have a DetachedCriteria and I named it dc. I declared it this way DetachedCriteria dc = getDetachedCriteria(). I want to add a collation statement before the order by. The purpose of collation is to handle ñ. The statement I want to add is COLLATE utf8_spanish_ci. I did it this way dc.add(Restrictions.sqlRestriction(" COLLATE utf8_spanish_ci ")). Of course I got an error because this is wrong. I don't know to do it. Please help.

You can execute native SQL queries in order to take advantage of your particular database features, this is how is done in hibernate using detached criteria...
List<YourEntity> list = (List<YourEntity>) yourEntityDAO.getHibernateTemplate().execute(
new HibernateCallback() {
#Override
public Object doInHibernate(Session session) throws HibernateException {
SQLQuery sq = session.createSQLQuery("SELECT * FROM MY_TABLE");
return sq.addEntity(YourEntity.class).list();
}
});

Related

generic upper case search on postgres and mysql not working

I am trying to do an easy search on a table that can be on any kind of database. The following query is working an the most databases, but I cannot find a solution which works on mysql.
The tables in my database are generated by the active objects framework, so I cannot change the names or config of those instances.
Here is the query that works fine on all databases but MySQL:
select * from "AO_69D057_FILTER" where "SHARED" = true AND "CONTAINS_PROJECT" = true AND UPPER("FILTER_NAME") like UPPER('%pr%').
MySql is not able to use the table name in double quotes for some reason. If I use the unquoted table name it works on MySQL but not on Postgres. Postgres is converting the table name to lowercase because it is unquoted. AO is generating the table names in upper case.
I also tried to use an alias, but that can not work because of the evaluation hierarchy of the statement.
Any suggestions how to get rid of the table name problem?
By default double quotes are used to columns.
You can change it:
SET SQL_MODE=ANSI_QUOTES;
Here is the documentation about it:
http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html
I had the same problem. I select the query according to the exception I get. In the first call of the db search, I try without quotes if it fails then I try with quotes. Then I set useQueryWithQuotes variable accordingly so that in future calls I do not need to check the exception. Below is the code snipped I am using.
private Boolean useQueryWithQuotes=null;
private final String queryWithQuotes = "\"OWNER\"=? or \"PRIVATE\"=?";
private final String queryWithoutQuotes = "OWNER=? or PRIVATE=?";
public Response getReports() {
List<ReportEntity> reports = null;
if(useQueryWithQuotes==null){
synchronized(this){
try {
reports = new ArrayList<ReportEntity>( Arrays.asList(ao.find(ReportEntity.class, Query.select().where(queryWithoutQuotes, getUserKey(), false))) );
useQueryWithQuotes = false;
} catch (net.java.ao.ActiveObjectsException e) {
log("exception:" + e);
log("trying query with quotes");
reports = new ArrayList<ReportEntity>( Arrays.asList(ao.find(ReportEntity.class, queryWithQuotes, getUserKey(), false)));
useQueryWithQuotes = true;
}
}
}else{
String query = useQueryWithQuotes ? queryWithQuotes : queryWithoutQuotes;
reports = new ArrayList<ReportEntity>( Arrays.asList(ao.find(ReportEntity.class, query, getUserKey(), false)));
}
...
}

EclipseLink fails to fetch a scalar Boolean value

The following JPA criteria query succeeds on Hibernate (4.2.7 final).
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Boolean>criteriaQuery=criteriaBuilder.createQuery(Boolean.class);
Root<UserTable> root = criteriaQuery.from(entityManager.getMetamodel().entity(UserTable.class));
criteriaQuery.multiselect(root.get(UserTable_.enabled));
ParameterExpression<String>parameterExpression=criteriaBuilder.parameter(String.class);
criteriaQuery.where(criteriaBuilder.equal(criteriaBuilder.lower(criteriaBuilder.trim(root.get(UserTable_.emailId))), criteriaBuilder.lower(criteriaBuilder.trim(parameterExpression))));
List<Boolean> list = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, "admin").getResultList();
for(Boolean o:list)
{
System.out.println("enabled : "+o);
}
It is simply meant to return a scalar Boolean value from MySQL database. The corresponding column is of type TINYINT(1) in MySQL.
It generate the following SQL statement.
SELECT
usertable0_.enabled as col_0_0_
FROM
social_networking.user_table usertable0_
WHERE
lower(trim(BOTH FROM usertable0_.email_id))=lower(trim(BOTH FROM ?))
The same query fails on EclipseLink (2.5.1) in which case it returns nothing (no error, no exception). It however, generates a correct SQL statement as follows.
SELECT enabled
FROM projectdb.user_table
WHERE (LOWER(TRIM(email_id)) = LOWER(TRIM(?)))
bind => [admin]
The corresponding JPQL like so,
SELECT u.enabled
FROM UserTable u
WHERE lower(trim(u.emailId))=lower(trim(:emailId))
also doesn't get the value in question.
It however, works with additional columns as shown below.
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]>criteriaQuery=criteriaBuilder.createQuery(Object[].class);
Root<UserTable> root = criteriaQuery.from(entityManager.getMetamodel().entity(UserTable.class));
criteriaQuery.multiselect(root.get(UserTable_.enabled), root.get(UserTable_.firstName));
ParameterExpression<String>parameterExpression=criteriaBuilder.parameter(String.class);
criteriaQuery.where(criteriaBuilder.equal(criteriaBuilder.lower(criteriaBuilder.trim(root.get(UserTable_.emailId))), criteriaBuilder.lower(criteriaBuilder.trim(parameterExpression))));
List<Object[]> list = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, userName).getResultList();
One extra column root.get(UserTable_.firstName) is added and the return type of CriteriaQuery is changed from CriteriaQuery<Boolean> to CriteriaQuery<Object[]>.
The column is defined as follows in the corresponding entity.
#Basic(optional = false)
#NotNull
#Column(name = "enabled")
private Boolean enabled; //Getter and setter.
Why doesn't it given a Boolean value in EclipseLink?
This is due to EclipseLink bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=340089 which causes EclipseLink to be unable to distinguish the value returned from the SQL query from the construct it uses internally to indicate there were no results. Selecting another value is the only workaround, but it seems simple enough that not many people hit it or just workaround it without commenting or voting for the bug.

Hibernate, jpa and mysql

I'm having a small problem with my insert statements in Spring-data/jpa/hibernate/mysql/junit application. I created a small unit test to see if i can do a simple INSERT statement.
This is what i have:
#Test
public void testAddAssignment() {
Assignment assignment = new Assignment();
assignment.setCharacter("Z");
assignment.setType(1);
assignmentController.addAssignment(assignment);
assignment = assignmentController.findAssignmentById(16);
assertEquals(16, assignment.getId());
assertEquals(1, assignment.getType());
assertEquals("Z", assignment.getCharacter());
}
When i try to execute this I get the following error:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.byronvoorbach.domain.Assignment]; SQL [insert into Assignment (CHARACTER, TYPE) values (?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.byronvoorbach.domain.Assignment]
And this is my addAssignment(assigment);
#Transactional
public Assignment addAssignment(Assignment assignment) {
return repository.save(assignment);
}
And this is my repository:
#Transactional(readOnly = true)
public interface AssignmentRepository extends JpaRepository<Assignment, Long> {
public List<Assignment> findByType(int type);
}
Since i never wrote an sql query I'm unable to find where the query gets messed up.
Does anyone have an idea?
(Let me know if you need more source code or my .xml files)
EDIT :
Thanks to JB Nizet and fmucar I was able to fix this problem. I changed the column names and now it works!! Apperantly there are a few limitations to column naming which I wasn't aware of.
Try using something other than character and type as column names, because these are probably reserved keywords in SQL.
CHARACTER and TYPE are keywords for sql.
Try not to use keywords as columns names. That may be the problem.

MYSQL case sensitive search (using hibernate) for utf8

I have Login Table that have utf8 charset and utf8 collation when I want check user name and retrieve other information for this specific user name the hql query give me the same result with lowercase and uppercase.
what should l do for my HQL query that work case sesitive
I use Mysql 5 and java hibernarte
this is my query:
return queryManager.executeQueryUniqueResult("select b.login from BranchEntity b where b.userName = ?", username);
The easiest way is to change your column's definition to use case-insensitive collation like utf8_bin.
Details are here
Add class
public class CollateDialect extends MySQLDialect {
#Override
public String getTableTypeString() {
return " COLLATE utf8_bin";
}
}
and use it:
cfg.setProperty("hibernate.dialect", "org.kriyak.hbm.CollateDialect");
this will make all queries case-sensitive that makes much more sence.

JPA native query for LONGTEXT field in a MySQL view results in error

I have the following JPA SqlResultSetMapping:
#SqlResultSetMappings({
#SqlResultSetMapping(name="GroupParticipantDTO",
columns={
#ColumnResult(name="gpId"),
#ColumnResult(name="gpRole"),
// #ColumnResult(name="gpRemarks")
}
)
Which is used like this:
StringBuilder sbQuery = new StringBuilder("Select ");
sbQuery.append(" gpId, ");
sbQuery.append(" gpRole, ");
// sbQuery.append(" gpRemarks ");
sbQuery.append(" FROM v_group_participants_with_details ");
Query query = em.createNativeQuery(sbQuery.toString(), "GroupParticipantDTO");
The view is like this:
DROP VIEW IF EXISTS `v_group_participants_with_details`;
CREATE VIEW `v_group_participants_with_details`
AS
SELECT
gp.id AS gpId,
gp.role AS gpRole,
gp.remarks AS gpRemarks
FROM GroupParticipation gp
;
The GroupParticipation table has the remarks column defined as LONGTEXT (I'm using Mysql 5.x)
Now for the problem:
When the remarks field is commented out from the query everything works perfectly, but if I try to include the remarks field in the query, I get the following error:
javax.persistence.PersistenceException: org.hibernate.MappingException:
No Dialect mapping for JDBC type: -1
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)
What gives? How can I get a LONGTEXT column from a native query?
This problem is reported in HHH-1483 and HHH-3892. In short, Hibernate does not know, how to map a LONGVARCHAR column returned by a native query.
This problem is fixed in Hibernate 3.5.0+. For previous versions, a workaround would be to extend the MysqlDialect to register the correct Hibernate Type for a LONGVARCHAR:
import java.sql.Types;
import org.hibernate.Hibernate;
public class MyMySQL5Dialect extends org.hibernate.dialect.MySQL5Dialect {
public MyMySQL5Dialect() {
super();
// register additional hibernate types for default use in scalar sqlquery type auto detection
registerHibernateType(Types.LONGVARCHAR, Hibernate.TEXT.getName());
}
}