distinct inner join hql - java

I've the following hibernate entities
public class Container {
...
#OneToMany
private List<ACLEntry> aclEntries;
}
For securing my container instances i use the following entity:
public class ACLEntry {
...
private Long sid;
private boolean principal;
private Integer mask;
}
The hql-queries will be created automatically so for searching container instances,
the following query will be created:
select container from Container container
inner join container.aclEntries as aclEntry
with bitwise_and (aclEntry.mask, 1) = 1 and
(aclEntry.sid = :userId or aclEntry.sid = :roleId)
The problem with this is, that the aclentry join could return 2 results which will result in duplicate container results.
Has anyone an idea how to solve this ?

As far as I understood the problem you need a container that can hold multiple entries of your Container object just replace your hql query with the following one:
With adding select distinct as native query.

As a brute force solution, you could write a native query.

It might make more sense to write this as a Criteria query, which easily supports selecting an object based on conditions of it's associations.
The same thing can be done in HQL or a native query, it might be instructive to execute a Criteria query specifying the same logic and just see what HQL/SQL it generates.

Related

Hibernate is making extra SQL statement with #ManyToOne and #Lazy fetching object

I would like someone to explain me why Hibernate is making one extra SQL statement in my straight forward case. Basically i have this object:
#Entity
class ConfigurationTechLog (
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long?,
val configurationId: Long,
val type: String,
val value: String?
) {
#JsonIgnore
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "configurationId", insertable = false, updatable = false)
val configuration: Configuration? = null
}
So as you can see, nothing special there. And when i execute this query :
#Query(value = "SELECT c FROM ConfigurationTechLog c where c.id = 10")
fun findById10() : Set<ConfigurationTechLog>
In my console i see this:
Hibernate:
/* SELECT
c
FROM
ConfigurationTechLog c
where
c.id = 10 */ select
configurat0_.id as id1_2_,
configurat0_.configuration_id as configur2_2_,
configurat0_.type as type3_2_,
configurat0_.value as value4_2_
from
configuration_tech_log configurat0_
where
configurat0_.id=10
Hibernate:
select
configurat0_.id as id1_0_0_,
configurat0_.branch_code as branch_c2_0_0_,
configurat0_.country as country3_0_0_,
configurat0_.merchant_name as merchant4_0_0_,
configurat0_.merchant_number as merchant5_0_0_,
configurat0_.org as org6_0_0_,
configurat0_.outlet_id as outlet_i7_0_0_,
configurat0_.platform_merchant_account_name as platform8_0_0_,
configurat0_.store_type as store_ty9_0_0_,
configurat0_.terminal_count as termina10_0_0_
from
configuration configurat0_
where
configurat0_.id=?
Can someone please explain me, what is happening here ? From where this second query is coming from ?
I assume you are using Kotlin data class. The kotlin data class would generate toString, hashCode and equals methods utilizing all the member fields. So if you are using the returned values in your code in a way that results in calling of any of these method may cause this issue.
BTW, using Kotlin data claases is against the basic requirements for JPA Entity as data classes are final classes having final members.
In order to make an association lazy, Hibernate has to create a proxy instance instead of using the real object, i.e. it needs to create an instance of dynamically generated subclass of the association class.
Since in Kotlin all classes are final by default, Hibernate cannot subclass it so it has to create the real object and initialize the association right away. In order to verify this, try declaring the Configuration class as open.
To solve this without the need to explicitly declare all entities open, it is easier to do it via the kotlin-allopen compiler plugin.
This Link can be useful for understand what kind (common) problem is that N + 1 Problem
Let me give you an example:
I have three Courses and each of them have Students related.
I would like to perform a "SELECT * FROM Courses". This is the first query that i want (+ 1) but Hibernate in background, in order to get details about Students for each Course that select * given to us, will execute three more queries, one for each course (N, there are three Course coming from select *). In the end i will see 4 queries into Hibernate Logs
Considering the example before, probably this is what happen in your case: You execute the first query that you want, getting Configuration Id = 10 but after, Hibernate, will take the entity related to this Configuration, then a new query is executed to get this related entity.
This problem should be related in specific to Relationships (of course) and LAZY Fetch. This is not a problem that you have caused but is an Hibernate Performance Issue with LAZY Fetch, consider it a sort of bug or a default behaviour
To solve this kind of problem, i don't know if will be in your case but ... i know three ways:
EAGER Fetch Type (but not the most good option)
Query with JOIN FETCH between Courses and Students
Creating EntityGraph Object that rappresent the Course and SubGraph that rappresent Students and is added to EntityGraph
Looking at your question, it seems like an expected behavior.
Since you've set up configuration to fetch lazily with #ManyToOne(fetch = FetchType.LAZY), the first sql just queries the other variables. When you try to access the configuration object, hibernate queries the db again. That's what lazy fetching is. If you'd like Hibernate to use joins and fetch all values at once, try setting #ManyToOne(fetch = FetchType.EAGER).

