How to make native query for nested projection in Spring Data JPA - spring-data-jpa

I need to write a native query for my projection with nested interfaces.
My TransactionView interface:
public interface TransactionView {
Long getId();
TransactionType getType();
LocalDate getDate();
AccountProjection getAcc1();
AccountProjection getAcc2();
interface AccountProjection {
String getName();
CurrencyName getCurrencyCode();
BigDecimal getBalance();
}
BigDecimal getAmount();
PartnerView getPartner();
interface PartnerView {
String getName();
}
String getComment();
CategoryView getCategory();
interface CategoryView {
String getName();
}
}
JpaRepository:
public interface TransactionsRepository extends JpaRepository<Transaction, Long> {
List<TransactionView> findByAcc1PersonIdOrderByDateDesc(int personId);
}
This approach works good and I get JSON like this:
[{
"id":34,
"type":"TRANSFER",
"comment":"test comment",
"date":"2022-12-23",
"amount":200.00,
"acc2":
{
"name":"cash",
"currencyCode":"USD",
"balance":200.00
},
"acc1":
{
"name":"test acc",
"currencyCode":"USD",
"balance":700.00
},
"partner":null,
"category":null
},
{
"id":20,
"type":"EXPENCE",
"comment":"",
"date":"2022-12-13",
"amount":33.07,
"acc2":null,
"acc1":
{
"name":"cash",
"currencyCode":"BYN",
"balance":322.33
},
"partner":
{
"name":"bmw"
},
"category":
{
"name":"auto"
}
}]
But Hibernate generates a very complex query with a lot of extra columns fetching.
My native query returns null nested objects:
#Query(value = "SELECT t.id AS id, " +
"t.transaction_type AS type, " +
"t.transaction_date AS date, " +
"t.amount AS amount, " +
"t.comment AS comment, " +
"a1.balance AS acc1Balance, " +
"a1.currency_code AS acc1CurrencyCode, " +
"a1.name AS acc1Name, " +
"a2.balance AS acc2Balance, " +
"a2.currency_code AS acc2CurrencyCode, " +
"a2.name AS acc2Name, " +
"par.name AS partnerName, " +
"cat.name AS categoryName, " +
"cat.category_type AS categoryType " +
"FROM transaction t " +
"LEFT OUTER JOIN account a1 ON t.acc1_id=a1.id " +
"LEFT OUTER JOIN person per ON a1.person_id=per.id " +
"LEFT OUTER JOIN account a2 ON t.acc2_id=a2.id " +
"LEFT OUTER JOIN partner par ON t.partner_id=par.id " +
"LEFT OUTER JOIN category cat ON t.category_id=cat.id " +
"WHERE per.id=?1 ORDER BY t.transaction_date DESC", nativeQuery = true)
List<TransactionView> findByAcc1PersonIdOrderByDateDescTest(int personId);
[{
"id":34,
"type":"TRANSFER",
"comment":"test comment",
"date":"2022-12-23",
"amount":200.00,
"acc2":null,
"acc1":null,
"partner":null,
"category":null
},
{
"id":20,
"type":"EXPENCE",
"comment":"",
"date":"2022-12-13",
"amount":33.07,
"acc2":null,
"acc1":null,
"partner":null,
"category":null
}]
Also I tried approach from Peter Gyschuk, but it doesn't work.
How can I solve it using native query?

Related

Is there best solution for get entity from PostgreSQL

I have two entities
Author entity with atributes Id, firstName, secondName, lastName and transiet attribute numberBooks
Book entity with attirbutes Id, name and authorIds which is array of uuid
Question:
Is there best solution to get list of authors with numberBooks. NumberBooks need calculate from book entity
My solution is
//Author service
fun getListAuthors(): List<Author> {
val result = repository.findAllByDeletedAtIsNull()
setNumberBooksToAuthors(result)
return result
}
private fun setNumberBooksToAuthors(authors: List<Author>) {
val authorNumberBooks = repository.getAuthorsByDeletedAtIsNull(authors.map { it.id!! }.toList())
.map {
UUID.fromString(it["id"]) to Integer.parseInt(it["count"])
}.toMap()
for (author in authors) {
if (authorNumberBooks.containsKey(author.id)) {
author.numberBooks = authorNumberBooks[author.id]!!
}
}
}
//Author repository
#Query(
"SELECT CAST(author.id as varchar), CAST((SELECT COUNT(*) FROM books as b " +
"WHERE author.id = ANY(b.author_ids) AND b.deleted_at IS NULL) as varchar) " +
"from book_authors as author " +
"WHERE author.deleted_at IS NULL AND author.id in :authorIds", nativeQuery = true
)
fun getAuthorsByDeletedAtIsNull(authorIds: List<UUID>): MutableList<MutableMap<String, String>>

Creating new Objects in JPQL returns only one object

