By default, JAXB maps non-annotated Java field and properties to elements.
Is there a way to change this behaviour so JAXB maps non-annotated Java field and properties to attributes instead of elements?
There is no way with the current spec. Even if you annotate one of the fields with #XmlValue, you must explicitly set all other fields with #XmlAttribute otherwise marshalling will fail.
Related
I have read questions like these #JsonInclude to ignore null values.
This works for me for regular fields within an entity but not for Collections. In case of empty Collections within an entity, Json serialisation gives a null value.
How does one do a equivalent ignore for collections ?
Try with the annotation
#JsonInclude(Include.NON_EMPTY)
private Collection field;
Since the Jackson 2.x it provides #JsonInclude annotation that controls serialization of a class as a whole or its individual fields based on their values during serialization. It recognizes following annotations as:
Include.NON_NULL Indicates that only non-null properties should be serialized.
Include.NON_EMPTY Indicates that only non-null and non-empty properties should be serialized. This is actually the superset of Include.NON_NULL
Hence over a collection Include.NON_EMPTY will work like
#JsonInclude(Include.NON_EMPTY)
private Collection field;
or you can put it over the class to impact upon the whole model like
#JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Foo {
}
In Hibernate 4.x, is there any way to get a list of persistent fields at runtime? By persistent fields I don't mean DB column names, but POJO field names or property names (depending on access type for a particular entity). Also, is there a method to get/set a persistent field's value given a field name as a string, regardless of whether it's a field or property name?
It seems that everything I need (and more) is contained in ClassMetadata class (documentation).
I can get the metadata for a particular class by using SessionFactory.getClassMetadata(entityClass) and then use metadata.getPropertyNames(), etc.
I have to map a complex structure of Java classes which don't expose their fields through set-/get-methods in general (this is given and can't be changed).
So mapping can only be performed on a direct field access. Dozer allows individual fields to be made accessible but I haven't found a setting to make this the general behaviour. As a result I wouldn't have to map each field explicitly just for making it accessible!
Does this option exist?
- On a class level?
- On a global level?
You can set it on a class level.
<mapping>
<class-a is-accessible="true">MyClass</class-a>
...
</mapping>
From the Dozer XSD:
is-accessible Indicates whether Dozer bypasses getter/setter methods and accesses the field directly. This will typically be set to "false". The default value is "false". If set to "true", the
getter/setter methods will NOT be invoked. You would want to set this to "true" if the field is lacking a getter or setter method.
I'm working on to convert Hibernate objects to XML using JAXB. In our classes, we've around 50 fields from which I would need only 10 of them.
Basically, I'd defined XmlType#propOrder with 2 properties. JAXB complained that some public getters are not part of proporder.
I see that if I don't mark a property with either of XmlTransient/XmlElement, JAXB complains about it. Is there any way to skip writing 'XmlTransient' on every field?
You can use #XmlAccessorType(XmlAccessType.NONE) so that only the annotated fields/properties are marshalled to XML. JAXB does not require any annotations. Annotations are only required to override the default behaviour. What exceptions are you seeing?
For More Information
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
I am using hibernate annotations. How to add methods to POJO object? For example i have "getChildNodes" method, associated with database, but i want also add recursive method "getAllChildNodes". I get "org.hibernate.PropertyNotFoundException: Could not find a setter for property progress in class" exception when i do it.
If I interpret this as "how do I add a method that is NOT related to persistence" then you need to use the #Transient annotation on the getAllChildNodes() method
There are two ways of defining the structure of your entity.
using annotations on the instance variables of your entity or
using annotations on the getter methods of your entity
When using the annotations on getter methods, Hibernate assumes that every getXxx (and isXxx for boolean types) represents definition of a persistent property. And this holds even if that particular getter does not contain any annotations, as happens in your case.
Hibernate also expects to find a matching setter method for each persistent property. And in your case that is what's missing and causes the exception.
You can solve this problem by declaring your custom getter as #Transient that says this getter does not represent a persistent property. Another way would be to convert the entity to use annotations on the instance variables. The latter would be my personal choice.
Open up the .java file and write a method named getAllChildNodes().
Hibernate doesn't write code for you, it maps fields in your database to your code. That's all. If you want to have extra logic in your domain/model classes besides the normal getters and setters for your properties, you'll have to add them yourself.