How to eager fetch join table collection in Hibernate Native SQL Query? - sql

I have following three database tables
Customer
Product
CustomerProductRelation
Corresponding to these tables, I have two Hibernate POJO's
Product
Customer
One of the member variable is a joinTable:
#JoinTable(name = "CustomerProductRelation", joinColumns = { #JoinColumn(name = "CUSTOMER_ID") }, inverseJoinColumns = { #JoinColumn(name = "PRODUCT_ID") })
private List<Product> products;
Due to some reason, I need to use a native SQL query on Customer table, in that case how do I eager fetch products in my customer list?
I am doing something similar to this:
String queryString = "select c.*,cpr.product_id from Customer c, CustomerProductRelation cpr where c.customer_id = cpr.customer_id";
List list = getSession().createSQLQuery(queryString)
.addEntity("c", Customer.class)
.addJoin("p", "c.products").list();
This does not seem to work. The exception is as follows:
java.lang.NullPointerException at org.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:37) at org.hibernate.loader.ColumnEntityAliases.<init>(ColumnEntityAliases.java:16) at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.generateCustomReturns(SQ‌​LQueryReturnProcessor.java:264)
Please let me know if anyone knows the solution to this.

Is this what you are seeing? (HHH-2225)

Related

Spring Data JPA Query for inner join table throwing error

Spring DATA JPA question... I am trying to write a query to access the data in my sql join table.
I have my join table set up as follows:
#Entity
#Table(name="WritingCompany")
public class WritingCompany {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "companyId")
private Long id;
// private String companyName;
#ManyToMany
#JoinTable(name = "Join-WritingCo-Carrier",
joinColumns = #JoinColumn(name="writingCo"),
inverseJoinColumns = #JoinColumn(name="carrier")
)
private Set<CarrierAppointment> carriers;
//...getters and setters
}
public class CarrierAppointment {
// ...
#ManyToMany(
cascade=CascadeType.ALL,
mappedBy = "companyName"
)
private Set<WritingCompany> companies;
public Set<WritingCompany> getCompanies() {
return companies;
}
public void setCompanies(Set<WritingCompany> companies) {
this.companies = companies;
}
//...
}
I am unsure which class repository I need to write the query for... I am trying to find all the writingCo names from the join table that all have the same carrier id that matches one specific carrier.
The Join Table is set up through writing company but I feel like it should be accessed through CarrierAppointment Repository since I am matching carrier Id's.
This is what I've tried in the CarrierAppointmentrepository and it throws this error (unexpected token: Join near line 1, column 94):
#Query("Select companyName FROM WritingCompany INNER JOIN CarrierAppointment ON Join-WritingCo-Carrier.carrier= CarrierAppointment.CarrierAppointmentId")
List<CarrierAppointment> findCarrierAppoinmentFromJoin(CarrierAppointment carrier);
I've also tried:
#Query("SELECT writingCo FROM Join-WritingCo-Carrier WHERE carrier= CarrierAppointment.CarrierAppointmentId")
List<CarrierAppointment> findCarrierAppoinmentFromJoin(CarrierAppointment carrier);
Then I tried this in the writingCompanyRepository which also throws a similar error:
#Query("Select companyName FROM WritingCompany INNER JOIN CarrierAppointment ON Join-WritingCo-Carrier.carrier= CarrierAppointment.CarrierAppointmentId")
List<WritingCompany> findAllWithDescriptionQuery(CarrierAppointment carrier);
I am having a hard time understanding what this query is saying. Do I ever need to access the columns from the sql join table, or am I just querying around the join table by asking for each class that is making up the join columns in the join table? What is the right part of the statement, after INNER JOIN stating ? Could someone please provide a deeper explanation of why the query is written so I can figure out why it's not working? I've been reading a lot of inner join examples and just can't seem to figure it out.

How to map ONE-TO-MANY native query result into a POJO class using #SqlResultSetMapping

