Comments (5)
@JuamBer That's right, the library does not support those queries.
One of possible ways to achieve described behavior is a fetching entities with "firstname" and "surname" fields and concatenating these fields in java code:
@Entity(name = "users")
public class User {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String firstname;
private String surname;
}
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping(params = {"firstname", "surname"})
public List<String> listCustomerNamesByFirstNameAndLastName(
@And({@Spec(path = "firstname", spec = Equal.class),
@Spec(path = "surname", spec = Equal.class)}) Specification<User> spec) {
return userRepository.findAll(spec).stream()
.map(user -> user.getFirstname() + " " + user.getSurname())
.collect(Collectors.toList());
}
}
You can also consider using a DTO pattern in your app and make this operation a part of transformation entity
-> DTO
:
@Entity(name = "users")
public class User {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;
private String firstname;
private String surname;
}
// Simplified example of DTO
public class UserDto {
public String fullName;
// other fields which you want to return via REST API
public UserDto(User user) {
this.fullName = user.getFirstname() + user.getSurname();
}
public String getFullName() {
return fullName;
}
}
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
UserRepository userRepository;
@GetMapping(params = {"firstname", "surname"})
public List<UserDto> listCustomerNamesByFirstNameAndLastName(
@And({@Spec(path = "firstname", spec = Equal.class),
@Spec(path = "surname", spec = Equal.class)}) Specification<User> spec) {
return userRepository.findAll(spec).stream()
.map(UserDto::new)
.collect(Collectors.toList());
}
}
from specification-arg-resolver.
Sorry, in my case the controller receives the full name and the result would be the normal entity.
@Spec(path = "fullName", params = "fullName", spec = LikeIgnoreCase.class)
In other words, in the specification I receive the full name but that field does not exist in the entity. I don't see a way to do it with this library, maybe I have to do a custom query. What do you think?
from specification-arg-resolver.
I assume that you have index with name full name
on two fields (firstName
, surname
), so when you create spec:
@And({@Spec(path = "firstname", spec = Equal.class),
@Spec(path = "surname", spec = Equal.class)}) Specification<User> spec
and perform search using this spec, the MySQL will automatically use index full_name(first_name, surname)
to optimize query execution, so you don't have to refer to the index name in any place in the code (custom query, specification definition etc).
In other words, in the specification I receive the full name but that field does not exist in the entity. I don't see a way to do it with this library, maybe I have to do a custom query.
If I misunderstood you, please provide a sample SQL of query which you want to achieve using this library. (E.g. SELECT X FROM Y WHERE ...
)
from specification-arg-resolver.
What I was looking for was to get the data from a table to which I passed the full name as a parameter. I leave here the query that I had to implement
SELECT * FROM users AS u WHERE LOWER(CONCAT(u.name, CONCAT(" ", u.surname))) LIKE LOWER(:fullName)
from specification-arg-resolver.
In order to achieve that, you would have to implement your own Specification class that takes 2 parameters, concatenates them, and executes like on CriteriaBuilder
. You can take a look on Like
specification and Between
(the latter one uses 2 parameters). It requires a bit of coding, but generally is very easy. I'm closing the issue, but feel free to report any further questions or challenges.
from specification-arg-resolver.
Related Issues (20)
- Required field HOT 2
- Specification arguments set HOT 1
- Java 17 incompatibility HOT 2
- Can you add SPeL in @Spec constVal with value from headers? HOT 2
- Issue with Join on Hierarchical Entities HOT 1
- [BUG] JoinFetch alias while pagination HOT 5
- Missing support for Spring Boot 3 and transition from javax to jakarta HOT 3
- Redundant proxy is created for multi-spec specifications when expected type is not a spec-interface HOT 3
- Path variables from `@RequestMapping` annotation above the class are not included in springdoc api documentation HOT 1
- Additional specifications HOT 1
- Support for auto-configuration
- Fail search with an error on unknown search parameters
- Add "description" attribute in the "@Spec" annotation HOT 2
- Json body params should not impose restrictions on requests HOT 3
- Interface extending Specification referenced from a method is not visible from class loader HOT 1
- Specification adds duplicate join when query has been built manually with existing join (Spring Boot 3.x)
- No params in Swagger UI generated by Springdoc-OpenAPI 1.6.13
- How to use with Dto and without entities in presentation layer (Controller)? HOT 2
- Query param with type Integer is not compatible when used with Blaze-Persistence
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from specification-arg-resolver.