How to join fetch associated child relation - spring-data-jdbc

i have the following constellation:
public final class ApplicationUser {
private final #Id
#Wither
long applicationUserId;
#NotNull
#Size(min = 4, max = 20)
private final
String login;
#NotNull
#Email
private final
String eMail;
#Wither
private Set<Privilege> privileges;
}
Now i want to query one ApplicationUser by its login which is done by a simple sql Query.
My question now:
Is there any spring-data-jdbc related thing to jpa´s join fetch so that i can fetch the associated Set<Privilege> privileges in one declared query?
Or do i have to fire another query like so:
query ApplicationUser by its login
query Privilege by its foreign key applicationuser
Thank you

Your answer describes the default way to do this. If you want to avoid the extra select you could use a statement that contains the join and use a ResultSetExtractor to construct your entities.
Pro: More efficient at runtime.
Con: More work for you.

I should have waited with my question, because i´ve just found an answer myself.
So here is my answer:
Just type this sql query onto your repository function:
#Query("select a.applicationuserid, a.email, a.login, p from applicationuser a left outer join privilege p on a.applicationuserid = p.applicationuser where lower(a.login) = lower(:login)")
In the corresponding logs there has been fired a second sql query to fetch the privilegs.

Related

Join 2 tables using Spring JPA

I have two entities(Invoice and InvoiceItems) without adding any relationship.
Invoice
public class Invoice {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long invoiceID;
#Column(name="code")
private String code;
//other columns
}
Invoice Items
public class InvoiceItems {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long invItemID;
#Column(name="invoice_id")
private Integer invoiceId;
//other columns
}
Can I join these entities and get data without adding relationship using JPA?
If it isn't possible how to join 2 entities using JPQL or Native query?
If your data is valid then using native query you can do that
#Query(nativeQuery = true, "select * from Invoice i join InvoiceItems im on i.id = im.invoice_id")
public List<Invoice> findData();
But that is not a good way join without relation using JPA.
Yes, you can join these entities and get data without adding relationship using JPA, but it's a little bit losing the purpose of using JPA.
You need to create a java class first, which will be the returning data object from the DB. After that you can use entityManager's createNamedQuery method to get the result.
createNamedQuery(String sqlString, ResultClass.Class)
sqlString may be something like:
SELECT INV.INVOICE_ID
INV.CODE
INV_ITEMS.INV_ITEM_ID
FROM INVOICE INV
JOIN INVOICE_ITEMS INV_ITEMS
ON INV.INVOICE_ID = INV_ITEMS.INVOICE_ID;
And the corresponding ResultClass:
public class ResultClass {
private Long invoiceID;
private String code;
private Long invItemID;
// other columns
}
Or you can even use RowMapper to map the object all by yourself for more flexibility by using JdbcTemplate with query() method.

Fetch child entities when finding by a normal field in Spring Data JPA

