Create custom finder with spring repository - spring

I am using Spring Roo with repository layers. I have two classes: Document (with the title of the document) and FilledDocument (that contains the related Document). Like this:
public class Document {
/**
*/
#NotNull
private String titleDocument;
...
}
public class FilledDocument {
/**
*/
#OneToOne
private Document relatedDocument;
...
}
I would want to create a finder for the class FilledDocument that match for title inside of the atribute relatedDocument.
I am using a solution that isnt elegant and efficient. In my controller I have:
#RequestMapping(params = "find", produces = "text/html")
public String findFilledDocumentsByTitleDocumentContaining(#RequestParam(value = "title", required = true) String title, Model uiModel) {
LinkedList<FilledDocument> allFilledDocuments = new LinkedList<FilledDocument>();
allFilledDocuments.addAll(filledDocumentService.findAllFilledDocuments());
ArrayList<FilledDocument> filledDocuments=new ArrayList<FilledDocument>();
for( FilledDocument filledDocument : allFilledDocuments ) {
if( filledDocument.getRelatedDocument().getTitleDocument().toLowerCase().contains(title.toLowerCase()) == true ) {
filledDocuments.add(filledDocument);
}
}
uiModel.addAttribute("filleddocuments", filledDocuments);
return "filleddocuments/list";
}
I am reading this Spring Data JPA - Reference Documentation and I am trying with #Query in the repository class, like this:
#Query("select f from FilledDocument f where f.relatedDocument.titleDocument containing = ?1")
public ArrayList<FilledDocument> findFilledDocumentByTitleDocument(#Param("titleDocument") String titleDocument);
but it doesnt work. Any ideas?

If you are using Spring Data JPA 1.3.1 or newer that you can just write :
#Query("select f from FilledDocument f where f.relatedDocument.titleDocument like %:title%")
List<FilledDocument> findFilledDocumentByTitleDocument(#Param("title") String titleDocument);
If you are stuck on an older version then your code would be:
#Query("select f from FilledDocument f where f.relatedDocument.titleDocument like :title")
List<FilledDocument> findFilledDocumentByTitleDocument(#Param("title") String titleDocument);
And when you call the findFilledDocumentByTitleDocument method you need to pass
"%"+title+"%"
as an argument.
Check out this blog post for the full story and section 1.3.4 of the documentation.

Related

DAO class does not recognize the table name and id

I'm trying to save my images using room database.
Saving is not working. I guess my DAO class does not recognize the table name and id. Can you review the following pictures and help me to solve this issue?
#Entity(tableName = "my_images")
public class MyImagesEntity {
#PrimaryKey(autoGenerate = true)
public int image_id;
public String image_title;
public String image_description;
//images/sounds are stored in database as binary large object
public byte[] image;
//constructor
public MyImagesEntity(String image_title, String image_description, byte[] image) {
this.image_title = image_title;
this.image_description = image_description;
this.image = image;
}
}
The #Dao annotated interface
#Dao
public interface MyImagesDao {
#Insert
void insert(MyImagesEntity myImagesEntity);
#Delete
void delete(MyImagesEntity myImagesEntity);
#Update
void update(MyImagesEntity myImagesEntity);
#Query("SELECT * FROM my_images ORDER BY image_id ASC")
LiveData<List<MyImagesEntity>> getAllImages();
}
The dependencies
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
def lifecycle_version = "2.5.0-alpha03"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Annotation processor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
Saving is not working.
Using the code you have supplied, I don't believe that your issue is that data is not being saved as using the following additional code :-
#Database(entities = {Data.class/*<<<<< due to test using a hijacked project*/,MyImagesEntity.class}, exportSchema = false, version = 1)
abstract class AppDatabase extends RoomDatabase {
abstract DataDao getDataDao(); //<<<<< due to test using a hijacked project
abstract MyImagesDao getMyImagesDao();
private static volatile AppDatabase instance = null;
static AppDatabase getAppDatabase(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context, AppDatabase.class,"the_database.db")
.allowMainThreadQueries()
.build();
}
return instance;
}
}
and the following in an activity:-
db = AppDatabase.getAppDatabase(this);
myImagesDao = db.getMyImagesDao();
myImagesDao.insert(new MyImagesEntity("Image001","Image 1 description",new byte[]{10,11,12,13,14,15,16,17}));
myImagesDao.insert(new MyImagesEntity("Image002","Image 2 description",new byte[]{0,1,2,3,4,5,6,7,8,9,}));
for(MyImagesEntity mie: myImagesDao.getAllImagesOther()) {
Log.d("DBINFO","Image is " + mie.image_description + " Description is " + mie.image_description);
}
with getAllImagesOther being (without LiveData) :-
#Query("SELECT * FROM my_images ")
List<MyImagesEntity> getAllImagesOther();
Then the result is :-
D/DBINFO: Image is Image 1 description Description is Image 1 description
D/DBINFO: Image is Image 2 description Description is Image 2 description
and using App Inspection you can see that the data has been added :-
Therefore I believe that you issues is with how your are observing (or not) the LiveData. I'd suggest using App Inspection to see if the data is being saved.

specification-arg-resolver - URL operators

Looking at the sample code from specification-argument-resolver,
#Controller
public class SampleController {
#Autowired
CustomerRepository customerRepository;
#RequestMapping("/find")
public List<Customer> findByRegistrationDate(
#And({
#Spec(path = "name", params = "name", spec = Equal.class),
#Spec(path = "registrationDate", params = "registrationDate", spec = GreaterThanOrEqual.class)
}) Specification<Customer> spec) {
return customerRepository.findAll(spec);
}
}
#Entity
public class Customer {
String name;
Date registrationDate;
}
Looking at the sample code from specification-argument-resolver, the sample request looks like below:
GET /find?name=John&registrationDate=2020-06-19
Is there a way for the query params to reflect the actual operator.
Example: registrationDate>=2020-06-19 instead of just =
I am looking for a hybrid between RSQL and specification-argument-resolver.
You may just use this library: https://github.com/turkraft/spring-filter
It will let you run search queries such as:
/search?filter= average(ratings) > 4.5 and brand.name in ('audi', 'land rover') and (year > 2018 or km < 50000) and color : 'white' and accidents is empty
It supports dates too, and manages very well the n+1 query problem.