Im working in a backend API using Java and MySql, and I'm trying to use #SqlResultSetMapping in JPA 2.1 for mapping a ONE-TO-MANY native query result into a POJO class, this is the native query:
#NamedNativeQuery(name = "User.getAll”, query = "SELECT DISTINCT t1.ID, t1.RELIGION_ID t1.gender,t1.NAME,t1.CITY_ID , t2.question_id, t2.answer_id FROM user_table t1 inner join user_answer_table t2 on t1.ID = t2.User_ID“,resultSetMapping="userMapping")
And, here is my result SQL mapping:
#SqlResultSetMapping(
name = "userMapping",
classes = {
#ConstructorResult(
targetClass = MiniUser.class,
columns = {
#ColumnResult(name = "id"),
#ColumnResult(name = "religion_id"),
#ColumnResult(name = "gender"),
#ColumnResult(name = "answers"),
#ColumnResult(name = "name"),
#ColumnResult(name = "city_id")
}
),
#ConstructorResult(
targetClass = MiniUserAnswer.class,
columns = {
#ColumnResult(name = "question_id"),
#ColumnResult(name = "answer_id")
}
)
})
And, here is the implementation of the POJO classes: (I just removed the constructor and the getters/setter)
MiniUser class
public class MiniUser {
String id;
String religionId;
Gender gender;
List<MiniUserAnswer> answers;
String name;
String city_id;
}
and the MiniUserAnswer class
public class MiniUserAnswer {
String questionId;
String answerId;
}
My goal is to execute this Query and return a list of MiniUser, and in each MiniUser: a list of his “answers", which is a list of MiniUserAnswer.
after running this code, I got this error:
The column result [answers] was not found in the results of the query.
I know why, it's because there is no “answers" field in the query select statement.
So, how can I accomplish something like this, considering the performance? This answers list may reach 100.
I really appreciate your help, Thanks in advance!
The query "SELECT DISTINCT t1.ID, t1.RELIGION_ID t1.gender, t1.NAME, t1.CITY_ID, t2.question_id, t2.answer_id" does not return a parameter called answers.
To obtain the result you are looking for I would use:
Option 1 (Criteria Builder)
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<UserTableEntity> cq = cb.createQuery(UserTableEntity.class);
Root<UserTableEntity> rootUserTable = cq.from(UserTableEntity.class);
Join<UserTableEntity,UserAnswerTableEntity> joinAnswerTable = rootUserTable.join(rootUserTable_.id) // if the relationship is defined as lazy, use "fetch" instead of "join"
//cq.where() NO WHERE CLAUSE
cq.select(rootUserTable)
entityManager.createQuery(cq).getResultList();
Option 2 (Named query, not native)
#NamedQuery(name = "User.getAll”, query = "SELECT t1 FROM UserTableEntityt1 join fetch t1.answers)
Option 3 (Entity subgraph, new in JPA 2.1)
In User Entity class:
#NamedEntityGraphs({
#NamedEntityGraph(name = "graph.User.Answers", attributeNodes = #NamedAttributeNode("answers"))
})
In DAO set hints in the entity manager:
EntityGraph graph = this.em.getEntityGraph("graph.User.Answers");
Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", graph);

deleting entries with JPA and subqueries

I just wrote an sql query :
DELETE FROM basisgegevens.gm_persoonburgstaat pbs
WHERE (pbs.ingangsdatum, pbs.id_persoon) in (
SELECT pbs2.ingangsdatum, pbs2.id_persoon
FROM basisgegevens.gm_persoonburgstaat pbs2
WHERE pbs2.ingangsdatum = pbs.ingangsdatum
AND pbs2.id_persoon = :persoonID
AND pbs2.id_persoonburgerlijkestaat > pbs.id_persoonburgerlijkestaat);
I need to rewrite it to JPQL, but am getting stuck with the subquery refrencing the outer query.
public class PersoonBurgerlijkeStaatEntity {
#Column(name = "id_persoonburgerlijkestaat"
private Long identifier;
private Date ingangsdatum;
#ManyToOne
#JoinColumn(name = "id_persoon", referencedColumnName = "id_persoon", nullable = false)
private PersoonEntity persoon;
}
The persoon entity has an identifier
Can someone help me rewrite this?
Thanks
Not sure about this but give a try.
DELETE FROM persoonburgstaat person where (person.ingangsdatum, person identifier) in
(select p.ingangsdatum, p.identifier from persoonburgstaat p
left join p.persoon per where per.id_persoon = :persoonID
AND per.id_persoonburgerlijkestaa > p.identifier)
the left join will make the outer query
But to be more sure post PersoonEntity entity as I think " id_persoonburgerlijkestaa " is the name of the column not the property and query will fail based on that.