I have a JPA Entity with a large number of fields and millions of records. For a report, I need only some of the fields. So I want to create an Object with the essential fields due to resource constraints. I tried to create a list of new objects in JPQL. It gives only one object as a result, where multiple objects are expected.
String j;
j = "select new lk.gov.health.phsp.pojcs.ClientEncounterComponentBasicDataToQuery("
+ "f.name, "
+ "f.code, "
+ "f.item.code, "
+ "f.shortTextValue, "
+ "f.integerNumberValue, "
+ "f.longNumberValue, "
+ "f.realNumberValue, "
+ "f.booleanValue, "
+ "f.dateValue, "
+ "f.itemValue.code"
+ ") ";
j += " from ClientEncounterComponentItem f "
+ " where f.retired=false "
+ " and f.encounter.id=:eid";
Map m = new HashMap();
m.put("eid", endId);
The EBJ is as follows.
public List<Object> findObjects(String jpql, Map<String, Object> parameters, TemporalType tt) {
TypedQuery<Object> qry = getEntityManager().createQuery(jpql, Object.class);
Set s = parameters.entrySet();
Iterator it = s.iterator();
while (it.hasNext()) {
Map.Entry m = (Map.Entry) it.next();
Object pVal = m.getValue();
String pPara = (String) m.getKey();
if (pVal instanceof Date) {
Date pDate = (Date) pVal;
qry.setParameter(pPara, pDate, TemporalType.DATE);
} else {
qry.setParameter(pPara, pVal);
}
}
try {
return qry.getResultList();
} catch (Exception e) {
return null;
}
}
What am I doing wrong? I am using EclipseLink as the persistence provider.

Invalid JSON number '2L'. shard key being serialized to number but defined as string?

Exception:
System.FormatException: Invalid JSON number '2L'.
at MongoDB.Bson.IO.JsonScanner.GetNumberToken(JsonBuffer buffer, Int32 firstChar)
at MongoDB.Bson.IO.JsonScanner.GetNextToken(JsonBuffer buffer)
at MongoDB.Bson.IO.JsonReader.PopToken()
at MongoDB.Bson.IO.JsonReader.ReadBsonType()
at MongoDB.Bson.Serialization.Serializers.BsonDocumentSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
Shard key definition, which is a composite key created by the constructor;
public string ShardKey { get; set; }
Query code:
public virtual async Task<ICollection<City>>(City city)
{
string shardKey = city.CityNumber + city.ShortName;
return await MongoDb.CitiesCollection.Aggregate().Match({
"{ 'Time': { $elemMatch: { $eq: 0 } }," + " ShardKey: " + shardKey + " }")
.ToListAsync();
}
Why does this fail?
Wrap the value containing the number like:
" ShardKey: '" + shardKey + "' }"
with single quotation marks.
I haven't been able to find this documented anywhere though. But it makes the my integration test run so :)

createNativeQuery - return Page

I have the following custom implementation of my repository.
public final EntityManager entityManager;
public ImputacionRepositoryImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
#Override
public List<ImputacionData> imputacionesList() {
Query q = entityManager.createNativeQuery("SELECT " + "empleado.id as empleadoId,"
+ "MAX(imputacion.dia) as dia," + "imputacion.id as id " + "FROM rrhh.empleado empleado "
+ "JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id "
+ "WHERE empleado.id NOT IN (SELECT empleado.id " + "FROM rrhh.empleado empleado "
+ "LEFT JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id " + "WHERE "
+ "(imputacion.dia >= '2017-06-01' AND imputacion.dia <= '2017-10-31') "
+ "GROUP BY empleado.id,imputacion.empleado, imputacion.id) " + "AND true=true "
+ "GROUP BY empleado.id,imputacion.empleado, imputacion.id ORDER BY imputacion.dia DESC");
List<ImputacionData> imputaciones = q.getResultList();
return imputaciones;
}
And I need that instead of returning a List<ImputacionData>, return Page<ImputacionData>, but I do not know if it is possible, and how to do it.
So you can use page implementation in Spring, in Your case it's may look like:
#Override
public Page<ImputacionData> imputacionesList() {
Query q = entityManager.createNativeQuery("SELECT " + "empleado.id as empleadoId,"
+ "MAX(imputacion.dia) as dia," + "imputacion.id as id " + "FROM rrhh.empleado empleado "
+ "JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id "
+ "WHERE empleado.id NOT IN (SELECT empleado.id " + "FROM rrhh.empleado empleado "
+ "LEFT JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id " + "WHERE "
+ "(imputacion.dia >= '2017-06-01' AND imputacion.dia <= '2017-10-31') "
+ "GROUP BY empleado.id,imputacion.empleado, imputacion.id) " + "AND true=true "
+ "GROUP BY empleado.id,imputacion.empleado, imputacion.id ORDER BY imputacion.dia DESC");
Page<ImputacionData> page = new PageImpl<>(q.getResultList());
return page;
}

How to write a dynamic where 'like' query in Entity framework?

Here is my code:
//order my baselist is context.Entity
public static GridData Getdata<T>(ObjectSet<T> baseList,
int currentPage,
int rowsPerPage,
string sortcolumn,
string sortord,
string searchQuery,
string searchColumns)where T: class{
var query = baseList.OrderBy("it." + sortcolumn + " " + sortord);
string strPredicate = string.Empty;
if (!string.IsNullOrEmpty(searchColumns))
{
strPredicate = "it." + searchColumns + " LIKE #" + searchColumns + " ";
query = baseList.Where(strPredicate, new ObjectParameter(searchColumns, searchQuery)).OrderBy("it." + sortcolumn + " " + sortord);
}
}
My problem is i am trying to write down or form a like query in entity framework and seems like it does not support it.
You can use .Contains which is the LIKE operator equivalent in entity framework.
you can use this
query = baseList.Where(baseli=>baseli.Contains(searchColumns )).OrderBy("it." + sortcolumn + " " + sortord);
:)