Spring data mongodb repository. How can I search by a list of IDs?

I have the following class!
public class Task{
ObjectId id;
String title;
String description;
/* Getters and Setters removed for brevity */
}
and I have the following mongoRepository class, very simple :
public interface TaskRepository extends MongoRepository<Task, String> {
}
As you can see, I have not yet tried to extend this class - What would I want to do here if I want to have a find method, where I could just hand it a list of Ids, and get my list of corresponding tasks back?
The CrudRepository which MongoRepository extends has a findAll method, which takes an Itereable<ID>
I think that is exactly what you are looking for.
Note that it is renamed to findAllById in the latest Milestone releases.
You can create a custom query method that searches for an array of _id values:
#Query(value = "{ '_id' : {'$in' : ?0 } }", fields = "{ 'description': 0 }")
Iterable<Task> findAllThin(Iterable<String> ids);
(in this case it returns the fields id and title only)
#Neil Lunn brought me to the answer.

Dynamic Queries in Spring Data JPA

I am looking for a solution to dynamically build queries using Spring Data JPA. I have a GameController which has a RESTful service endpoint /games which takes 4 optional parameters: genre, platform, year, title. The API may be passed none of those, all 4, and every combination in between. If any parameter is not passed it defaults to null. I need a method in the Repository that will build the appropriate query and ideally also still allow Spring Data JPA Paging, although I'm not sure if that is possible.
I found this article but this doesn't seem to be what I need unless I am misunderstanding. http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
I know JPA has a Query Criteria API but really have no idea how to implement this.
I realize I could create a method for each possible scenario but that seems like really bad practice and a lot of unnecessary code.
GameRepository:
package net.jkratz.igdb.repository;
import net.jkratz.igdb.model.Game;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface GameRepository extends JpaRepository<Game, Long> {
#Query("select g from Game g, GamePlatformMap gpm, Platform p where g = gpm.game and gpm.platform = p and p.id = :platform")
Page<Game> getGamesByPlatform(#Param("platform") Long platformId, Pageable pageable);
#Query("select g from Game g where g.title like :title")
Page<Game> getGamesByTitle(#Param("title") String title, Pageable pageable);
#Query("select g from Game g, GameGenreMap ggm, Genre ge where g = ggm.game and ggm.genre = ge and ge.id = :genreId")
Page<Game> getGamesByGenre(#Param("genre") Long genreId, Pageable pageable);
}
I would say that using QueryDSL is one way of doing what you want.
For example I have a repository defined as below:
public interface UserRepository extends PagingAndSortingRepository<User, Long>, QueryDslPredicateExecutor<User> {
public Page<User> findAll(Predicate predicate, Pageable p);
}
I can call this method with any combination of parameters, like below:
public class UserRepositoryTest{
#Autowired
private UserRepository userRepository;
#Test
public void testFindByGender() {
List<User> users = userRepository.findAll(QUser.user.gender.eq(Gender.M));
Assert.assertEquals(4, users.size());
users = userRepository.findAll(QUser.user.gender.eq(Gender.F));
Assert.assertEquals(2, users.size());
}
#Test
public void testFindByCity() {
List<User> users = userRepository.findAll(QUser.user.address.town.eq("Edinburgh"));
Assert.assertEquals(2, users.size());
users = userRepository.findAll(QUser.user.address.town.eq("Stirling"));
Assert.assertEquals(1, users.size());
}
#Test
public void testFindByGenderAndCity() {
List<User> users = userRepository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.M)));
Assert.assertEquals(2, users.size());
users = userRepository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.F)));
Assert.assertEquals(1, users.size());
}
}
For those using Kotlin (and Spring Data JPA), we've just open-sourced a Kotlin JPA Specification DSL library which lets you create type-safe dynamic queries for a JPA Repository.
It uses Spring Data's JpaSpecificationExecutor (i.e. JPA criteria queries), but without the need for any boilerplate or generated metamodel.
The readme has more details on how it works internally, but here's the relevant code examples for a quick intro.
import au.com.console.jpaspecificationsdsl.* // 1. Import Kotlin magic
////
// 2. Declare JPA Entities
#Entity
data class TvShow(
#Id
#GeneratedValue
val id: Int = 0,
val name: String = "",
val synopsis: String = "",
val availableOnNetflix: Boolean = false,
val releaseDate: String? = null,
#OneToMany(cascade = arrayOf(javax.persistence.CascadeType.ALL))
val starRatings: Set<StarRating> = emptySet())
#Entity
data class StarRating(
#Id
#GeneratedValue
val id: Int = 0,
val stars: Int = 0)
////
// 3. Declare JPA Repository with JpaSpecificationExecutor
#Repository
interface TvShowRepository : CrudRepository<TvShow, Int>, JpaSpecificationExecutor<TvShow>
////
// 4. Kotlin Properties are now usable to create fluent specifications
#Service
class MyService #Inject constructor(val tvShowRepo: TvShowRepository) {
fun findShowsReleasedIn2010NotOnNetflix(): List<TvShow> {
return tvShowRepo.findAll(TvShow::availableOnNetflix.isFalse() and TvShow::releaseDate.equal("2010"))
}
/* Fall back to spring API with some extra helpers for more complex join queries */
fun findShowsWithComplexQuery(): List<TvShow> {
return tvShowRepo.findAll(where { equal(it.join(TvShow::starRatings).get(StarRating::stars), 2) })
}
}
For more complex and dynamic queries it's good practice to create functions that use the DSL to make queries more readable (as you would for QueryDSL), and to allow for their composition in complex dynamic queries.
fun hasName(name: String?): Specifications<TvShow>? = name?.let {
TvShow::name.equal(it)
}
fun availableOnNetflix(available: Boolean?): Specifications<TvShow>? = available?.let {
TvShow::availableOnNetflix.equal(it)
}
fun hasKeywordIn(keywords: List<String>?): Specifications<TvShow>? = keywords?.let {
or(keywords.map { hasKeyword(it) })
}
fun hasKeyword(keyword: String?): Specifications<TvShow>? = keyword?.let {
TvShow::synopsis.like("%$keyword%")
}
These functions can be combined with and() and or() for complex nested queries:
val shows = tvShowRepo.findAll(
or(
and(
availableOnNetflix(false),
hasKeywordIn(listOf("Jimmy"))
),
and(
availableOnNetflix(true),
or(
hasKeyword("killer"),
hasKeyword("monster")
)
)
)
)
Or they can be combined with a service-layer query DTO and mapping extension function
/**
* A TV show query DTO - typically used at the service layer.
*/
data class TvShowQuery(
val name: String? = null,
val availableOnNetflix: Boolean? = null,
val keywords: List<String> = listOf()
)
/**
* A single TvShowQuery is equivalent to an AND of all supplied criteria.
* Note: any criteria that is null will be ignored (not included in the query).
*/
fun TvShowQuery.toSpecification(): Specifications<TvShow> = and(
hasName(name),
availableOnNetflix(availableOnNetflix),
hasKeywordIn(keywords)
)
for powerful dynamic queries:
val query = TvShowQuery(availableOnNetflix = false, keywords = listOf("Rick", "Jimmy"))
val shows = tvShowRepo.findAll(query.toSpecification())
JpaSpecificationExecutor supports paging, so you can achieve pageable, type-safe, dynamic queries!
I have got a solution for this. I wrote some code to extend the spring-data-jpa .
I call it spring-data-jpa-extra
spring-data-jpa-extra comes to solve three problem:
dynamic native query support like mybatis
return type can be anything
no code, just sql
You can try it : )

