Springboot : How do i perform complex queries in MongoDB like getting a list of a property of an Object and not the whole object in Mongo DB - spring-boot

I have a Springboot app which uses contains 2 Entities, User and Friend Request. The user Entity looks like this:
public class User{
#Id
private BigInteger id;
private String firstName;
private String lastName;
private String email;
private String password;
}
while the FriendRequest Object looks like this:
public class FriendRequest{
#Id
private BigInteger id;
private User sender;
private FriendRequestStatus friendRequestStatus;
private User recipient;
}
The friend request class has 4 properties:
The ID
the sender of a request which is a user
the recipient of a request which is also user
The friendRequestStatus - which can be Pending (if the request has not been accepted), Accepted (if the friend request has been accepted), or Declined (if the request has been declined).
instead of me making a friends table, i want to use the FriendRequest table to get all the friends of a particular user - it works by getting all friends request where a Specific User is a send or receiver and friendRequest is Accepted. Then going further to select the Sender as a friend if the recipient is the user whos friends you are looking for and selecting the recipient if the Sender was the user whos friends you are looking for. At the end i want to get a List.
My problem is i dont know how to achieve this in MongoDB for springboot , when using Postgre this was the query i ran, and it worked perfectly:
#Query(value = "SELECT * \n" +
"FROM ( \n" +
"SELECT DISTINCT CASE sender WHEN 1 THEN recipient ELSE sender END AS friend_id \n" +
"FROM friendrequest WHERE 1 IN (recipient, sender) \n" +
"AND friendrequeststatus = 'ACCEPTED' \n" +
") f \n" +
"LEFT JOIN users u\n" +
"ON f.friend_id = u.id\n" +
"ORDER BY friend_id \n-- #pageable\n"
,nativeQuery = true)
Page<User> getFriends(Pageable pageable);
but how to replicate it in MongoDB for Springboot is a big problem

Related

Adding and removing dynamically SQL WHERE clause using JPA [duplicate]

Is there a way in Spring data to dynamically form the where clause?
What I want to do is have a method (which is like the findBy / get method) which runs a WHERE and AND using the mentioned properties which are NOT NULL.
For example,
Consider the object Person [firstName, lastName, age, gender]
Our method looks something like this
findBy_IfNotNullFirstName_AndIfNotNullLastName_AndIfNotNullAge_AndIfNotNullGender(String firstName, String lastName, Integer age, String gender)
Thanks.
A simpler option is to test if the parameter is null right in the JPQL query:
Exemple from my project:
#Query("select m from MessageEntity m " +
"join fetch m.demandeAnalyseEntities d " +
"where (:patientId is null or d.noPtn= :patientId) " +
" and " +
" ( :labNbr is null or d.noLab= :labNbr) " +
" and " +
" ( :reqDate is null or d.dteReq= :reqDate) " +
" and " +
" ( :reqNum is null or d.noReq= :reqNum) "
)
List<MessageEntity> findMessagesWithDemandesOnly(#Param("patientId") Long pid,
#Param("labNbr") Integer labNo,
#Param("reqDate") String reqDate,
#Param("reqNum") Integer reqNum,
Pageable pageable);
Take a look at JPA Specification and Predicate, and Even better QueryDSL, there both supported by spring data repositories.
This article provide an example:
http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
Another solution: You can extend your JPA repo interface using custom fragment interfaces.
Define your custom methods on a new interface
public interface PersonFragRepository {
List<User> findPersonByWhatever(
String firstName, String lastName, String age, String gender);
}
Provide the implementation
public class PersonFragRepositoryImpl implements PersonFragRepository {
#PersistenceContext
private EntityManager entityManager;
#Override
List<User> findPersonByWhatever(
String firstName, String lastName, String age, String gender) {
...
}
}
Extends your JPA interface
public interface PersonRepository
extends JpaRepository<Person, Integer>, PersonFragRepository

Spring JPA - insert list with batch_size using native query in JpaRepository or CrudRepository

