I am playing around with hibernate and I have downloaded the following test DB https://dev.mysql.com/doc/employee/en/sakila-structure.html
I have a named query on the employee class of:
#NamedNativeQuery(
name="complexQuery",
query="select * from employees inner join salaries on employees.emp_no=salaries.emp_no where salaries.from_date < 19870101 " +
"AND employees.emp_no = 10064;",
resultClass=Employee.class
)
I have mapped the employees to salaries via:
#OneToMany(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER
)
#JoinColumn(name = "emp_no", nullable = false, insertable=false, updatable=false)
private Set<Salary> salaries = new HashSet<>();
I expected that the following hibernate queries would include the where statement of
salaries.from_date < 19870101
however I noticed that actually the hibernate query for the salaries selects all rows for that employee id:
Hibernate:
select
*
from
employees
inner join
salaries
on employees.emp_no=salaries.emp_no
where
salaries.from_date < 19870101
AND employees.emp_no = 10064;
Hibernate:
select
salaries0_.emp_no as emp_no1_4_0_,
salaries0_.from_date as from_dat2_4_0_,
salaries0_.emp_no as emp_no1_4_1_,
salaries0_.from_date as from_dat2_4_1_,
salaries0_.salary as salary3_4_1_,
salaries0_.to_date as to_date4_4_1_
from
salaries salaries0_
where
salaries0_.emp_no=?
Is there anyway to have the auto generated salaries query also include
where
salaries0_.from_date < ?
AND salaries0_.emp_no=?
EDIT: I am also getting the issue for a named query:
#NamedQuery(
name="complexQuery",
query="select e " +
"from Employee e, Salary s " +
"where e.id = 10064 " +
"AND s.id.fromDate < 19900101" +
"AND s.id.empNo = 10064"
)
You are not joining Employee and Salary!
That's the correct statement
#NamedQuery(
name="complexQuery",
query="select e " +
"from Employee e join e.salaries s " +
"where e.id = 10064 " +
"AND s.id.fromDate < 19900101"
)
I am using Jpa 2.2 with Hibernate 5.3.7 final jar. Below are my entities
and code for the test case is not executing correctly:
Instructor Entity :
#Entity
#DynamicUpdate
#DynamicInsert
public class Instructor {
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
#GenericGenerator(name = "native", strategy = "native")
private int id;
#Version
#Column(columnDefinition = "int(11) not null default 0")
private int version = 0;
**#OneToOne(mappedBy = "instructor", fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.ALL)
#JoinColumn(name = "proof_id")
private IdProof idProof;
#OneToMany(mappedBy = "instructor", orphanRemoval = true, cascade = CascadeType.ALL)
private Set<Vehicle> vehicles = new HashSet<>();**
IdProof Entity:
#Entity
#Table(name = "id_proof_tbl")
#DynamicInsert
#DynamicUpdate
public class IdProof {
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
#GenericGenerator(name = "native", strategy = "native")
#Column(updatable = false)
private int id;
#Version
#Column(columnDefinition = "int(11) not null default 0")
private int version;
**#OneToOne(fetch = FetchType.LAZY)
private Instructor instructor;**
Vehicle Entity :
#Entity
#DynamicInsert
#DynamicUpdate
public class Vehicle {
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
#GenericGenerator(name = "native", strategy = "native")
private int id;
**#ManyToOne(fetch = FetchType.LAZY)
private Instructor instructor;**
**#OneToMany(mappedBy = "vehicle", fetch = FetchType.LAZY, orphanRemoval = true, cascade = CascadeType.ALL)
private Set<Document> documents = new HashSet<>();**
And Documents Entity :
#Entity
#DynamicInsert
#DynamicUpdate
public class Document {
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
#GenericGenerator(name = "native", strategy = "native")
private int id;
**#ManyToOne(fetch = FetchType.LAZY)
private Vehicle vehicle;**
I am fetching data from MySQL database with code described as below as
a JUnit Test:
#Test
#Rollback(false)
#Transactional
public void fetchPartialDataWithJPQLQueryWithEqualEntityGraph() {
**// Preparing Entity Graph to be passed as a hint .**
EntityGraph<Instructor> instructorGraph =
em.createEntityGraph(Instructor.class);
instructorGraph.addAttributeNodes(Instructor_.idProof);
Subgraph<Vehicle> vehcileSubgraph =
instructorGraph.addSubgraph(Instructor_.VEHICLES);
vehcileSubgraph.addAttributeNodes(Vehicle_.documents);
**//Case 1:**
TypedQuery<Instructor> typedQueryJoinFetch =
em.createQuery(" select i from Instructor i "
+ " join fetch i.idProof id "
+ " join fetch i.vehicles v "
+ " join fetch v.documents vd ",
Instructor.class);
typedQueryJoinFetch.setHint("javax.persistence.fetchgraph",
instructorGraph);
List<Instructor> instructors
=typedQueryJoinFetch.getResultList();
**//Case 2:**
TypedQuery<Instructor> typedQueryLeftJoin =
em.createQuery(" select i from Instructor i "
+ " left join i.idProof id "
+ " left join i.vehicles v "
+ " left join v.documents vd ",
Instructor.class);
typedQueryLeftJoin.setHint("javax.persistence.fetchgraph",
instructorGraph);
List<Instructor> instructorsWithLeftJoin =
typedQueryLeftJoin.getResultList();
**// Case 3:**
try {
TypedQuery<Instructor>
typedQueryLeftJoinAndJoinFetchMixed =
em.createQuery(" select i from Instructor i "
+ " join fetch i.idProof id "
+ " left join i.vehicles v "
+ " join fetch v.documents vd ",
Instructor.class);
typedQueryLeftJoinAndJoinFetchMixed.
setHint("javax.persistence.fetchgraph", instructorGraph);
List<Instructor>
instructorsWithLeftJoinAndJoinFetchMixed =
typedQueryLeftJoinAndJoinFetchMixed
.getResultList();
} catch (Exception e) {
e.printStackTrace();
}
}
**Case 1 executes correctly and results in the inner join between the four
tables with below query:**
select
instructor0_.id as id1_2_0_,
idproof1_.id as id1_1_1_,
vehicles2_.id as id1_5_2_,
documents3_.id as id1_0_3_,
instructor0_.address as address2_2_0_,
instructor0_.birth_date_time as birth_da3_2_0_,
instructor0_.birth_date_time_zone_offset as birth_da4_2_0_,
instructor0_.created_date as created_5_2_0_,
instructor0_.day_off_time as day_off_6_2_0_,
instructor0_.day_start_time as day_star7_2_0_,
instructor0_.father_name as father_n8_2_0_,
instructor0_.mother_name as mother_n9_2_0_,
instructor0_.name as name10_2_0_,
instructor0_.photo as photo11_2_0_,
instructor0_.monthly_salary as monthly12_2_0_,
instructor0_.updated_date as updated13_2_0_,
instructor0_.version as version14_2_0_,
idproof1_.address as address2_1_1_,
idproof1_.created_date as created_3_1_1_,
idproof1_.father_name as father_n4_1_1_,
idproof1_.instructor_id as instruc12_1_1_,
idproof1_.is_foreigner as is_forei5_1_1_,
idproof1_.mother_name as mother_n6_1_1_,
idproof1_.name as name7_1_1_,
idproof1_.proof_sequence_no as proof_se8_1_1_,
idproof1_.sex as sex9_1_1_,
idproof1_.updated_date as updated10_1_1_,
idproof1_.version as version11_1_1_,
vehicles2_.creation_date as creation2_5_2_,
vehicles2_.instructor_id as instruct8_5_2_,
vehicles2_.purchased_date_time as purchase3_5_2_,
vehicles2_.purchased_date_zone_offset as purchase4_5_2_,
vehicles2_.student_id as student_9_5_2_,
vehicles2_.updated_date as updated_5_5_2_,
vehicles2_.vechicle_type as vechicle6_5_2_,
vehicles2_.vehicle_number as vehicle_7_5_2_,
vehicles2_.instructor_id as instruct8_5_0__,
vehicles2_.id as id1_5_0__,
documents3_.name as name2_0_3_,
documents3_.vehicle_id as vehicle_3_0_3_,
documents3_.vehicle_id as vehicle_3_0_1__,
documents3_.id as id1_0_1__
from
instructor instructor0_
inner join
id_proof_tbl idproof1_
on instructor0_.id=idproof1_.instructor_id
inner join
vehicle vehicles2_
on instructor0_.id=vehicles2_.instructor_id
inner join
document documents3_
on vehicles2_.id=documents3_.vehicle_id
**Case 2 executes successfully and results in left outer join between
four tables with below query:**
select
instructor0_.id as id1_2_0_,
idproof1_.id as id1_1_1_,
vehicles2_.id as id1_5_2_,
documents3_.id as id1_0_3_,
instructor0_.address as address2_2_0_,
instructor0_.birth_date_time as birth_da3_2_0_,
instructor0_.birth_date_time_zone_offset as birth_da4_2_0_,
instructor0_.created_date as created_5_2_0_,
instructor0_.day_off_time as day_off_6_2_0_,
instructor0_.day_start_time as day_star7_2_0_,
instructor0_.father_name as father_n8_2_0_,
instructor0_.mother_name as mother_n9_2_0_,
instructor0_.name as name10_2_0_,
instructor0_.photo as photo11_2_0_,
instructor0_.monthly_salary as monthly12_2_0_,
instructor0_.updated_date as updated13_2_0_,
instructor0_.version as version14_2_0_,
idproof1_.address as address2_1_1_,
idproof1_.created_date as created_3_1_1_,
idproof1_.father_name as father_n4_1_1_,
idproof1_.instructor_id as instruc12_1_1_,
idproof1_.is_foreigner as is_forei5_1_1_,
idproof1_.mother_name as mother_n6_1_1_,
idproof1_.name as name7_1_1_,
idproof1_.proof_sequence_no as proof_se8_1_1_,
idproof1_.sex as sex9_1_1_,
idproof1_.updated_date as updated10_1_1_,
idproof1_.version as version11_1_1_,
vehicles2_.creation_date as creation2_5_2_,
vehicles2_.instructor_id as instruct8_5_2_,
vehicles2_.purchased_date_time as purchase3_5_2_,
vehicles2_.purchased_date_zone_offset as purchase4_5_2_,
vehicles2_.student_id as student_9_5_2_,
vehicles2_.updated_date as updated_5_5_2_,
vehicles2_.vechicle_type as vechicle6_5_2_,
vehicles2_.vehicle_number as vehicle_7_5_2_,
vehicles2_.instructor_id as instruct8_5_0__,
vehicles2_.id as id1_5_0__,
documents3_.name as name2_0_3_,
documents3_.vehicle_id as vehicle_3_0_3_,
documents3_.vehicle_id as vehicle_3_0_1__,
documents3_.id as id1_0_1__
from
instructor instructor0_
left outer join
id_proof_tbl idproof1_
on instructor0_.id=idproof1_.instructor_id
left outer join
vehicle vehicles2_
on instructor0_.id=vehicles2_.instructor_id
left outer join
document documents3_
on vehicles2_.id=documents3_.vehicle_id
But I want to fetch only those instructors having IdProofs present in the database along with their only vehicles having documents present in the database. So I coded case 3. But Case 3 results in below exception:
java.lang.IllegalArgumentException: org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=vd,role=com.katariasoft.technologies.jpaHibernate.college.data.entity.Vehicle.documents,tableName=document,tableAlias=documents3_,origin=vehicle vehicles2_,columns={vehicles2_.id ,className=com.katariasoft.technologies.jpaHibernate.college.data.entity.utils.Document}}] [ select i from com.katariasoft.technologies.jpaHibernate.college.data.entity.Instructor i join fetch i.idProof id left join i.vehicles v join fetch v.documents vd ]
Please let me know how I can fulfil my case 3 requirement to fetch
All Instructors having IdProofs present In DB along with their only vehicles having documents present in DB.
You would need to join the document without the fetch as hibernate is trying to fetch these for a non-existant vehicle also with that keyword.
em.createQuery(" select i from Instructor i "
+ " join fetch i.idProof id "
+ "left join i.vehicles v "
+ " join v.documents vd ",
Instructor.class);
Now after you get the results, before processing / accessing the documents of a vehicle you would simply check whether Instructor.vehicles is empty and not proceed if true.
Below code fetches Only those instrcutors having IdProof present in
DB . If No Instructor is having idProof present in db then written
query will return empty List as there is inner join between
Instructor and IdProof .
All Instructors not having any vehicle will also be fetched and their
vehicle list will be initialised as empty.
All vehicle not having any document will also be fetched along with
vehicles having atleast one document . Vehicles not having any
document can be avoided in code while processing data .
But if inner join is created between Vehicle and document and if
there is no document present in db then it will make the complete
result empty . As final inner join will be applied on join results
evaluted till yet in db on first three tables Instructor , IdProof and
Vehcile.
try {
TypedQuery<Instructor>
typedQueryLeftJoinAndJoinFetchMixed =
em.createQuery(" select i from Instructor i "
+ " join fetch i.idProof id "
+ " left join i.vehicles v "
+ " left join v.documents vd ",
Instructor.class);
typedQueryLeftJoinAndJoinFetchMixed.
setHint("javax.persistence.fetchgraph", instructorGraph);
List<Instructor>
instructorsWithLeftJoinAndJoinFetchMixed =
typedQueryLeftJoinAndJoinFetchMixed
.getResultList();
} catch (Exception e) {
e.printStackTrace();
}
}
I have 3 classes, I am trying to get a list of all the events of an eventhost that a user is subscribed to. I am probably thinking way too complicated but I have very little experience with JPA/HQL.
User class
#ManyToMany
#JoinTable(name = "Subscriptions", joinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id") , inverseJoinColumns = #JoinColumn(name = "event_host_id", referencedColumnName = "id") )
private List<EventHost> subscriptions;
EventHost class
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
#JoinColumn(name = "event_host_id", referencedColumnName = "id", updatable = true)
private List<Event> events;
I tried using this query, but it tells me that subscriptions is not mapped, which it is not since it's not a java class.
String hql = "SELECT o FROM Event WHERE event_host_id IN (SELECT a FROM EventHost WHERE id IN(SELECT b FROM User WHERE = + " + userid + "))";
I know injecting the userid like this is bad practice, I'm just doing it for testing purposes.
Please ask if you need something more, I would really like to understand how to write a query for this.
This question should really be HQL with two join tables, but I'll let you change it. Since its HQL, or JPA, it's database independent.
Anyway, any time you see a OneToMany or ManyToMany relationship you have a join table and so you should be thinking joins. It's always a good idea to look at the sql create table statements to see what's going on. In this case your user_subscriptions join table is:
create table user_subscriptions (user_id integer not null, subscriptions_id integer not null)
and your event_host_events join table is this:
create table event_host_events (event_host_id integer not null, events_id integer not null)
Nothing new there. When you're trying to get something new working that you don't intuitively understand, break it down into things you can do. For example, you can execute two queries, getting a Users subscriptions first, and then getting the Events for those subscriptions:
Query query = session.createQuery("select u.subscriptions from User u where name = :name");
query.setParameter("name", name);
List<EventHost> subscriptions = query.list();
List<Event> events = new ArrayList<Event>();
Query query2 = session.createQuery("select s.events from EventHost s where id = :id");
for (EventHost s: subscriptions ) {
query2.setParameter("id", s.getId());
events.addAll( query2.list());
}
Not elegant, but it works. Then, keeping join in mind, figure out how to make one statement out of the two of them.
Query query = session.createQuery("select s.events from User u join u.subscriptions s where u.name = :name)");
query.setParameter("name", name);
return query.list();
The join will use an inner join by default, so you're ok there. The JPA provider will auto-magically join your three Entity tables and two Join Tables for you:
select
event4_.id as id1_2_
from user user0_
inner join user_subscriptions subscripti1_ on user0_.id=subscripti1_.user_id
inner join event_host eventhost2_ on subscripti1_.subscriptions_id=eventhost2_.id
inner join event_host_events events3_ on eventhost2_.id=events3_.event_host_id
inner join event event4_ on events3_.events_id=event4_.id
where user0_.name=?
Aren't you glad you don't have to write that query?
I have entity with following attributes:
#Table(name = "MY_TABLE")
public class MyTable {
......
#OneToMany(mappedBy = "mytable", cascade = ALL, orphanRemoval = true)
private List<MyAnotherTable> otherTableValues = new ArrayList<MyAnotherTable>();
......
}
Now I am trying to write HSQL as
TypedQuery<Share> q =
getEntityManager().createQuery("SELECT MyTable FROM MyTable AS mytable WHERE " +
"mytable.someField=:firstParam AND mytable.secondField IS NOT NULL AND " +
// "AND mytable.otherTableValues"
, Share.class);
q.setParameter(firstParam, firstVal);
return q.getResultList();
So not sure how to put consition on otherTableValues as it is list type. Any suggestion?
You have to join the collection:
SELECT DISTINCT mytable
FROM MyTable AS mytable join mytable.otherTableValues AS otv
WHERE mytable.someField = :firstParam
AND mytable.secondField IS NOT NULL
AND otv.someOtherField = :someOtherField
I've got a question regarding children retrieval when using Transformers.aliasToBean function in Hibernate. I've wrote a query which populates my object, but when I try to populate children query doesn't work. I'll post sample code first and add generated SQL and exception below.
Parent
public class Parent {
#Id
#Column(name="id")
#GeneratedValue
private long id;
#Column(name="name")
private String name;
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name="parent_id")
#LazyCollection(LazyCollectionOption.FALSE)
private List<Child> children;
#Transient
private long someCount;
// public constructor + getters and setters
}
Child
public class Child {
#Id
#Column(name="id")
#GeneratedValue
private long id;
// public constructor + getters and setters
}
Query
Query query = session.createQuery("SELECT p.id as id, p.name as name, "
+ " (SELECT COUNT(*) FROM stat WHERE parent_id=p.id) as someCount, "
+ " FROM " + Parent.class.getSimpleName() + " p ")
.setResultTransformer(Transformers.aliasToBean(Parent.class));
Basically when I try to include p.children as children right in the first line of query I get the following sql generated by hibernate:
select parent0_.id as col_0_0_, parent0_.name as col_1_0_, . as col_2_0_,
(select count(*) from stats stat3_ where parent_id=parent0_.id) as col_3_0_, child2_.id
as id1_2_1_ from inner join child child2_ on parent0_.id=child2_.parent_id
And the error is:
WARN: SQL Error: 0, SQLState: 42601
ERROR: ERROR: syntax error at or near "." at position 60
That position 60 corresponds to , . as col_2_0_, the position before the dot in the first line of the query. Basically it seems to retrieve the children, but generated SQL prevents it from successfully executing the query.
Any help appreciated!
Simply speaking, you can not add p.children to result columns of SELECT, because it's a collection, and only entities or scalars are allowed here. But you can try this alternative:
Query query = session.createQuery("SELECT p, "
+ " (SELECT COUNT(*) FROM stat WHERE parent_id=p.id) "
+ " FROM " + Parent.class.getSimpleName() + " p " +
+ " LEFT JOIN FETCH p.children ");
for (Object[] row : (List<Object[]>)query.list()) {
Parent p = (Parent)row[0];
p.setSomeCount(((Number)row[1]).longValue());
}
Please tell if it really works for you.