SpecificationBuilder | Object-Relational Mapping library
kandi X-RAY | SpecificationBuilder Summary
kandi X-RAY | SpecificationBuilder Summary
SpecificationBuilder
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Main entry point
- Builds a specification from a field
- Get all fields for a given class
- Add all superclass fields for the given superclass
- Field parse method
- Handler for like %
- Handle collection
- Append predicate
- Gets actual type arguments
- Checks if is primitive
- Gets raw class
- Creates a new instance of the specified class
- Sets the value of a field
- Gets the field from an object
- Invoke a method
- Checks field access
SpecificationBuilder Key Features
SpecificationBuilder Examples and Code Snippets
Community Discussions
Trending Discussions on SpecificationBuilder
QUESTION
Lets say that i have an abstract class Product and i have two more entities that extend it ProductA and ProductB with different fields. I am using Product repository to search for all of them using Specification and SpecificationBuilder using some common fields. What i want to accomplish is to detect what type of product it is (A or B) and based on that to use different search query.
For example ProductA has stock and ProductB has featured so naturally if i insert search criteria quantity and try to do a specification on Product repo, i would like the api to search for ProductA. Is this form of search possible in any way? This is my current code:
...ANSWER
Answered 2018-Dec-24 at 14:42As far as I know there is no "nice way" of doing this (if you want to preserve the type), but you could employ one of the following solutions:
0. Switch case generally I am really against switch
and case
in Java
but it has its own place... The easiest solution is to build your query in separate cases as well... they are separate cases (pun not intended).
From your question I assume that you have a way more complicated structure than the presented one, so next I have two possible solutions for you.
1. Programmatic solution You could create a solution based on reflection. You can have a full solution implementing some interesting logic. Mainly you have to have a list from the objects that are "searchable" in this feature. In a loop you can use reflection to determine if the field is "acceptable" in the entity. (For example ˙stock˙ is only acceptable in PRODUCT_A
so the creation of the criteria will be based on that Entity
. I definitely do not recommend this approach as this could introduce a lot of problems. (For example how would you handle if stock
and featured
is set simultaneously in your DTO?
2. create a view to construct your search basically you create a database view and an entity in Spring having all the possible parameters.
Original tables:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SpecificationBuilder
You can use SpecificationBuilder like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the SpecificationBuilder component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page