UPDATE: Thank you to #M.Deinum for informing me how to deal with the #ManyToOne cascade issue that I was previously stuck on by using EntityManager getReference or JpaRepository getOne function. I am now able to batch save with basic JpaRepsitorymethods as follows:
#Transactional
public void insertCommands(List<CommandDto> dtos) {
final List<Command> commands = new ArrayList<>();
for (CommandDto dto : dtos) {
ZonedDateTime now = ZonedDateTime.now();
final Request request = requestRepository.getOne(dto.getRequestId());
String commandId = UUID.randomUUID().toString();
final Command command = new Command();
command.setId(commandId);
command.setCreatedBy(SYSTEM);
command.setCreatedTimestamp(now);
command.setStatus(dto.getStatus());
command.setContent(dto.getContent());
command.setSendOnDate(dto.getSendOnDate());
command.setRequest(request);
commands.add(command);
}
commandRepository.saveAll(commands);
}
Original post content as seen below:
I need to insert multiple rows to my application's database using the batch_size property set in my properties:
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_size=10
I am looking for a way to insert them using nativequery in JpaRepository or CrudRepository with a syntax along the lines of the following, but allowing for multiple rows:
#Modifying
#Transactional
#Query(nativeQuery = true, value = "INSERT into command (id, created_by, created_timestamp, " +
" updated_by, updated_timestamp, status, content, send_on_date, request_id) " +
"VALUES (:id, :createdBy, :createdTimestamp, :updatedBy, :updatedTimestamp, " +
" :status, :content, :sendOnDate, :requestId) ")
int batchInsertCommandDto( #Param("commands") List<CommandDto> commandDtos);
How can I perform this sort of query with a list?
NOTE: Before you bring up EntityManger, I must note that have not had any luck with saveall functionality because:
I am translating the data from it's JSON-friendly class (CommandDto) to its data entity class(Command)
The class for the entity (Command) has a #ManyToOne annotation for one or more Object fields whereas the JSON object (CommandDto) simply has the id of these fields. For example, java class for command entity has "request" field:
#ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
#JoinColumn(name = "request_id", foreignKey = #ForeignKey(name="fk_rcommand_request_id"))
private Request request;
Whereas CommandDto object simply has field "requestId". That means that if I simply try creating a request object with only the requestId, entityManager will fail to save because the Request object is not fully formed and therefore not recognized. It would be grossly inefficient to retrieve the Request object for each command being saved, so I am looking to do the mapping as seen in the nativequery above.

Bulk data to find exists or not : Spring Data JPA

I get an Post request that would give me a List<PersonApi> Objects
class PersonApi {
private String name;
private String age;
private String pincode ;
}
And I have an Entity Object named Person
#Entity
#Table(name = "person_master")
public class Person{
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
#Column(name = "name")
String name;
#Column(name = "age")
String age;
#Column(name = "pincode ")
String pincode ;
}
My record from Post request would look something like this (pseudocode representation of the data below)
[
"Arun","33","09876gh"
"James","34","8765468"
]
I need to do a bulk-validation using Spring JPA.. Give the List<PersonApi> and get a True or False based on the condition that all the entries in the PersonApi objects list should be there in the database.
How to do this ?
The selected answer is not a right one. (not always right)
You are selecting the whole database to check for existence. Unless your use case is very special, i.e. table is very small, this will kill the performance.
The proper way may start from issuing repository.existsById(id) for each Person, if you never delete the persons, you can even apply some caching on top of it.
exists
Pseudo Code:
List<PersonApi> personsApiList = ...; //from request
List<Person> result = personRepository.findAll();
in your service class you can access your repository to fetch all database entities and check if your list of personapi's is completeley available.
boolean allEntriesExist = result.stream().allMatch(person -> personsApiList.contains(createPersonApiFromPerson(person)));
public PersonApi createPersonApiFromPerson(Person person){
return new PersonApi(person.getName(), person.getAge(), person.getPincode());
}

Initialize field with current year and assigned ID

I've got an entity that I am persisting. Its ID is automatically assigned when storing it into the database via Spring Repository.
In the same entity, I have a field build from the Id and the current year: "<current_year>-<id>".
In a method annotated with #PrePersist, the ID has not been assigned yet, so I wrote some code in a #PostPersist method:
#PostPersist
protected void setupOrderNumber() {
this.orderNumber = Calendar.getInstance().get(Calendar.YEAR) + "-" + id;
}
This code does not store the orderNumber into the database, because the entity was stored already.
How can I achieve such a result with JPA directly within the entity?
If not possible with JPA, I could use Hibernate with a #Formula annotation, but I am not sure how to write it: #Formula("extract(year from current_date) + '-' + id") does not seem to work.
As you've already noticed: In #PrePersist a generated ID is not available - just because the ID is set afterwards when persisting into the database. And no changes made in #PostPersist are persisted, just because the persist has already taken place...
You can use a #Formula, as long you don't need the value in the database. But I wouldn't use extract(year from current_date) - as this would change the orderNumber when the year changes - what is different to your experiment with #PostPersist.
Instead use a year field, which you initialize in #PrePersist and reference that one in your formula:
#Entity
public class MyEntity {
#Id
#GeneratedValue(...)
private Long id;
private int year;
#Formula("concat(id, '-', year)")
private String orderNumber;
#PrePersist
private void prePersist() {
this.year = Calendar.getInstance().get(Calendar.YEAR);
}
#PostPersist
private void postPersist() {
this.orderNumber = id + "-" + year;
}
}
I initialize the orderNumber in postPersist() as well, to have a valid value immediately after EntityManager.persist().

Spring data JPA query with parameter properties

What is the simplest way of declaring a Spring data JPA query that uses properties of an input parameter as query parameters?
For example, suppose I have an entity class:
public class Person {
#Id
private long id;
#Column
private String forename;
#Column
private String surname;
}
and another class:
public class Name {
private String forename;
private String surname;
[constructor and getters]
}
... then I would like to write a Spring data repository as follows:
public interface PersonRepository extends CrudRepository<Person, Long> {
#Query("select p from Person p where p.forename = ?1.forename and p.surname = ?1.surname")
findByName(Name name);
}
... but Spring data / JPA doesn't like me specifying property names on the ?1 parameter.
What is the neatest alternative?
This link will help you: Spring Data JPA M1 with SpEL expressions supported. The similar example would be:
#Query("select u from User u where u.firstname = :#{#customer.firstname}")
List<User> findUsersByCustomersFirstname(#Param("customer") Customer customer);
https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions
Define the query method with signatures as follows.
#Query(select p from Person p where p.forename = :forename and p.surname = :surname)
User findByForenameAndSurname(#Param("surname") String lastname,
#Param("forename") String firstname);
}
For further details, check the Spring Data JPA reference
What you want is not possible. You have to create two parameters, and bind them separately:
select p from Person p where p.forename = :forename and p.surname = :surname
...
query.setParameter("forename", name.getForename());
query.setParameter("surname", name.getSurname());
You could also solve it with an interface default method:
#Query(select p from Person p where p.forename = :forename and p.surname = :surname)
User findByForenameAndSurname(#Param("surname") String lastname,
#Param("forename") String firstname);
default User findByName(Name name) {
return findByForenameAndSurname(name.getLastname(), name.getFirstname());
}
Of course you'd still have the actual repository function publicly visible...
You can try something like this:
public interface PersonRepository extends CrudRepository<Person, Long> {
#Query("select p from Person AS p"
+ " ,Name AS n"
+ " where p.forename = n.forename "
+ " and p.surname = n.surname"
+ " and n = :name")
Set<Person>findByName(#Param("name") Name name);
}
if we are using JpaRepository then it will internally created the queries.
Sample
findByLastnameAndFirstname(String lastname,String firstname)
findByLastnameOrFirstname(String lastname,String firstname)
findByStartDateBetween(Date date1,Date2)
findById(int id)
Note
if suppose we need complex queries then we need to write manual queries like
#Query("SELECT salesOrder FROM SalesOrder salesOrder WHERE salesOrder.clientId=:clientId AND salesOrder.driver_username=:driver_username AND salesOrder.date>=:fdate AND salesOrder.date<=:tdate ")
#Transactional(readOnly=true)
List<SalesOrder> findAllSalesByDriver(#Param("clientId")Integer clientId, #Param("driver_username")String driver_username, #Param("fdate") Date fDate, #Param("tdate") Date tdate);
The simplicity of Spring Data JPA is that it tries to interpret from the name of the function in repository without specifying any additional #Query or #Param annotations.
If you are supplying the complete name, try to break it down as firstname and lastname and then use something like this -
HotelEntity findByName(String name);
My HotelEntity does contain the field name so JPA tries to interpret on its own to infer the name of the field I am trying to query on and create a subsequent query internally.
Some more evidence from JPA documentation -
Further details - here
Are you working with a #Service too? Because if you are, then you can #Autowired your PersonRepository to the #Service and then in the service just invoke the Name class and use the form that #CuriosMind... proposed:
#Query(select p from Person p where p.forename = :forename and p.surname = :surname)
User findByForenameAndSurname(#Param("surname") String lastname,
#Param("forename") String firstname);
}
and when invoking the method from the repository in the service, you can then pass those parameters.
for using this, you can create a Repository for example this one:
Member findByEmail(String email);
List<Member> findByDate(Date date);
// custom query example and return a member
#Query("select m from Member m where m.username = :username and m.password=:password")
Member findByUsernameAndPassword(#Param("username") String username, #Param("password") String password);
#Autowired
private EntityManager entityManager;
#RequestMapping("/authors/{fname}/{lname}")
public List actionAutherMulti(#PathVariable("fname") String fname, #PathVariable("lname") String lname) {
return entityManager.createQuery("select A from Auther A WHERE A.firstName = ?1 AND A.lastName=?2")
.setParameter(1, fname)
.setParameter(2, lname)
.getResultList();
}

Resources