Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it work to put Java XML and persistance annotations on the same bean?

I haven't worked an implementation yet but I'm wondering if it's okay to put XML annotations as well as persistance annotations onto the same bean.

The reason I'm asking is because I want to read in some XML using Spring OXM, have the XML written to domain objects which are also the domain objects that are mapped to the database (that mapping was already done).

like image 533
Crowie Avatar asked Feb 17 '23 07:02

Crowie


1 Answers

Annotations are just meta data. On their own, they don't do anything to your code. You need to use reflection to make use of them. So, yes, you could put any number of annotations on your classes and fields.

Your persistence framework will read the persistence annotations while your XML parser will read the XML annotations.

Eg.

@Entity // JPA
@XmlRootElement(name = "book") // JAXB
@SuppressWarnings(value = "random") // whatever other annotation
public class User  {

    @Id
    @GeneratedValue
    @GenericGenerator(name = "incremental", strategy = "increment")
    @XmlElement
    private Long userID;

    // more
}
like image 176
Sotirios Delimanolis Avatar answered Mar 26 '23 22:03

Sotirios Delimanolis