When use a JoinType in Criteria ? Hibernate mapping

I'm a bit confused about when use a join with Criteria. Example of what I'm talking about:
.createAlias("cars", "c", JoinType.LEFT_OUTER_JOIN)
Here are the different JoinType:
LEFT_OUTER_JOIN
INNER_JOIN
LEFT_OUTER_JOIN
NONE
RIGHT_OUTER_JOIN
When we map entities with Hibernate there is already a "JoinType" automatically (is that an INNER_JOIN ?).
Simple example with user - car:
User class (table name USERS)
class User {
#Id
#Column(name="ID_USER")
private int idUser;
#Column(name="NAME")
private String name;
#OneToMany
#JoinColumn(name="ID_USER")
private Set<Car> cars;
}
And Car class (table name CARS):
class Car{
#Id
#Column(name="ID_CAR")
private int idCar;
#Column(name="MODEL)
private String model;
#Column(name="ID_USER")
private int idUser;
}
If I have a user u and i type u.getCars() which Join is it mapped by default ?
If I want for example results of:
SELECT * FROM Users u LEFT JOIN Cars c on u.id_user = c.id_user
Then is that correct to use that :
Criteria c = getSession().createCriteria(User.class);
c.createAlias("cars", "c", JoinType.LEFT_OUTER_JOIN);
return c.list();
(And then I loop on User and Car to search what I'm looking for).
And if I'm looking for that:
SELECT * FROM Users u INNER JOIN Cars c on u.id_user = c.id_user
Am I right saying I have no need to create a JoinType.INNER_JOIN ?
If I'm, JoinType.INNER_JOIN is useless when I already have map entites ?
I have to map a lot of complex tables using a lot of join and I'm a little muddled !
When we map entities with Hibernate there is already a "JoinType" automatically (is that an INNER_JOIN ?).
Assuming you have marked the oneToMany association as Fetch.EAGER. In that case when you load User object via hibernate criteria, it uses LEFT OUTER JOIN by default, unless you override it in Criteria. If you have not marked it as FetchType.EAGER, it will be lazy and so no join by default, again, unless you override it in Criteria.
If I have a user u and i type u.getCars() which Join is it mapped by default ?
For EAGER loading refer above. If lazy and it is within the same session you are calling u.getCars, it will fire a new SELECT to load cars based on the userId and so no join.
Am I right saying I have no need to create a JoinType.INNER_JOIN ? If I'm, JoinType.INNER_JOIN is useless when I already have map entites ?
Refer above, by default it is LEFT OUTER JOIN if it is EAGER and unless overidden in Criteria.
On suggestion, - It would be good idea to view the SQLs that hibernate is actually firing in the background via enable show_sql property with different FetchTypes and Criteria JOIN Types, because based on your entiy mappings the count/complexity of SQL queries might be different.

Datanucleus creates subquery instead of join

I have these annotations:
public class Account {
#Persistent(defaultFetchGroup = "true", dependent = "false")
#Column(name = "user_owner_id")
private User user;
}
public class User {
#Persistent(defaultFetchGroup = "true", mappedBy = "user")
#Element(column = "user_owner_id", dependent = "true")
private Set<Account> accounts;
}
When initiating the Account class, the query on the database use a SELECT * FROM accounts where exists (SELECT id from users where id=23)
I am trying to give datanucleus an annotation that tells it to run on the database SELECT a.* FROM accounts a JOIN users u on a.id = u.user_id where u.id = 23 as this is more optimal.
So which annotation should I use to make data nucleus change its query formation?
--- addition ----
This is a stripped down version of how we're retrieving the data:
PersistenceManager persistenceManager = persistenceManagerFactory.getPersistenceManager();
persistenceManager.getFetchPlan().setMaxFetchDepth(FetchPlan.FETCH_SIZE_GREEDY);
Query query = persistenceManager.newQuery("javax.jdo.query.JDOQL", null);
query.setClass(User.class);
query.setFilter("this.uuid==p1");
query.declareParameters("java.lang.String p1");
final List<E> entities = (List<E>) query.execute(uuid);
E entity = entities.iterator().next();
return persistenceManager.detachCopy(entity);
You are performing a Query just to get one object, which is very inefficient. Instead you could easily do
User u = pm.getObjectById(User.class, 1);
and this would likely issues 2 SQLs in total; 1 to get the basic User object, and 1 to get the Accounts connected to that User. There would be no EXISTS clause.
With regards to what you are actually doing. A Query is issued. A Query is general and in most use-cases will return multiple objects. The filter clause of the query can be complex. The Query is converted into an SQL to get the basic User fields. It can't get the related objects in a single call, so your log will likely say something about BULK FETCH (or something similar, whatever DataNucleus calls it). This will have an EXISTS clause with the EXISTS subquery restricting the second query to the objects the Query applies to). They do this to avoid the N+1 problem. Using EXISTS is, in general, the most appropriate for the general case of a query. In your specific situation it would have been nice to have an INNER JOIN, but I don't think that is supported as a BULK FETCH option currently. Their code is open source and I know they have asked people to contribute things in the past where they want alternative handling ... so you could contribute something if you want to use a query in this precise situation.

QueryDSL code generation for ManyToMany

I'm porting some complex JPQL queries in a large Hibernate/JPA2 application to use QueryDSL 2.3.0, and I'm stuck on one.
My Client entity contains
#ManyToMany
private List<Group> groups;
My existing query fragment is
EXISTS(SELECT g FROM Group g WHERE g MEMBER OF slr.groups AND
UPPER(g.description) LIKE :group)
The QueryDSL code generation has produced the following in my QClient class:
public final SimplePath<java.util.List<Group>> groups =
createSimple("groups", java.util.List.class);
The code generation using SimplePath doesn't let me use the in or contains methods to query membership. I think I need a CollectionPath instead. Is there a way to annotate the Client class so that QueryDSL uses the correct type for querying a collection?
I have an answer. This looks like a bug introduced in QueryDSL 2.2.5, which only happens when working in Eclipse.
The correct solution is to not use Eclipse to generate the source (don't enable annotation processing). Instead, I'm using m2eclipse and generating the source in Maven.
For reference, my first workaround was to extend the generated QClient class with my own QQClient class, which adds one member:
public final ListPath<Group, QGroup> fixedgroups =
createList("groups", Group.class, QGroup.class);
At that point the equivalent to my original query is:
QGroup g = QGroup.group;
JPQLSubQuery subquery = new JPQLSubQuery().from(g);
subquery = subquery.where(slr.fixedgroups.contains(g),
g.description.upper().like("%" + group.toUpperCase() + "%"));
query = query.where(subquery.exists());
(query is the larger query this is part of. slr is an instance of QQClient brought into the outer query by a left join.)

JPA Query For Exists In

I have the following Entities (reduced and renamed for this example)
#Entity
public class Case {
#Id
private Long id;
#ManyToOne(optional=false)
private CourtConfiguration courtConfiguration;
#ElementCollection(fetch=FetchType.EAGER)
private List<String> caseNumbers;
}
Second Entity
#Entity
public class CourtConfiguration {
#Id
private Long id;
String countyId;
String referenceId;
....
}
I am trying to search using JPQL for all Cases that have a certain courtConfiguration countyId and have caseNumbers containing all of a provided set of important caseNumbers.
So my query needs the countyId and set of caseNumbers as parameters. Called countyId and importantCaseNumbers respectively.
I have tried and failed to get it to work.
My query looks like this
String query = "SELECT case FROM Case case JOIN case.caseNumbers caseNumbers WHERE ";
query += "case.caseConfiguration.countyId = :countyId ";
The bit above works until I add my caseNumber conditions.
I have tried a foreach importantNumbers to extend the query and as soon as the list of important numbers goes above one it doesn't work. No values get returned.
for (String importantCaseNum : importantCaseNumbers) {
query += " AND '"+importantCaseNum+"' in (caseNumbers)";
}
Any suggestions/pointers appreciated. I guess what I am looking for is a case.caseNumbers contains (importantNumbers) clause.
Update I have reverted to native SQL for my query as I didn't want to tie myself into hibernate by using HQL. Thanks to #soulcheck and #mikko for helping me out. I'll post up when the hibernate JPA fix is available.
Thanks
Paul
Syntactically correct way to build this JPQL query is with MEMBER OF. But because of problem reported in HHH-5209 it doesn't work with old Hibernate versions (fix version 4.1.8, 4.3.0.Beta1). According bug report HQL version of this query works, so your options includes at least:
Using JPQL query and switching to some other JPA implementation
Using HQL instead and sticking with Hibernate:
query += " AND '"+importantCaseNum+"' in elements(caseNumbers)";

Categories