I'm getting following error
java.lang.NoSuchMethodError: org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V
org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1173)
org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:248)
when execute the following hql
select object(o),p.dateApply,p.reason,p.Approver from Person as o "+
" com.jpa.entities.Application as p where o.id = p.nricNo and o.batchNumber= :batchNo
if i remove the following query
p.dateApply,p.reason,p.Approver
it's working just fine. The thing i want is to return value from both Person entities and Application entities.
Hope somebody can assist me as I'm new to HQL.
It sounds like you've got either incompatible Hibernate-related JARs, or duplicates of some or all of those JARs. Perhaps because you upgraded to a different version of Hibernate without deleting the old JARs (or something similar).
If different versions of the class com.my.MyClass are found in both lib1.jar and lib2.jar and the classloader loads MyClass from lib1.jar, but the "correct" MyClass (the one with the method the JRE is looking for) is found in lib2.jar, you'll experience exactly the problem you're seeing.
Related
I upgraded my codebase from Grails 2.1.0 to 3.2.0. However, I encountered a situation that one of my queries did not work as expected.
public List<Location> findAllLocationsByNames(Collection<String> placeNames) {
return Location.executeQuery("select l from Location l where l.placeName in (:placeNames)", [placeNames: placeNames])
}
Before upgrade, this query was working well. I passed in a collection of type LinkedKeySet (from HashMap.getKeySet()) and it returns correctly the list of locations. But now with new version of Grails I got this error:
java.util.LinkedHashMap$LinkedKeySet cannot be cast to java.lang.String
I digged a bit deeper inside Grails and GORM and saw that QueryTranslatorImpl translated the named parameter placeNames to SQL AST as [NAMED_PARAM] ParameterNode: '?' {name=placeNames, expectedType=org.hibernate.type.StringType#49c72fb7}, but I'm not sure why.
In the end, I changed the original query to use where and DetachedCriteria:
public List<Location> findAllLocationsByNames(Collection<String> placeNames) {
return Location.where {placeName in placeNames}.list()
}
This time, everything works fine and the returned result was what I expected.
What is the problem with the first query using executeQuery?
Seems newer versions of Hibernate are less lenient if you specify parameters that are not of type java.util.List and instead are other collection types (Set etc.)
I'd like to create a small app, that uses Ebean as ORM. I've a little problem with the Ebean, and the enhancement, so I created a minimal example, that can be viewed here: https://github.com/vilmosnagy/ebean-example
It's a small Maven project, with one Entity, and one Dao. I wrote a test, where I'd like to save the entity into DB. (The DB is H2 in this example, 'cause to be small. I tried it with MySQL, but that didn't work either.)
When I run mvn clean install, I see the following lines in the output:
transform> pkg: com/example/entites
transform> cls: com/example/entites/BaseEntity msg: interfaces: entityInterface[false] transactional[false]
transform> cls: com/example/entites/BaseEntity msg: performing entity transform
transform> cls: com/example/entites/BaseEntity msg: ... adding intercept <init> in CONSTRUCTOR:()V OWNER/SUPER:java/lang/Object
transform> cls: com/example/entites/BaseEntity msg: adding equals() hashCode() and _ebean_getIdentity() with Id field id index:0 primative:false
transform> cls: com/example/entites/BaseEntity msg: enhanced
So, I think, my entity is enhanced succefully.
But when the tests run, I got the following exception:
Tests in error:
test_save(com.example.daos.BaseDaoTest): The type
[class com.example.entites.BaseEntity] is not a registered entity? If you don't
explicitly list the entity classes to use Ebean will search for them in the
classpath. If the entity is in a Jar check the ebean.search.jars property in
ebean.properties file or check ServerConfig.addJar().
I cannot find any solution. What am I missing?
Thanks!
We are running an app in jboss 5.1.0.GA, and it's complaining when we try to use the EntityManager.createQuery method. An implementation of this method is available in org.hibernate.ejb.AbstractEntityManagerImpl (and probably a few other places included in our code).
I suspect that it's picking up an older version of hibernate from the jboss libs directory.
How can I fix this?
Code looks something like this:
List<UserGroup> userGroups= DbHelper.getNonNullEntityManager().createQuery("SELECT ug FROM UserGroup ug", UserGroup.class).getResultList();
Exception is this:
java.lang.NoSuchMethodError: javax.persistence.EntityManager.createQuery(Ljava/lang/String;Ljava/lang/Class;)Ljavax/persistence/TypedQuery;
Ok I fixed it by removing the Class parameter from the method. Seems that was added in a later method of hibernate:
List<UserGroup> userGroups= DbHelper.getNonNullEntityManager().createQuery("SELECT ug FROM UserGroup ug").getResultList();
I have a simple query that worked for a good time, now i changed some stuff in my code to:
(hibernate.cfg.xml)
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
and the following code:
public UserEn findByIDFetch(Long id, String... fetches) {
beginTransaction();
DetachedCriteria dc = DetachedCriteria.forClass(UserEn.class);
for (String fetch : fetches) {
dc.createAlias(fetch, fetch, JoinType.INNER_JOIN);
}
dc.add(Restrictions.eq("id", id));
UserEn result = (UserEn) dc.getExecutableCriteria(getSession()).uniqueResult();
commitTransaction();
return result;
}
after the commit, result object don't have the list loaded (that is comming from fetches)
if i do result.getAnyList().size(); before the commit, it keeps loaded, also, if i use HQL it works perfectly. (but it's not how it's supposed to work (let it open and load when used), createAlias should work fine as always)
It looks like DetachedCriteria's createAlias is not fetching the given path (in this case is characterEnList)
#EDIT
I found out that if i use setFetchMode in the desired path (characterEnList in this case) the fetching works but if i use createAlias (as usual) it simply stops working, i don't know how, probably a hibernate bug or something, anyway, i'll wait for someone answer, maybe someone that had the same problem...
it is lazy loaded only when accessed during the transaction. So you must call the lazy loaded field during the transaction, for hibernate to fetch it. It will then be available in the view.
or of course make it eager.
Found as the same problem as follows: https://hibernate.atlassian.net/browse/HHH-7842
This is probably a bug, if i ask it to be inner join in the alias, it should be, i can do this with HQL, why can't i do with Criteria?
So, now i will have to use the LEFT_OUTER_JOIN type to solve this, or don't use alias at all (if i don't need to make a where clause or 2-depth fetch)
Hope it help someone, thanks for all your help!
example
dc.createAlias(fetch, fetch, JoinType.LEFT_OUTER_JOIN);
OR
dc.setFetchMode(fetch, FetchMode.JOIN);
While working on some java projects i've saw some sort of SQL repository.
The idea was to place all queries in one(or few) xml files and retrieve them when needed by name. Something like this:
String sql = getSQLRepository().getSQL("SELECT_ALL_ROWS", params)
String sql2 = getSQLRepository().getSQL("SELECT_ROWS_WITH_COND", params)
In my current Grails project i have a lot of HQL queries in dozens of classes and it's hard to track them all. It seems that HQL repository would be very nice solution.
So could anyone tell if some sort of SQL\HQL repository implementation allready present or there are better solutions present ?
Have a look at Mapping Queries in Hibernate reference.
After we started use the http://source.mysema.com/display/querydsl there is no need to think about text queries and how to manage them.
I'd recommend you to use the good old properties files. You can put them into your classpath and then use as following:
Properties sql = new Properties();
properties.load(getClass().getResourceAsStream("sql.properties"));
////////
String query = sql.get("SELECT_ALL_ROWS");
I'm sorry, and it doesn't relate to Hibernate, but when I worked with iBatis, - there are the situation as you are writing about exactly. A few xml (partially generated itself) files, containing SQL queries, which was easily to use in DAO