I am using Spring Data JpaRepository to find List of entities matching a particular field. Consider the following code snippet:
Entity:
#Entity
#Table(name = "master")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Master implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator")
#Column(name = "id", nullable = false)
private Long Id;
#NotNull
#Column(name = "user_id", nullable = false)
private String userId;
#OneToOne(fetch = FetchType.EAGER)
#JoinColumn(name="id", referencedColumnName="id", insertable=false, updatable=false)
private Details Details;
Spring Data Custom JpaRepository:
public interface MasterRepository extends JpaRepository<Master,Long> {
List<Master> findMasterByUserId(String userId);
}
When i am using findBookingMasterByUserId repository method to find all records with specific user id, I am getting the List of Master entity but I am not getting the Details entity that has id as foreign key in it.
However, I get all the dependent entities when I use out of the box findAll method of JpaRepository but with custom findMasterByUserId repository method, child entities are not being fetched eagerly.
Any type of help would be highly appreciated. Thanks!
You can use #EntityGraph in your repo to eagerly get associated data:
#EntityGraph(attributePaths = {"details"})
List<Master> findBookingMasterByUserId(String userId);
P.S. Don't forget to change 'Details' field to details;
Your entity name is "Master" not "booking_master".
Change your method to:
List<Master> findByUserId(String userId);
Refer to below spring docs for more information on query creation mechanism for JPA.
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
Alternatively,
#Query("SELECT m FROM Master m WHERE m.userId = :userId")
List<Master> findByUserId(#Param("userId") String userId);
The query generation from the method name is a query generation strategy where the invoked query is derived from the name of the query method.
We can create query methods that use this strategy by following these rules:
The name of our query method must start with one of the following
prefixes: find…By, read…By, query…By, count…By, and get…By.
If we want to limit the number of returned query results, we can add
the First or the Top keyword before the first By word. If we want to
get more than one result, we have to append the optional numeric
value to the First and the Top keywords. For example, findTopBy,
findTop1By, findFirstBy, and findFirst1By all return the first entity
that matches with the specified search criteria.
If we want to select unique results, we have to add the Distinct
keyword before the first By word. For example, findTitleDistinctBy or
findDistinctTitleBy means that we want to select all unique titles
that are found from the database.
We must add the search criteria of our query method after the first
By word. We can specify the search criteria by combining property
expressions with the supported keywords.
If our query method specifies x search conditions, we must add x
method parameters to it. In other words, the number of method
parameters must be equal than the number of search conditions. Also,
the method parameters must be given in the same order than the search
conditions.

Spring Data find by inner relation

My question is about the way Spring data is generating the query .
I have two entities : Message , Sender
#Entity
public class Message extends BaseEntity {
#ManyToOne
protected Account sender;
}
I have a call to
messageDao.findBySenderId(Long id)
The result is query all columns from the two two table with a left outer join between the two tables , but my expectation was simply to just select from message table where sender_id = the passed value.
So is there a way to force selecting only the first message entity and not to join with the other one? I want simple condition in the where clause
by using findBy not custom #Query
You will need a repository like (untested) :
#Repository
public interface MessageRepository extends JpaRepository<Message, Long> {
Message findFirstBySenderId(Long id);
}
See repositories.query-methods
I think Hibernate is doing a LEFT JOIN because your #ManyToOne is optional = true (default).
Try:
#ManyTone(optional = false)
And you will see Hibernate doing a query without a JOIN, as you expected.
You can use fetch type LAZY for the associations types:
#ManyToOne(optional = false, fetch = FetchType.LAZY)

Sort by joined table's field Spring JPA

I have two entity classes Request,User:
//Ommiting some annotations for brevity
public class User{
private Long id;
private String name;
private Integer age;
}
public class Request{
private Long id;
private String message;
private Date createTime;
#ManyToOne
#JoinColumn(name="user_id")
private User user;
}
I can sort request list by create time :
Sort = new Sort(Direction.ASC,"createTime");
Is there a possible way to sort request list by User's name? Like:
Sort = new Sort(Direction.ASC,"User.name");
Yes. new Sort(Direction.ASC,"user.name"); should work just fine.
Spring Data JPA will left outer join the User to the Request and order by the joined column's name resulting in SQL like this:
select
id, message, createTime
from
Request r
left outer join User u on u.id = r.user_id
order by
u.name asc
This works great on one-to-one and, like you have here, many-to-one relationships but because a left outer join is employed to implement the order by clause, if the joined entity represents a many relationship (like in a one-to-many), the Sort may result in SQL in which duplicate records are returned. This is the case because the Sort parameter will always result in a new left outer join even if the entity being joined is already joined in the query!
Edit Incidentally, there is an open ticket concerning this issue: https://jira.spring.io/browse/DATAJPA-776

JPA Why only this #ManyToOne returns null?

UPDATE
I find something interesting
When I run JPQL like this
SELECT s FROM STUDENTS s WHERE s.community=:community
then this issue happens, but this query runs fine and returns all necessary fields
SELECT s FROM STUDENTS s WHERE s.id=:id
meaning if there is an inner join with its child field, then somehow the value of the other field get missing. I got both native query and both returns all necessary field values. Must be openjpa dismiss the school fields some how when native query returns
Yes, Community and School both may map to another entity, but how come that could cause this strange behavior? I am kind of mad with OpenJPA
I have spent hours to fix this strange issue
there is a class contains several #ManyToOne relation
public class Student{
// Relationships
#NotNull
#ManyToOne(fetch=FetchType.EAGER)
private Teacher teacher;
#ManyToOne(fetch=FetchType.EAGER)
#Column(name = "SCHOOL_ID")
private School school;
#ManyToOne(fetch=FetchType.EAGER)
private Club club;
#ManyToOne(fetch=FetchType.EAGER)
private Bus bus;
....
}
Each of the many side has a definition like this(Only give the School entity as it has the issue):
public class School implements Institution{
// Relationships
#OneToMany(mappedBy="school")
private List<Student> students= new ArrayList<Student>();
....
}
The problem here is when I try to get All students for a certain age:
SELECT s FROM STUDENTS s WHERE s.age=:age
I can clearly see the all other #ManyToOne fields' value ONLY EXCEPT school, and in Oracle database the school_id field clearly stored with correct data which map to the correct entry in School table
So what could be the possible reason for this situation?
I am using Spring MVC3 + Openjpa + Roo
Your school mapping should use a #JoinColumn annotation (not a #Column annotation):
#ManyToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "SCHOOL_ID")
private School school;

Resources