when writing jpql queries I can reference not mapped columns with COLUMN() (I know it is eclipselink specific).
Is there any way I can reference unmapped column when building criteria using javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.Predicate, etc?
The problem I am facing: I have postgresql table with full text search column. I do not want it to be mapped to entity objects but I qould like to use is in queries (and I need also queries using CriteriaBuilder).
Edit: code example
I have a table: X (id integer, name varchar, fts tsvector)
"fts" column is full text search index data.
I need entity class without "fts" attribute, like:
#Entity class X {
#Id Integer id;
#Column String name;
}
so that fts data is never fetched (for performance), but I need to be able to query that column. I can do this with jpql:
SELECT t FROM X t WHERE COLUMN('fts', t) IS NOT NULL;
but how can I reference such column when building criteria like:
Specification<Fts> spec = new Specification<Fts>() {
#Override
public Predicate toPredicate(Root<Fts> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.notNull(root.<String>get("fts"));
}
};
other choice:
How to add attribute in entity class that reference table column but is never fetched? I tried adding #Basic(fetchType = FetchType.LAZY) but it does not work and value is fetched upon query...
Ok. I managed to solve it this way:
Specification<Fts> spec = new Specification<Fts>() {
#Override
public Predicate toPredicate(Root<Fts> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
JpaCriteriaBuilder cb = (JpaCriteriaBuilder) builder;
List<String> args = new ArrayList();
args.add("FTS query");
javax.persistence.criteria.Expression<Boolean> expr = cb.fromExpression(
cb.toExpression(
cb.function("", Boolean.class, cb.fromExpression(((RootImpl) root).getCurrentNode().getField("fts")))
)
.sql("? ## to_tsquery(?)", args)
);
// getField() allows to reference column not mapped to Entity
query.where(expr);
return null; // or return cb.isTrue(expr); and then not need to set query.where()
}
};
Related
I wanna know is there a way to do something like this in hibernate using criteriaBuilder
select users.first_name,orders.payable,order_item.product_title
from "order" orders
join users on orders.user_id_fk = users.id_pk
join order_item on orders.id_pk = order_id_fk
I need this specially if I have to use group by. I search and google and also read this article but have no clue how can I do this in hibernate:
Query Selection Other Than Entities
querycriteria
hibernate-facts-multi-level-fetching
also I code this for selecting field in first layer and it worked perfectly for selecting first layer but it need some change to work with join and select field from other table than root:
<R> List<R> reportEntityGroupBy(List<String> groupBy, List<String> selects, Class<T> root, Class<R> output) {
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<R> criteriaQuery = criteriaBuilder.createQuery(output);
Root<T> rootQuery = criteriaQuery.from(root);
if (selects == null || selects.isEmpty()) {
selects = groupBy;
}
criteriaQuery.multiselect(selects.stream().map(rootQuery::get).collect(Collectors.toList()));
criteriaQuery.groupBy(groupBy.stream().map(rootQuery::get).collect(Collectors.toList()));
I use Hibernate 5.4.22.Final and use entityGraph for my join.
I don't know how your selects look like, but I suppose you are using paths i.e. attributes separated by .. In that case, you have to split the paths and call Path#get for each attribute name.
List<Path> paths = selects.stream()
.map(select -> {
Path<?> path = rootQuery;
for (String part : select.split("\\.")) {
path = path.get(part);
}
return path;
})
.collect(Collectors.toList()));
criteriaQuery.multiselect(paths);
criteriaQuery.groupBy(paths);
I am in the process of internationalizing an existing software using. As part of it I have a table which, reduced to the simplest case, is:
ID AUTONUMERIC
ID_OF_TEXT NUMBER
TEXT VARCHAR
The text column must be translated to new languages (which may or may not be present). So there is another new table with colunmns:
ID_OF_TEXT NUMBER
LANGUAGE_CODE VARCHAR
TRANSLATED_TEXT VARCHAR
There is already an entity to represent the base table and this entity cannot be changed. It's generated code looks like:
class QMyBaseEntity extends EntityPathBase<MyBaseEntity> {
NumberPath<Long> id = createNumber("id", Long.class);
NumberPath<Long> toTranslateId = createNumber("toTranslateId", Long.class);
StringPath text = createString("text");
}
and the generated code for the translations look up table looks like:
#Generated("com.mysema.query.codegen.EntitySerializer")
class QTranslationLookup extends EntityPathBase<TranslationLookup> {
NumberPath<Long> id = createNumber("id", Long.class);
NumberPath<Long> translationId = createNumber("translationId", Long.class);
StringPath languageCode = createString("languageCode");
StringPath translated_text = createString("translated_text");
}
I want the field text of MyBaseEntity to take the value of the translation table (if it exists) instead of its original one. There is already a very complex query that I cannot change except for adding a join like:
String languageCode = "de";
JPAQuery query = new JPAQuery(entityManager);
query.from(qMyBaseEntity);
// add lots of other joins and stuff here
query.leftJoin(qTranslationLookup)
.on(qTranslationLookup.translationId.eq(qMyBaseEntity.toTranslateId)
.and(qTranslationLookup.languageCode.eq(languageCode)));
List<MyBaseEntity> results = query.list(qMyBaseEntity);
With the minimum change possible to the existing code, how do I set the value of the translation in the results list so that the returned entity instances contain the translation in the text column instead of the original value from the old (untranslated) table?
Mysema isn't a framework, QueryDSL is. QueryDSL used to be developed by Mysema, but isn't anymore and the package has since moved to the com.querydsl group id.
You can't override the values of managed properties in an entity projection. The values will always be the actual field values from the entity. Entities cannot be used as DTO's. If you want to project different kinds of expressions, you need to use tuple projection.
List<Tuple> results = query.from(qMyBaseEntity)
.leftJoin(qTranslationLookup)
.on(qTranslationLookup.translationId.eq(qMyBaseEntity.toTranslateId)
.and(qTranslationLookup.languageCode.eq(languageCode)))
.select(qMyBaseEntity, qTranslationLookup.translatedText.coalesce(qMyBaseEntity.text))
.fetch()
Alternatively, you could for example return a mapping:
Map<MyBaseEntity, String> results = query.from(qMyBaseEntity)
.leftJoin(qTranslationLookup)
.on(qTranslationLookup.translationId.eq(qMyBaseEntity.toTranslateId)
.and(qTranslationLookup.languageCode.eq(languageCode)))
.transform(GroupBy.groupBy(qMyBaseEntity).as(
qTranslationLookup.translatedText.coalesce(qMyBaseEntity.text))
.fetch()
Or you could use QueryDSL's DTO projection:
List<ResultDTO> results = query.from(qMyBaseEntity)
.leftJoin(qTranslationLookup)
.on(qTranslationLookup.translationId.eq(qMyBaseEntity.toTranslateId)
.and(qTranslationLookup.languageCode.eq(languageCode)))
.select(Projections.constructor(ResultDTO.class, qMyBaseEntity, qTranslationLookup.translatedText.coalesce(qMyBaseEntity.text))
.fetch()
I try to build a CriteriaQuery which provides the following functionality:
I have three tables with the following fields:
table_a:
id, name_a
table_b:
id, name_b
table_ab:
id_a, id_b
Now I want to get all elements out of table_a ordered by the name_b field of the corresponding element in table_b.
The Result should be a Specification for usage in a JpaRepository. I tried using joins, but i stuck at the point, how to combine the joins:
Specification<TableA> specification = (root, query, cb) -> {
CriteriaQuery<TableAb> abQuery = cb.createQuery(TableAb.class);
CriteriaQuery<TableB> bQuery = cb.createQuery(TableB.class);
Root<TableAb> abRoot = abQuery.from(TableAb.class);
Join<TableAb, TableA> aJoin = abRoot.join("tableA");
Join<TableAb, TableB> bJoin = abRoot.join("tableB");
//combine joins
query.orderBy(cb.asc(/* Expression to order by */));
return cb.conjunction();
};
In my opinion the main problem is that there is no "path" from table_a to table_b, but I explicitly do not want to have any reference inside of table_a to table_b.
Since you're using Spring Data JPA , you can just make an interface with a method on it that look like this:
public interface TableABRepository extends Repository<TableAB, Long> {
public List<TableAB> findAllByOrderByTableB();
}
Assuming your TableAB class is something like this:
class TableAB {
TableA tableA;
TableB tableB;
}
Thak method will return all elements from table_ab ordered by the name_b field.
After that you just get the TableA elements from the TableAB returned list.
You have the EntityManager.find(Class entityClass, Object primaryKey) method to find a specific row with a primary key.
But how do I find a value in a column that just have unique values and is not a primary key?
You can use appropriate JPQL with TypedQuery.
try {
TypedQuery<Bean> tq = em.createQuery("from Bean WHERE column=?", Bean.class);
Bean result = tq.setParameter(1, "uniqueKey").getSingleResult();
} catch(NoResultException noresult) {
// if there is no result
} catch(NonUniqueResultException notUnique) {
// if more than one result
}
For example, like this:
List<T> results = em.createQuery("SELECT t FROM TABLE t", T.class)
.getResultList();
With parameters:
List<T> results = em.createQuery("SELECT t FROM TABLE t where t.value = :value1")
.setParameter("value1", "some value").getResultList();
For single result replace getResultList() with getSingleResult():
T entity = em.createQuery("SELECT t FROM TABLE t where t.uniqueKey = :value1")
.setParameter("value1", "KEY1").getSingleResult();
One other way is to use Criteria API.
You can use a Query, either JPQL, Criteria, or SQL.
Not sure if your concern is in obtaining cache hits similar to find(). In EclipseLink 2.4 cache indexes were added to allow you to index non-primary key fields and obtain cache hits from JPQL or Criteria.
See,
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Indexes
Prior to 2.4 you could use in-memory queries to query the cache on non-id fields.
TL;DR
With in DSL level - JPA no practice mentioned in previous answers
How do I find a value in a column that just have unique values and is not a primary key?
There isn't specification for query with custom field with in root interface of javax.persistence.EntityManager, you need to have criteria base query.
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<R> criteriaQuery = criteriaBuilder.createQuery(EntityType.class)
Root<R> root = criteriaQuery.from(type);
criteriaBuilder.and(criteriaBuilder.equal(root.get(your_field), value));
You can also group your predicates together and pass them all together.
andPredicates.add(criteriaBuilder.and(root.get(field).in(child)));
criteriaBuilder.and(andPredicates.toArray(new Predicate[]{});
And calling result(rather single entity or a list of entities) with
entityManager.createQuery(suitable_criteria_query).getSingleResult();
entityManager.createQuery(suitable_criteria_query).getResultList();
We have a DB table that is mapped into a hibernate entity. So far everything goes well...
However what we want is to only map enentitys that satisty a specific criteria, like ' distinct(fieldA,fieldB) '...
Is it possible to map with hibernate and hibernate annotations? How can we do it? With #Filter?
I would recommend that you use #Where annotation. This annotation can be used on the element Entity or target entity of a collection. You provide a clause attribute written in sql that will be applied to any select that hibernate performs on that entity. It is very easy to use and very readable.
Here is an example.
#Entity
//I am only interested in Donuts that have NOT been eaten
#Where(clause = "EATEN_YN = 'N'")
public class Donut {
#Column(name = "FILLING")
private String filling;
#Column(name = "GLAZED")
private boolean glazed = true;
#Column(name = "EATEN_YN")
private boolean eaten = false;
...
}
You could create a view and then map that view to entity:
create view my_data as
select ... from ...
#Entity(table="my_data")
public class MyData { ... }
One option is to map the table normally, then you could fetch your always entities through a query or a filter.
You could also make a native SQL query and map the entity on the results:
Query q = sess.createSQLQuery("SELECT DISTINCT fieldA, fieldB FROM some_table")
.addEntity(MyEntity.class);
List<MyEntity> cats = q.list();
It might be also possible to add DISTINCT to this type of HQL query:
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
Methods 1, 3 and 4 will make a read-only mapping.
Could you be more specific about the criteria you are using? The view approach is more generic since you can't do everything with a hibernate query or filter.
perhaps you could create a new Pojo that encapsulates the fields and the condition that they should statisy . And then then make that class a 'custom user defined type', such that Hibernate will have to use the mapping class that you provide, for mapping that 'type'..
In addition to the options mentioned by Juha, you can also create an object directly out of a SQL query using the NamedNativeQuery and SqlResultSetMapping annotations.
#Entity
#SqlResultSetMapping(name = "compositekey", entities =
#EntityResult(entityClass = MiniBar.class,
fields = { #FieldResult(name = "miniBar", column = "BAR_ID"), })
)
#NamedNativeQuery(name = "compositekey",
query = "select BAR_ID from BAR", resultSetMapping = "compositekey")
#Table(name = "BAR")
public class Bar {
Flavor the SQL query to your taste