For example, I have helper class with Specifications. Here is the example of one:
public static Specification<Player> filterByName(String name) {
return ((root, query, criteriaBuilder) ->
name == null ? null : criteriaBuilder.like(root.get("name"), "%" + name + "%"));
}
I use them later to call Repository. I don't understand how objects of root, query, criteriaBuiler "appear" in he method above if the are not being created by me.
I do appreciate your answer a lot!
This happens because from your declaration your method has to return an object implementing the Specification interface that contains the abstract toPredicate method having three parameters. What Java compiler does is the type inference of the returned value and of the three arguments from the lambda expression assigning type to them according to the toPredicate method's declaration, ending this process with success.
Related
I saw this in a code, but I can't figure it out, and I didn't find it in documentation (Most probably I don't know how to search)
so it's like that in a model:
public function functionName(int $parameter): ClassName{...}
What does the Class after the parameter list stands for?
public function functionName(int $parameter): ClassName{...}
In this case ClassName is written to specify the return type of function.
functionName returns object instance of ClassName
you can read more about return types here: https://wiki.php.net/rfc/return_types
this is the return type declarations. PHP7 introduces this syntex.
It indicates the types of value that the function will return.
you can use int float and it's not limited to array you can use your own class
public function functionName(int $parameter): ClassName{...}
it's not just readability, if the function returns anything elase then it will throw an error
I need a custom query (due to entity subclassing) for my deleteAll repository method such as (Kotlin code):
#Query("#{#n1ql.delete} WHERE (_class='a.b.C' OR _class='d.e.F')")
override fun deleteAll()
However, when running this I get:
org.springframework.data.couchbase.core.CouchbaseQueryExecutionException: Query returning a primitive type are expected to return exactly 1 result, got 0
I cannot find an example how to provide a custom query for a deleteAll method, which (by interface signature) cannot return anything.
I'm working in a Spring Boot environnement using Kotlin. I made a controller with a method annotated with #GetMapping. This method have some parameters of type #RequestParam declared as Double type. If I try to call my method without providing these parameters, my code raises the following exception:
java.lang.IllegalStateException: Optional double parameter 'latitude' is present but cannot be translated into a null value due to being declared as a primitive type.
I assume that the parameters have default value (probably 0.0), but Kotlin need an object which can be null, so the exception is raised.
All works fine if I provide the parameters, but I want my code working if no parameters are provided.
How can I avoid this exception?
Here's how my controller looks like:
#RestController
#RequestMapping("/api/stations")
class StationController {
#GetMapping
fun findAll(#RequestParam(value = "latitude", required = false) currentLatitude: Double,
#RequestParam(value = "longitude", required = false) currentLongitude: Double): ResponseEntity<List<Entity>> {
//Method body
}
Maybe the following part of the documentation regarding basic types will help you:
On the Java platform, numbers are physically stored as JVM primitive types, unless we need a nullable number reference (e.g. Int?) or generics are involved. In the latter cases numbers are boxed.
Your guess might be correct then. Try using Double? and it should be ok.
We are using graphql-java and with com.coxautodev.graphql.tools.SchemaParser (from graphql-java-tools) we are creating executable schema - and it works just fine. Now we need to add user information and propagate it to our graphql method logic. The obvious approach is to use "context" object.
So, in mutations.graphql file there is:
type Mutations
createGroup(input: CreateGroupInput): IdRequest
...
On the other hand, there is a Java class with the corresponding method:
IdRequest createGroup(CreateGroupInput input) {
...
}
Then, when calling graphql.GraphQL.execute(myQuery, contextObject) how to read this contextObject into Java method above?
The thing I was looking for is DataFetchingEnvironment (see Fetching data).
The "contextObject" can then be retrieved in Java class by adding DataFetchingEnvironment as a last argument, like:
IdRequest createGroup(CreateGroupInput input, DataFetchingEnvironment environment) {
Object contextObject = environment.getContext();
...
}
I want to mock the below mentioned method.
public class MockClass {
public boolean ToBeMocked(Cinput, Coutput, List<CIOChain<Cinput, Coutput>>)
}
What should be in place of ?? in below mentioned code ?
Easymock.expect(MockClassObject.ToBeMocked(Cinput.class, Coutput.class, ??)).andReturn(true);
At the Class level, all List interfaces are the same regardless of the generic type, due to type erasure; they are only different at compile time.
So it's just List.class in place of ??.
That is,
Easymock.expect(MockClassObject.ToBeMocked(Cinput.class, Coutput.class, List.class)).
andReturn(true);
In the scope of mocking, you should really be specifying the objects that you expect to be passed to that method, like:
Easymock.expect(MockClassObject.ToBeMocked(cInputObj, cOutputObj, listObj)).
andReturn(true);
If for some reason you can't do that, you can use isA/anyObject variants:
Easymock.expect(MockClassObject.ToBeMocked(isA(Cinput.class), isA(Coutput.class), isA(List.class))).
andReturn(true);