I have a JpaRepository:
public interface UserRepository extends JpaRepository<User, Long> {
List<User> getByFirstName(String firstName);
}
But how to search in several values? I need something like this:
List<User> getByFirstNames(List<String> firstNames);
You need to change the signature of the method to:
List<User> getByFirstNameIn(List<String> firstNames);
Take a look at all the supported method of Spring Data JPA on their reference.
The following code is working,
Code in Repository,
List<Shop> findByNameIn(List<String> names);
Code in controller,
List<String> names=new ArrayList<String>();
names.add("gunaa");
names.add("pranav");
List<Shop> sl=shopService.findByNameIn(names);
for(Shop s:sl)
System.out.println(s.getName());
Related
I have a PostgreSQL table where channel_ids field is BIGINT[].
In console I can search like so:
select * FROM user where 10000=ANY(channel_ids).
But I can't figure out how to build up the Specification for search in JpaRepository.
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findAll(Specification<User> spec, Pageable pageable);
}
Is it even supported?
My UserRepository:
public interface UserRepository extends CrudRepository<User, Integer> {
List<User> findAll(List<Integer> ids);
}
Error:
Caused by:
org.springframework.data.mapping.PropertyReferenceException: No
property findAll found for type User
Refer - http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html?is-external=true#findAll-java.lang.Iterable-
Can some one tell me how to get list of User objects based on List of Id's.
This is working
#Query(" select new User(id,x,y,z) from User b where b.id in ?1 ")
List<User> findById(List<Integer> id);
Firstly, I would rename the repository to UserRepository, because having 2 User classes is confusing.
findAll(), by definition, is meant to get all the models with no criteria. You should add a method named
findByIdIn(Collection<Integer> ids)
Use List<User> findAll(Iterable<Integer> ids) or List<User> findByIdIn(List<Integer> ids)
I want to add in my Repository interface a method which find all the data greater than a long publishdata value and Order it Decreacingly:
I tried this, but it doesn't seems to be working:
#Repository
public interface NoticiaRepository extends CrudRepository<Noticia,Long>{
Noticia findById(long id);
List<Noticia> findByOrderPublishdateGreaterThanDesc(long publishdate);
}
List<Noticia> findByPublishdateGreaterThanOrderByPublishdateDesc(Long publishdate)
I have a field in my mongodb called "name". I am using annotations in spring data to support querying. My question is, is there a way to support wildcard? i.e. If I have values for "name" called "Robert", "Roberto" "Ramano", I could support queries that allow me to pass say "R" to a function, and it will match on everything that starts with R? Right now I have to basically do an "exact spelling" of Robert, or any one of those names to get an exact match.
I know how to do wildcard search with mongodb directly, but am not sure how to do it in java with spring data. I have a model class representing my Student documents that I use annotations to describe how to query.
db.users.find({"name": /.*m.*/})
I don't know how to translate that into java as I want to pass in a variable. For example:
Pseudocode:
String myvar = "R";
db.users.find({/.*<variable here>*/})
The following is what I have on my "MongoRepository" implementation:
public interface UserRepository extends MongoRepository<UserId, String> {
{
#Query("{'name' : {$regex : ?0}}")
public List<Users> findByName(String username);
}
When I pass in the full name "Robert", then it is able to find "Robert". However, if I put "R", it does not find anything.
Did you try it with query method?
public interface UserRepository extends MongoRepository<UserId, String> {
{
public List<Users> findByNameLike(String username);
}
Why dont you use regex like you're trying to in the following manner :
public interface UserRepository extends MongoRepository<UserId, String> {
{
#Query("{'name' : {$regex : ?0}}")
public List<Users> findByName(String regexp);
}
and in your service form a query somewhat like this :
#Mock
UserRepository userRepository;
String queryInput = "^R.*$";
List<Users> users = userRepository.findByName(queryInput);
find more detailed answers in here in section 4.2
I'm using Spring Data with MongoDB using MongoRepository.
I was wondering if it is possible do a delete by filter using query annotation. I have been looking here and google and I cannot find any documentation.
#Query(value="{'id' : $0}", delete = true)
public Person deleteById (String id);
Maybe you can use repository delete queries. Here is an example from documentation:
public interface PersonRepository extends MongoRepository<Person, String> {
List <Person> deleteByLastname(String lastname);
Long deletePersonByLastname(String lastname);
}
Using return type List will retrieve and return all matching documents before actually deleting them. A numeric return type directly removes the matching documents returning the total number of documents removed.
Try this, it's work for me.
#Repository
public interface DepartmentDao extends MongoRepository<Department, String> {
#DeleteQuery
void deleteByDepartment(String department);
}
OR
#Query(value="{'_id' : ?0}", delete = true)
public void deleteById(String id);
Unfortunately spring data doesn't provides any method to delete documents based on a query. And the #Query annotation is only for find documents.
What you could do is implement a custom repository that deletes documents based on what you want.
How to delete a list of ids in the query ?
#Query(value="{idList : $0}", delete = true)
Repository:
#Component
public interface SomeRepository extends MongoRepository<SomeObject, String> {
#Query("{ '_id' : ?0 }")
SomeObject findById(String _id);
}
Code in some class:
#Autowired
private SomeRepository pRepo;
public void delete(String id) {
pRepo.delete(pRepo.findById(id));
}
#Repository
public interface DepartmentDao extends MongoRepository<Department, String> {
void deleteByDepartment(String department);
}
is clean and shorter.