I am currently working on a project to transfer some legacy jdbc select statements over to using Hibernate and it's criteria api.
The two relevant table columns and the SQL query looks like:
-QUERIES-
primaryId
-QUERYDETAILS-
primaryId
linkedQueryId -> Foreign key references queries.primaryId
value1
value2
select *
from queries q
where q.primaryId not in (SELECT qd.linkedQueryId
FROM querydetails qd
WHERE (qd.value1 LIKE 'PROMPT%'
OR qd.value2 LIKE 'PROMPT%'));
My entity relationships look like:
#Table("queries")
public class QueryEntity{
#Id
#Column
private Long primaryId;
#OneToMany(targetEntity = QueryDetailEntity.class, mappedBy = "query", fetch = FetchType.EAGER)
private Set<QueryDetailEntities> queryDetails;
//..getters/setters..
}
#Entity
#Table(name = "queryDetails")
public class QueryDetailEntity {
#Id
#Column
private Long primaryId;
#ManyToOne(targetEntity = QueryEntity.class)
private QueryEntity query;
#Column(name="value1")
private String value1;
#Column(name="value2")
private String value2;
//..getters/setters..
}
I am attempting to utilize the criteria api in this way:
Criteria crit = sessionFactory.getCurrentSession().createCriteria(QueryEntity.class);
DetachedCriteria subQuery = DetachedCriteria.forClass(QueryDetailEntity.class);
LogicalExpression hasPrompt = Restrictions.or(Restrictions.ilike("value1", "PROMPT%"),
Restrictions.ilike("value2", "PROMPT%"));
subQuery.add(hasPrompt);
Criterion subQueryCrit = Subqueries.notIn("queryDetails", subQuery);
crit.add(subQueryCrit);
List<QueryMainEntity> entities = (List<QueryMainEntity>) crit.list();
System.out.println("# of results = " + entities.size());
I am getting a NullPointerException on the crit.list() line that looks like
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getProjectedTypes(CriteriaQueryTranslator.java:362)
at org.hibernate.criterion.SubqueryExpression.createAndSetInnerQuery(SubqueryExpression.java:153)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:69)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:114)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:83)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1687)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
Now, I think its pretty safe to say I'm using the Criteria Api/Detached Query Api incorrectly, but I'm not sure what the 'correct' way to do it is since the Hibernate Docs only briefly cover criteria api subqueries.
I realize this is a pretty long question, but I figure its appear to put it all the relevant aspects of the question (query I'm attempting to represent via Criteria API, tables, entities).
Give this a shot:
DetachedCriteria d = DetachedCriteria.forClass(QueryDetailEntity.class, "qd");
d.setProjection(Projections.projectionList().add(Projections.property("qd.query")));
d.add(Restrictions.or(Restrictions.like("qd.value1", "PROMPT%"), Restrictions.like("qd.value2", "PROMPT%")));
criteria = session.createCriteria(QueryEntity.class, "q");
criteria.add(Subqueries.propertyNotIn("q.primaryId", d));
criteria.list();
The use of the following are property names, not column names:
qd.query
qd.value1
qd.value2
q.primaryId
As a side note, if this is not a dynamically generated query, have you given thought to using HQL instead?
Related
I have the next entity:
#Entity
#Table(name = "search_request_items")
public class SearchRequestItem extends LongIdEntity {
#Column(name = "date")
private Instant date;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "user_id")
private User user;
#Column(name = "result_count")
private Long resultCount;
/**
* Request's text.
*/
#Column(name = "request")
private String request;
/**
* Request's quality. It may take 0 or 1.
*/
#Column(name = "quality")
private Integer quality;
...
}
Then i have the next queryDSL query, which return collection of quality avg and user count grouped by request' text:
public JPAQuery<Tuple> prepareTotalQuery() {
QSearchRequestItem requestItem = QSearchRequestItem.searchRequestItem;
QUser user = QUser.user;
NumberExpression<Double> qualityAvgExpression = requestItem.quality.avg();
NumberExpression<Long> qualityCountExpression = requestItem.user.countDistinct();
JPAQuery<Tuple> query = queryFactory
.select(qualityAvgExpression, qualityCountExpression)
.from(requestItem)
.leftJoin(requestItem.user, user)
.groupBy(requestItem.request)
.having(qualityAvgExpression.isNotNull(),
qualityCountExpression.gt(2));
return query;
}
But i need to return total avg upon this collection just like this native query:
select avg(n1.avg_quality)
from (select count(distinct user_id), avg(quality) as avg_quality
from search_request_items
group by request
having avg(quality) is not null and count(distinct user_id) > 2
) n1;
So, how to update my querydsl query to get this result?
This issue here is that you're using JPA and JPA doesn't allow to use subqueries as join target in the from clause.
Blaze-Persistence is an extension of JPA and integrates well with Hibernate. It adds Common Table Expressions and subselect (even lateral) joins to JPQL. Blaze-Persistence also has a Querydsl integration, allowing you to write a query like the following:
List<Number> fetch = new BlazeJPAQuery<>(entityManager, cbf)
.with(cteType, new BlazeJPAQuery<>()
.bind(cteType.avgQuantity, requestItem.quality.avg())
.from(requestItem)
.leftJoin(requestItem.user, user)
.groupBy(requestItem.request)
.having(qualityAvgExpression.isNotNull(), qualityCountExpression.gt(2))))
)
.select(cteType.avgQuantity.avg())
.from(cteType)
.fetch();
However, with plain JPA and Hibernate, there is no simple way to do this.
Provided that you're only averaging a set of numbers though, which are not intensive to serialize over JDBC and do not suffer from potential N+1 issues, I'd suggest to simply do the final average step in memory:
queryFactory
.select(qualityCountExpression)
.from(requestItem)
.leftJoin(requestItem.user, user)
.groupBy(requestItem.request)
.having(qualityAvgExpression.isNotNull(),
qualityCountExpression.gt(2))
.stream()
.collect(Collectors.averagingDouble(i -> i.doubleValue()))
Usually during my work hours i spend a lot of time querying the db(oracle) and parsing blob from various table where the streams that we receive are stored.
There are various type of stream so i was trying to made a simple webapp where i write the select statement and it returns all the stream parsed accordingly.
My problem is that using jpa and executing the simple native query:
select B_BODY from TABLE_B where TRANSACTION_ID = 'GG-148c-01502790743907855009';
the statement doesn't return anything but querying directly the database return the record.
this is my java code:
#Transactional(readOnly = true)
public List<Object[]> retrieveBlobs(String squery) {
squery = squery + " and rownum <= "+maxResults;
Query query = em.createNativeQuery(squery);
List<Object[]> resultList = query.getResultList();
return resultList;
}
this is the sql generated:
Hibernate:
select
B_BODY
from
TABLE_B
where
TRANSACTION_ID ='GG-148c-01502790743907855009'
and rownum <= 100
i know that this way might seems weird but our team spend a lot of time trying to tokenize the stored streams(the code that identify how to parse the stream is also stored in the tables).Useless to say this application is going to be used only internally.there is a way to just execute the query as it is and retrieve the correct output?
Well, I tried to reproduce your problem on MariaDB (with mysql-connector-java + hibernate) but selecting a lob with native query was working properly.
You can try to create entities which will be holding your blob and check if this would help. Just make a standard entity with #Lob annotation over your lob column.
#Entity
#NamedQueries(
#NamedQuery(name = FIND_ALL, query = "SELECT m FROM LobEntity m")
)
public class LobEntity {
public static final String FIND_ALL = "PhpEntity.findAll";
#Id
#Column(name = "id")
private String id;
#Lob
#Column(name = "lob")
private byte[] lob;
//Use Blob class if you want to use streams.
//#Column(name = "lob")
//#Lob
//private Blob lob;
}
I am getting an error with the following JPQL query:
#NamedQuery (name = "Customer.getById", query =
"SELECT o
FROM bub.Customer o
WHERE o.user_id = :myid")
[bub.Customer is the #Entity name]
This is an excerpt of the error message I'm receiving:
org.hibernate.HibernateException:
Errors in named queries:
Customer.getById\n
Caused by: org.hibernate.HibernateException:
Errors in named queries: Customer.getById
When I remove the WHERE clause Wildfly allows me to deploy my web app so I know there is something wrong wtih my WHERE clause. Specifically since the column name is user_id in my Customer table I believe there may be an issue with the underscore(_) in the JPQL. I've tried changing the WHERE clause to "WHERE o.userId = :myid" but that didn't work either.
How can I fix the WHERE clause so my website will deploy and still work the correct way?
EDIT:
The relevant method is this:
public static Customer getById (final EntityManager em, final long id)
{
return em.createNamedQuery ("Customer.getById", Customer.class).setParameter ("myid", id).getSingleResult ();
}
I don't think this is the issue though.
EDIT2:
It turns out this was the issue:
#ManyToOne (fetch = FetchType.LAZY)
#JoinColumn (name = "user_id")
private User user;
I ended up changing the JPQL query to this and now it's working:
#NamedQuery (name = "Customer.getById", query =
"SELECT o
FROM bub.Customer o
WHERE o.user = :myid")
In JPQL you don't use the column name, but the attribute name(unless you annotate something else)
I thought I understood hibernate's fetching strategies, but it seems I was wrong.
So, I have an namedNativeQuery:
#NamedNativeQueries({
#NamedNativeQuery(
name = "getTest",
resultClass = ArticleOnDate.class,
query = "SELECT `a`.`id` AS `article_id`, `a`.`name` AS `name`, `b`.`price` AS `price` FROM article a LEFT JOIN price b ON (a.id = b.article_id) WHERE a.date <= :date"
)
})
#Entity()
#Immutable
public class ArtikelOnDate implements Serializable {
#Id
#OneToOne
#JoinColumn(name = "article_id")
private Article article;
...
}
Then I call it:
Query query = session.getNamedQuery("getTest").setDate("date", date);
List<ArticleOnDate> list = (List<ArticleOnDate>) query.list();
The query returns thousand of entities... Well, ok, but after that query hibernate queries thousand other queries:
Hibernate:
select
article0_.id as id1_0_0_,
article0_.bereich as name2_0_0_,
price1_.price as price1_14_1_
from
article artikel0_
where
artikel0_.id=?
Ok, that's logic, because the #OneToOne relation is fetched eagerly. I don't want to fetch it lazy, so I want a batch fetching strategy.
I tried to annotate the Article property but it didn't work:
#Id
#OneToOne
#JoinColumn(name = "article_id")
#BatchSize(size=100)
private Article article;
So what can I do to fetch the relation in a batch?
i have declared an entity the following way:
public class MyEntity {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
#Persistent
private String text;
//getters and setters
}
Now I want to retrieve the objects using the id. I tried to manage it from the Google Appengine Data Viewer with "SELECT * FROM MyEntity Where id = 382005" or via a query in a servlet. I get no results returned. But i know for sure that the object with the id exists (i made a jsp which queries all objects in the db and displays them in the db).
So what is wrong in my query? Am I querying the wrong field? The Google Appengine Data Viewer names the field "ID/name" and it has the value "id=382005". Do I have to query with this names? I've tried but it didn't work out :(
You can use below since you are querying using the primary key:
MyEntity yourEntity = entityManager.find(MyEntity.class, yourId);
Note, this should work as well, but it's easier to use find() if you are searching based on the primary key:
Query query = entityManager.createQuery(
"SELECT m FROM MyEntity m WHERE id = :id");
query.setParameter("id", yourId);
MyEntity yourEntity = (MyEntity) query.getSingleResult();