Spring Data MongoDB: Accessing and updating sub documents

First experiments with Spring Data and MongoDB were great. Now I've got the following structure (simplified):
public class Letter {
#Id
private String id;
private List<Section> sections;
}
public class Section {
private String id;
private String content;
}
Loading and saving entire Letter objects/documents works like a charm. (I use ObjectId to generate unique IDs for the Section.id field.)
Letter letter1 = mongoTemplate.findById(id, Letter.class)
mongoTemplate.insert(letter2);
mongoTemplate.save(letter3);
As documents are big (200K) and sometimes only sub-parts are needed by the application: Is there a possibility to query for a sub-document (section), modify and save it?
I'd like to implement a method like
Section s = findLetterSection(letterId, sectionId);
s.setText("blubb");
replaceLetterSection(letterId, sectionId, s);
And of course methods like:
addLetterSection(letterId, s); // add after last section
insertLetterSection(letterId, sectionId, s); // insert before given section
deleteLetterSection(letterId, sectionId); // delete given section
I see that the last three methods are somewhat "strange", i.e. loading the entire document, modifying the collection and saving it again may be the better approach from an object-oriented point of view; but the first use case ("navigating" to a sub-document/sub-object and working in the scope of this object) seems natural.
I think MongoDB can update sub-documents, but can SpringData be used for object mapping? Thanks for any pointers.
I figured out the following approach for slicing and loading only one subobject. Does it seem ok? I am aware of problems with concurrent modifications.
Query query1 = Query.query(Criteria.where("_id").is(instance));
query1.fields().include("sections._id");
LetterInstance letter1 = mongoTemplate.findOne(query1, LetterInstance.class);
LetterSection emptySection = letter1.findSectionById(sectionId);
int index = letter1.getSections().indexOf(emptySection);
Query query2 = Query.query(Criteria.where("_id").is(instance));
query2.fields().include("sections").slice("sections", index, 1);
LetterInstance letter2 = mongoTemplate.findOne(query2, LetterInstance.class);
LetterSection section = letter2.getSections().get(0);
This is an alternative solution loading all sections, but omitting the other (large) fields.
Query query = Query.query(Criteria.where("_id").is(instance));
query.fields().include("sections");
LetterInstance letter = mongoTemplate.findOne(query, LetterInstance.class);
LetterSection section = letter.findSectionById(sectionId);
This is the code I use for storing only a single collection element:
MongoConverter converter = mongoTemplate.getConverter();
DBObject newSectionRec = (DBObject)converter.convertToMongoType(newSection);
Query query = Query.query(Criteria.where("_id").is(instance).and("sections._id").is(new ObjectId(newSection.getSectionId())));
Update update = new Update().set("sections.$", newSectionRec);
mongoTemplate.updateFirst(query, update, LetterInstance.class);
It is nice to see how Spring Data can be used with "partial results" from MongoDB.
Any comments highly appreciated!
I think Matthias Wuttke's answer is great, for anyone looking for a generic version of his answer see code below:
#Service
public class MongoUtils {
#Autowired
private MongoTemplate mongo;
public <D, N extends Domain> N findNestedDocument(Class<D> docClass, String collectionName, UUID outerId, UUID innerId,
Function<D, List<N>> collectionGetter) {
// get index of subdocument in array
Query query = new Query(Criteria.where("_id").is(outerId).and(collectionName + "._id").is(innerId));
query.fields().include(collectionName + "._id");
D obj = mongo.findOne(query, docClass);
if (obj == null) {
return null;
}
List<UUID> itemIds = collectionGetter.apply(obj).stream().map(N::getId).collect(Collectors.toList());
int index = itemIds.indexOf(innerId);
if (index == -1) {
return null;
}
// retrieve subdocument at index using slice operator
Query query2 = new Query(Criteria.where("_id").is(outerId).and(collectionName + "._id").is(innerId));
query2.fields().include(collectionName).slice(collectionName, index, 1);
D obj2 = mongo.findOne(query2, docClass);
if (obj2 == null) {
return null;
}
return collectionGetter.apply(obj2).get(0);
}
public void removeNestedDocument(UUID outerId, UUID innerId, String collectionName, Class<?> outerClass) {
Update update = new Update();
update.pull(collectionName, new Query(Criteria.where("_id").is(innerId)));
mongo.updateFirst(new Query(Criteria.where("_id").is(outerId)), update, outerClass);
}
}
This could for example be called using
mongoUtils.findNestedDocument(Shop.class, "items", shopId, itemId, Shop::getItems);
mongoUtils.removeNestedDocument(shopId, itemId, "items", Shop.class);
The Domain interface looks like this:
public interface Domain {
UUID getId();
}
Notice: If the nested document's constructor contains elements with primitive datatype, it is important for the nested document to have a default (empty) constructor, which may be protected, in order for the class to be instantiatable with null arguments.
Solution
Thats my solution for this problem:
The object should be updated
#Getter
#Setter
#Document(collection = "projectchild")
public class ProjectChild {
#Id
private String _id;
private String name;
private String code;
#Field("desc")
private String description;
private String startDate;
private String endDate;
#Field("cost")
private long estimatedCost;
private List<String> countryList;
private List<Task> tasks;
#Version
private Long version;
}
Coding the Solution
public Mono<ProjectChild> UpdateCritTemplChild(
String id, String idch, String ownername) {
Query query = new Query();
query.addCriteria(Criteria.where("_id")
.is(id)); // find the parent
query.addCriteria(Criteria.where("tasks._id")
.is(idch)); // find the child which will be changed
Update update = new Update();
update.set("tasks.$.ownername", ownername); // change the field inside the child that must be updated
return template
// findAndModify:
// Find/modify/get the "new object" from a single operation.
.findAndModify(
query, update,
new FindAndModifyOptions().returnNew(true), ProjectChild.class
)
;
}

Resources