Spring-data-jpa projection generating query incorrectly - spring-data

I've made a sample project that uses spring-data-jpa projection feature, but it's generating incorrect query. Instead of fetching declared field only it's fetching the whole entity. Here is the link for sample project:
https://github.com/ravshansbox/spring-data-jpa-projection-demo
Can anyone explain the reason?

I found the problem, it was the version. Upgrading spring-boot to 1.4.0.RELEASE solved the issue.

I have exactly the same problem and I am already using spring-boot 1.5.1.RELEASE.
Are you sure updating spring-boot to 1.4.0.RELEASE fixed your issue and not something different?
My interface:
public interface CommentWithoutData {
public Long getId();
public String getUsername();
}
Generated (SQL) query: select comment0_.id as id1_0_, comment0_.created as created2_0_, comment0_.data as data3_0_, comment0_.username as username4_0_ from comment comment0_
Regards

Related

QueryDsl Spring Data Mongodb projecting SpringDataMongodbQuery

I wanted to know if there was a way to use QueryDsl's TypeSafe approach to perform a projection on a query in conjunction with Spring Data.
Like for example :
public interface ColorCollectionRepository extends MongoRepository<ColorCollection, String>, QuerydslPredicateExecutor<ColorCollection> {
Optional<ColorCollection> findByCodeIs(String code);
List<ColorCollection> findAll(Predicate predicate);
}
And running a query where I would like to exclude a field named "references"
QColorCollection qColorCollection = QColorCollection.colorCollection;
qColorCollection.code.eq("code");
qColorCollection.exclude("references");
Thanks
Querydsl doesn't support projections for mongodb as of now. You need to implement custom solution for it to work.
I have developed a custom solution which is useful if you want to include specific fields. You may check it here - https://stackoverflow.com/a/66152979/8949877

Spring Boot Couchbase Reactive isn't supporting Pagination

I'm trying to implement Pagination in my Reactive WebFlux app and my DB is Couchbase.
The Spring Data Couchbase doc allows me to pass Pageable as an argument in my Repository.
Flux<Person> findByFirstnameOrderByLastname(String firstname, Pageable pageable);
However, when I try to implement it I get the below error:
Caused by: java.lang.IllegalStateException: Method has to have one of the following return types! [interface org.springframework.data.domain.Slice, interface java.util.List, interface org.springframework.data.domain.Page]
My Repository method looks like this:
Flux<Building> findAll(Pageable pageable);
However, if I use this workaround, I've no problem.
#Query("#{#n1ql.selectEntity} where #{#n1ql.filter} LIMIT $1 OFFSET $2")
Flux<Building> findAll(Integer limit, Integer offset);
Is this a bug? Or, am I using it wrong?
Spring Boot version: 2.2.7.RELEASE
Full Repository:
#Repository
public interface BuildingRepository extends ReactiveSortingRepository<Building, String> {
#Query("#{#n1ql.selectEntity} where #{#n1ql.filter} LIMIT $1 OFFSET $2")
Flux<Building> findAll(Integer limit, Integer offset);
//This works if I comment the below
Flux<Building> findAll(Pageable pageable);
}
The short answer is that the documentation is not current, and the best solution is to implement paging yourself with limit and offset as you have done. There is work underway to remedy this in https://jira.spring.io/browse/DATACOUCH-588 (it's not described there, but that's the tracking issue)
Even so, a more efficient way of paging is key-set pagination ( https://use-the-index-luke.com/no-offset ) - but you’ll need to implement that in your application. It uses the indexes to get the items beginning with the first one required, instead of “skipping” over the ones in prior pages.

Spring JPA findBy accent

I am developing a spring-data-jpa application.
I 've ridden the repository with findBy but does not work when I look content with an accent. Does anyone know why?
I am using the following:
Page<Dades> findByNomcomercialIgnoreCaseContaining (#Param ("nomcom") String nomcom, Pageable pageable);
The database is Oracle.
Thanks for your interest.
It's because ignoreCase does not deal with accents. It only compares both String after uppercasing them. See the documentation for more informations.
I don't know any simple solution to ignore accents with Spring Jpa. You coud either :
Remove this parameter in your query, and filter the result in Java afterward (comparing Strings after doing org.apache.commons.lang3.StringUtils.stripAccents).
Use Spring Jpa Specifications. Again, it provides only functionslike upper, like, ... so you have to write your own, depending on what database you are using.
#Param is not required. if you have any query i.e if you are using #Query then we need use #param to pass the specific value releated to query.Below you can check how to use #Query and #Param
#Query("SELECT t.title FROM Todo t where t.id = :id")
String findTitleById(#Param("id") Long id);
if you are not added these annotation use in your repository layer
#Transactional
#Repository
Check another thing i.e
#Transactional
#Repository
public interface IXyzRepository extends CrudRepository<ABC,Integer>{
//some methods you have
}
whether the primary id of class ABC is of type Integer

Spring LDAP JPA - Limit number of result

I'm using spring-ldap 2.0.4 and spring-data-jpa 1.9.0.
I built a JPA repository like this :
public interface PersonRepo extends LdapRepository<Person> {
Person findByUid (String uid);
#Query("(&(attribute=*{0}*)(attribute2=X)(attribute3=Y))")
List<Person> findByAttributeContains(String attribute);
}
So far everything is fine. I could write methods that fill my needs thanks to query methods.
For some queries i had to use #Query annotation because they were many and operator.
But i would like to limit the number of result to return from my second query method.
I know there is there is the Top and First keywords to define query methods in spring JPA. But I didn't manage to get it work. Plus I want to use multiple and operator in the method.
Thanks
I managed to limit the number of result using XML LDAP configuration.
<ldap:ldap-template context-source-ref="contextSource" count-limit="100"/>

JPA QL "IN" collection doesn't work for openjpa 1.2.2

JPA 1 with OpenJPA 1.2.2 in a unit-test backed Hsqldb 1.8. The following JPA QL doesn't work with a "IN" on a List of ids doesn't work.
#Transactional
public void updateSettlementId(List<Long> voucherIds, Long settlementId) {
String query = "UPDATE VoucherProcessed vp SET vp.settlement.id=:settlementId where vp.voucher.id IN (:voucherIds)";
em.createQuery(query).setParameter("settlementId", settlementId)
.setParameter("voucherIds", StringUtils.join(voucherIds.iterator(), ","))
.executeUpdate();
}
java.sql.SQLException: Table not found in statement [UPDATE VOUCHER_PROCESSED t0 SET t1.settlement_id = ? WHERE (t0.voucher_id IN (?))]
VOUCHER_PROCESSED table is being created in hsqldb while the test is run so the above error seems erroneous and misleading.
Suggestions?
Try this:
.setParameter("voucherIds", voucherIds)
works with Hibernate, but (please correct me if I'm wrong) JPA specification does not define IN construct allow using collection in setParameter() for queries like "IN (:voucherIds)", it is vendor specific.