OneToMany + JoinTable + OrderColumn: Order is written correctly but not read

I have an entity that can consist of itself:
class Group {
// ...
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinTable(
name = "group_group",
joinColumns = #JoinColumn(name = "id"),
inverseJoinColumns = #JoinColumn(name = "parentgroup_id")
)
#OrderColumn
private List<Group> groups = new ArrayList<Group>();
#ManyToOne
private Group parentGroup;
public List<Group> getGroups() {
return groups;
}
public Group getGroup() {
return parentGroup;
}
}
I can create a group and add two child groups:
Group parent = new Group();
Group child1 = new Group();
Group child2 = new Group();
parent.getGroups().add(child1);
parent.getGroups().add(child2);
parent = groupRepository.save(parent);
// parent.getGroups().get(0).getId() == child1.getId()
// parent.getGroups().get(1).getId() == child2.getId()
But this seems to be a coincidence. I am able to update the order (e.g. using Collections.sort) and the join table rows are updated correctly.
It does not matter how I load the parent group, the child groups are always in the order of creation. The executed SQL query is:
SELECT t1.ID, t1.PARENTGROUP_ID FROM GROUP t0, GROUP t1 WHERE ((t1.PARENTGROUP_ID = ?) AND (t0.ID = t1.PARENTGROUP_ID))
There is no ORDER BY which seems wrong. How can I persuade EclipseLink to add it?
The problem was that I tried to load the child group entities using a CrudRepository. The repository ignores the #OrderColumn annotation. I fixed it by fetching the parent group and using getGroups().
Try using the #OrderBy which does not rely on the database. You will need to provide a field from Group to order by (note field not column).
#OrderBy("fieldNameGoesHere DESC")
private List<Group> groups = new ArrayList<Group>();
OrderColumn will maintain the order of the list within the database, which carries additional performance and maintenance overhead. OrderBy puts the onus on the persistence provider to maintain the order of the entities, however the order will not persist across persistence contexts.
For more information, checkout this blog post I created regarding #OrderBy.

Using NHibernate to report a table with two sums

I have three tables: People, Purchases, Payments with 1-to-many relationships between People and Purchases, and People and Payments.
I want to generate a report of People showing the sum of their Purchases and Payments.
I can easily generate a report for all people showing the sum of the payments or purchases, vis:
var query =
DetachedCriteria.For<People>("People")
.CreateAlias("Payments", "paymentsMade");
query.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("Id"), "Id")
.Add(Projections.Sum("paymentsMade.Amount"), "TotalPayments")
Can I do this in a single query in NHibernate? Either using the criteria API (preferred) or HQL.
Try something like this:
var query = #"select
(select sum(pu.Amount) from Purchase pu where pu.People.Id = ppl.Id),
(select sum(pa.Amount) from Payments pa where pa.People.Id = ppl.Id)
from People ppl";
var results = session
.CreateQuery(query)
.List();
Or perhaps using ad-hoc mapping:
public class BulkReport
{
public double Purchases { get; set; }
public double Payments { get; set; }
}
var results = session
.CreateQuery(query)
.SetResultTransformer(Transformers.AliasToBean(typeof(BulkReport)))
.List<BulkReport>();
Another option would be using MultiQueries.