Is there anyway to annotate only required fields for JAXB - java

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

Related

How to map non-annotated fields to attributes by default in Jaxb

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.

Combine annotations in Java

How can I combine annotations in Java?
EDIT I was asking if I two annotations a and b, can I combine to a single annotations c?
If possible, how do I do that?
You cannot combine the annotations by e.g. annotating the annotations, unless the annotation consumer will process the meta-annotation tree explicitly. For example, Spring supports such feature for #Transactional, #Component and some other annotations (you may wish to have a look at SpringTransactionAnnotationParser#parseTransactionAnnotation()). Nice to have this feature in Java core, but alas...
However you can declare the common parent class that has a set of annotations you need and extend it. But this is not always applicable.
Assuming you want to have multiple annotations on a single element, you can just list them in sequence.
The Wikipedia page on Java annotations has quite a few useful examples, e.g.
#Entity // Declares this an entity bean
#Table(name = "people") // Maps the bean to SQL table "people"
class Person implements Serializable {
...
}

Java, Hibernate annotations, How to add methods to POJO object?

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.

Can you specialize inherited Java annotations

I have an abstract superclass that has JPA annotations on it mapping some of its fields. The class itself has the #MappedSuperclass annotation.
Can I specialize/add or change just one element of an inherited annotation without re-specifying the entire annotation?
The only thing you can do with Annotations is to place an Annotation on an Annotation and have a library which understands this as a form of inheritance.
The problem is that if you re-specify the entire annotation, any library which expects MappedSuperclass will ignore it.

Inheritance with JAXB

I have an XSD file which is used to generate some objects which are then shared with other systems. I'd like to customize some of the business logic a bit in there by making some more specific implementation of these. I'm not adding new fields which need to be serialized, but more along the lines of adding setMethods which may take different format parameters. These methods do the work of translating the data into a form which is needed by the underlying object.
I may have a field on the JAXB object which is a string, but my system gives me an integer. So, I want to handle the work of converting that in a class which extends my base class.
So, is there anything special you need to do in order to get JAXB to look for XmlRootElement on a subclass of the object you are asking it to serialize? Or must I include a #XmlRootElement attribute on my more specific class?
thanks!
Yes, #XmlRootElement/ #XmlType should be enough. But why don't you just add these methods to the generated classes? (in case you are not regenerating them on each build, which is wrong imo)
However I'd advice externalizing the conversion to other (converter) classes / methods.

Categories

Resources