Is it possible with SolrJ to define a bean and provide its field mapping by a mixin class when using QueryResponse#getBeans(Class)?
Example: Let E be an entity with a name attribute. The schema defines *_s_de and *_s_en dynamic fields.
Depending on a supplied language identifier I want to map the name_s_de or name_s_en field value to the name attribute of E without changing its implementation to have #Field("name_s_de") hardcoded.
Thanks!
Related
I'm working on a project in which I need to pass parameters to the #subselect annotation of spring boot (which maps a request to an entity), like the following example:
#Entity
#Immutable
#Subselect("SELECT FROM Employe INNER JOIN Employe_adress ON Employe.id = Employe_adress.eid WHERE Employe_adress.aid=?x")
public class Employe {
...
}
I want to bind an external value to "x" variable. Thank you.
Edit: One method I find is adding a global variable, but "The value for annotation attribute must be a constant expression".
It can be done with #FilterDef and #Filter: https://www.baeldung.com/hibernate-dynamic-mapping#parameterized-filtering-with-filter
Defining the #Filter
To demonstrate how #Filter works, let's first add the following filter definition to the Employee entity:
#FilterDef(
name = "incomeLevelFilter",
parameters = #ParamDef(name = "incomeLimit", type = "int")
)
#Filter(
name = "incomeLevelFilter",
condition = "grossIncome > :incomeLimit"
)
public class Employee implements Serializable {
The #FilterDef annotation defines the filter name and a set of its parameters that will participate in the query. The type of the parameter is the name of one of the Hibernate types (Type, UserType or CompositeUserType), in our case, an int.
The #FilterDef annotation may be placed either on the type or on package level. Note that it does not specify the filter condition itself (although we could specify the defaultCondition parameter).
This means that we can define the filter (its name and set of parameters) in one place and then define the conditions for the filter in multiple other places differently.
This can be done with the #Filter annotation. In our case, we put it in the same class for simplicity. The syntax of the condition is a raw SQL with parameter names preceded by colons.
I have a dto used as request body in a rest service:
#Data
public class MyClass {
#JsonAlias("myAlias")
private String myProperty;
}
When we produce our openapi/swagger yaml the property name is "myProperty"
Is it possible to automatically force the alias inside the openapi/swagger produced?
(annotation? during yaml creation? anything else?)
Serialization always uses the primary name - in this case, myProperty. In order to change the outputted name, you'd need to change the name of myProperty. The JsonAlias annotation only affects deserialization - the alias is an alternate name.
I have an instance of a class that looks as following
public class SomeEntity{
private OpMetric metric = Options.MEASURED;
private Scope scope = Scopes.GLOBAL;
}
Which need to be serialized into following XML
<SomeEntity xmlns="">
<op-metric>
<value>0.3</value>
</op-metric>
<calculated-scope>
<value>updated-global</value>
</calculated-scope>
</SomeEntity >
in both cases the value to be set in the xml is calculated based on enum values of the original fields ,meaning I need to use getters (+ #JsonIgnore on the fields ) and not just annotate the fields.
I've tried to use the following annotation on the getters to generate the format
#JacksonXmlProperty(isAttribute = false, localName = "value")
#JacksonXmlElementWrapper(localName="op-metric")
but it can only be used on one of them due to collision when using the same local name :
com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "value":
Using Mixins did not advance me much since obviously the same limitation applies there as well.
How should I go about creating this XML structure ?
I've ended up creating special methods for the purpose of XML creation ,each of which returns an instance of a class whose only field is named "value", which is then "automatically" gets serialized into the format required .
Annotations were added in using Jackson mixin
I want to annotate a field in my jaxb generated class with this annotation - #XmlElement(required = false). Which attribute in the XSD would generate my field with this annotation?.
I can't hand type this as the JAXB classes are auto generated using Maven every time a build is run.
My jaxb version is xjc 2.2.4-2
Thanks
When an element has minOccurs="0" the corresponding #XmlElement has required=false. Note that false is the default value of the required attribute so it may not actually appear in the generated annotation.
UPDATE
Based on your comment:
Let me explain my actual problem. I'm using Jackson to generate the
JSON from the JAXB classes. Issue is when the element is not
present in the xml, I see the json output with the field name as 'pip'
and value as null. I am actually expecting the field 'pip' to be
absent from my json output as I declared it to be minOccurs=0 in the
XSD. Can't figure out if it's an issue with JAXB or Jackson.
Interestingly when I annotate the field explicitly with required=false
in the jaxb class, I see my expected output with the field being
absent
This is an issue with Jackson not handling the default value of the required property on the #XmlElement annotation correctly.
I have the following problem. I got a database schema generated by JPA/Hibernate in Java. I have one table for an inheritance hierarchy. For this to work Hibernate uses a DTYPE column to distinguish between the concrete implementations of the classes in my hierarchy.
I now need to load that same hierarchy to GRAILS domain objects. However GRAILS uses a column with the name 'class' to save the names of the concrete implementation and I was not able to find any way to change this mapping. So my question is: is there a way to map GRAILS' 'class' column to Hibernate's DTYPE column?
You can customise the name used for the discriminator column in the base class mapping closure.
class TopOfTheHierarchy {
…
static mapping = {
discriminator column: "DTYPE"
}
}
To use something other than the class name as the discriminator value you use a similar mapping entry for each subclass
class ChildClass {
…
static mapping = {
discriminator "child"
}
}
If you already have a DB schema you can try using Grails Database Reverse Engineering Plugin to generate your Grails domain class from your schema.