How can I create a derived query methods in Spring data which select result based on multiple conditions but with only one parameter.
List<Entity> findById1OrId2OrId3(String id1OrId2OrId3);
Something like this example.
I don't think it is possible with derived queries.
Alternatively,
#Query("from Entity e where e.id1 = ?0 or e.id2 = ?0 or e.id3 = ?0")
List<Entity> findById1OrId2OrId3(String singleId);
You can use "In" and make list of Ids then make a new derived query to achieve this
Related
Assume that we have a persisted Entity object which has 10 variables, if I do for example repository.read(id) or repository.findById(id) I will get back an Entity object with every variable which is set from the repository.
Is there any way using JPAQuery or EntityManager or any other possible way, that I can make the call on the repository and get back the Entity object BUT without a specific variable being fetched as well?
I have tried the following, but it doesnt seem to do anything, still brings the Set within the response:
JPAQuery<Fruit> query = new JPAQuery<>(entityManager);
QFruit fruit = QFruit.Fruit;
Set<Apple> apple = new HashSet<Apple>();
query.select(fruit).from(fruit).where(fruit.id.eq(fruitId))
.createQuery().getParameters().remove(apple);
return query.fetchOne();
You can use any custom POJO to get your results in and specify what is selected.
https://docs.oracle.com/html/E13946_05/ejb3_langref.html#ejb3_langref_constructor
public interface AppleRepository extends CrudRepository<Apple, Long> {
#Query("SELECT new com.mypackage.Apple(a.field1, a.field2) FROM " +
" Apple a ")
List<Apple> findCustomApples();
}
Other way is to make any particular column to be Lazy Loaded. You can do that with annotation.
Eventually I was trying to read specific data from an entry of a Table, because the specific table has so many data it was harassing the performance, thus bringing the whole entity just for 1 or 2 variables was not correct.
Eventually what helped me was Tuple.
Using JPAQuery you have the advantage that you can select specific variables to be brought back from the search.
e.g.
JPAQuery<MyObject > query = new JPAQuery<>(entityManager);
MyObject myObject = QMyObject.MyObject ;
Tuple response = query.select(myObject.id, myObject.version)
.where(myObject.id.eq("12345")).or(myObject.version.eq("12345")).fetchaAll();
Then you can easily retrieve the Tuple object and handle the values as an array.
Consider this trivial query:
SELECT 1 as first, 2 as second
When using Hibernate we can then do something like:
em.createNativeQuery(query).fetchResultList()
However, there seem to be no way of getting the aliases (or column names). This would be very helpful for creating List<Map<String, Object>> where each map would be a row with their aliases, for instance in this case: [{first: 1, second: 2}].
Is there a way to do something like that?
I would suggest a bit different approach which may meet your needs.
In JPA 2.1 there is a feature called "result set mapping".
Basically you have to define a POJO class which would hold the result values (all the values must be passed using the constructor):
public class ResultClass{
private String fieldOne;
private String fieldTwo;
public ResultClass(String fieldOne, String fieldTwo){
this.fieldOne = fieldOne;
this.fieldTwo = fieldTwo;
}
}
Then you have to declare the mapping on one of your entities (does not matter on which, it just has to be a declated #Entity):
#SqlResultSetMapping(name="ResultMapping", classes = {
#ConstructorResult(targetClass = ResultClass.class,
columns = {#ColumnResult(name="columnOne"), #ColumnResult(name="columnTwo")})
})
The columnOne and columnTwo are aliases as declared in the select clause of the native query.
And finally use in the query creation:
List<ResultClass> results = em.createNativeQuery(query, "ResultMapping").getResultList();
In my opinion this is more elegant and "a level above" solution as you are not working with a generic Map key/values pairs but with a concrete POJO class.
You can use ResultTransformer interface . Implement custom mapper for mapping values with aliases.
here is example https://vladmihalcea.com/why-you-should-use-the-hibernate-resulttransformer-to-customize-result-set-mappings/
with ResultTransformer you can easy customize result set type , especially if you need aliases
Generally my questian is very simple I think, nevertheless I couldn't find a good solution. Let's say I have an Entity class called MyEntity which has a OneToMany relation to an Entity class called EntityAttribute, so it has a list or set attributes with objects of this class. EntityAttribute has an attribute name of type String.
Now I want to implement a method which takes attribute names and returns all entities that contains for each name in attributes at least one attribute with that name. Although this sounds very straight forward, the only solution I found was to execute a query for each attribute name and merge the results like this:
for (String name : attributeNames) {
CriteriaQuery<MyEntity> cq = cb.createQuery(MyEntity.class);
Root<MyEntity> entity = cq.from(MyEntity.class);
Join<MyEntity, EntityAttribute> attributeJoin = entity.join(MyEntity_.attributes);
cq.where(attributeJoin.get(EntityAttribute_.name).equals(name));
cq.select(entity);
... // get result list and merge
}
This code isn't tested but generally is one solution. This doesn't seem to be the most efficient one.
Another solution I testet was to use multiple joins like
CriteriaQuery<MyEntity> cq = cb.createQuery(MyEntity.class);
Root<MyEntity> entity = cq.from(MyEntity.class);
List<Predicate> predicates = new ArrayList<>();
for (String name : attributeNames) {
Join<MyEntity, EntityAttribute> attributeJoin = entity.join(MyEntity_.attributes);
predicates.add(attributeJoin.get(EntityAttribute_.name).equals(name));
}
cq.select(predicates.toArray(new Predicate[] {}));
... // get result list
This seems to be more efficient, but it iterates over the cartesian products... So it's highly inefficient.
I could also imagine to nest subqueries, but this seems to be very complicated.
The question simply is: What is the best solution for this problem? Afterwards I would also like to implement AND and OR, so I can query for all entities with attributes x and (y or z) or something like that. But for now I only want to make the AND case.
Thanks in advance
Maybe you could achieve this using in clause + group by + having + count, if I understand your question correctly. The idea is to count the number of matches for each MyEntity. If the count is equal to the number of attributes passed in, it means that each of them was found for that entity (assuming they are unique). In JPQL the query would look like this:
select e from MyEntity e join e.attributes a
where a.name in (:attributeNames)
group by e having count(*) = :attributeCount
where :attributeCount is the value of attributeNames.size().
I'm not very familiar with the criteria API, but you can experiment with something like this:
...
cq.groupBy(entity);
cq.having(cb.equal(cb.count(entity), attributeNames.size()));
// TODO: add IN clause
...
I have a very complicated query that doesn't convert to HQL, so I must use SQL. I put my query into a NamedNativeQuery annotation. It "should" return a list of two values (List<Object[2]>). When I try to run, the session fails to load the query because I haven't defined a mapping or result class. I cannot create a simple class to map these values to because the annotations have to go in a specific DAO project and the code that is using the queries exists in its own project. (The DAO project is more general-purpose and the entities in that project map directly to tables in our database. I cannot just create a small class in this project to map to my query result because it wouldn't fit the schema of this project)
Is there a way for me to map the query results to a more generic class, or even better is there some way for me to just get a List<Object[]> back without having to map at all to anything particular? This is extremely frustrating that Hibernate has to work in this particular way when all I want to do is execute a query and get the result back as an array.
This is the code that I use for storing the result of a SQL query in a List<Object[]>. Maybe you can adapt it to what you need:
public List<Object[]> executeSelectQuery(String sqlQuery) {
List<Object[]> cleanedResults = new ArrayList<Object[]>();
SQLQuery query = sessionFactory.getCurrentSession().createSQLQuery(sqlQuery);
List<Object[]> hibernateResults = query.list();
// Hibernate does not return always a List<Object[]>, but a list of strings or integers, so it is necessary to check the returned values
if (!hibernateResults.isEmpty()) {
if (hibernateResults.get(0) instanceof Object[]) {
cleanedResults = hibernateResults;
} else {
Object[] row;
// Use a 'for' because 'foreach' sometimes has casting exceptions converting to object
for (int i = 0; i < hibernateResults.size(); i++) {
row = new Object[1];
row[0] = hibernateResults.get(i);
cleanedResults.add(row);
}
}
}
return cleanedResults;
}
A NamedNativeQuery supports an addition annotation to allow you map the result to a POJO:
#SqlResultSetMapping(name="MyResult.Mapping", entities = {
#EntityResult(entityClass=MyResult.class, fields = {
#FieldResult(name="email", column="email"),
#FieldResult(name="name", column="name")
})
})
Then add resultSetMapping="MyResult.Mapping" to your NamedNativeQuery annotation.
I ended up not using NamedNativeQueries because they weren't working for me. Instead, I just used session.createSQLQuery with constant Strings, and it worked.
I probably didn't phrase my question properly. I could not use a #SqlResultSetMapping because I didn't have any classes to map to based on the structure of the code I am working with. My query has to come out as a List<Object[]> result, and there is no way to do that with NamedNativeQueries because Hibernate doesn't support it.
I have to go through some code of a project to implement some missiong functionality.
It uses jpa.In some popj classes i have found
#Entity
#Table(name="TYPE")
#NamedQueries( {
#NamedQuery(name = "getTypes", query = "SELECT dct FROM Type dct")
})
I know that i can used to get all records by using this query.Does this query return all records in type table?
This query will return all the Type entities including subtypes, if any. And since I can't say if this there are any subtypes, I can't say if this query will be restricted to the TYPE table.
Yes, it does. It generates an SQL query that looks roughly like this:
SELECT [column list here] FROM type