Giter Site home page Giter Site logo

Comments (5)

jradlica avatar jradlica commented on August 16, 2024 1

@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.

JuamBer avatar JuamBer commented on August 16, 2024

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.

jradlica avatar jradlica commented on August 16, 2024

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.

JuamBer avatar JuamBer commented on August 16, 2024

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.

tkaczmarzyk avatar tkaczmarzyk commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.