JPA and JAXB Annotated POJOs [message #491926] |
Fri, 16 October 2009 14:59 |
Marc Schlegel Messages: 69 Registered: July 2009 |
Member |
|
|
Hello Everyone
I am still working on my little rcp application which uses a JPA annotated model. Now I want to have a functionallity that allows me to export some objects and import them again. So I added some JAXB annotations as well.
My problem now is, that I can only have one of the mentioned annotations working properly. When I combine both, I get into erros. I couldnt find much information about this issue, even though I expected this to be a very common usecase (a model that is persisted to a db, and exported imported via jaxb, transfered via webservice etc.).
It seems the biggest problem is my abstract superclass which only contains the id field. Currently my setup looks like the following:
@MappedSuperclass
@XmlType
public abstract class EntityBase implements Serializable{
@Transient
public static final long serialVersionUID = -3178098860021538369L;
@Id
@GeneratedValue
@Column(name = "Id")
@XmlElement
private long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
@Entity
@XmlType
public class Person extends EntityBase {
private static final long serialVersionUID = EntityBase.serialVersionUID;
@Column (name="FirstName", nullable=false)
private String firstName;
@Column (name="LastName", nullable=false)
private String lastName;
@OneToOne
@PrimaryKeyJoinColumn
private Address address;
...
@Entity @Table(name = "Customers")
@DiscriminatorValue("C")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType
public class Customer extends Person {
private static final long serialVersionUID = EntityBase.serialVersionUID;
I probably have some more errors in there now, after I tried different types of annotations, but the main error is, that jaxb complains about an XmlId which occurs multiple times?
Does anyone have an idea how to solve this? I am also interested in a best-practise approach.
Thanks a lot
|
|
|
Re: JPA and JAXB Annotated POJOs [message #491975 is a reply to message #491926] |
Fri, 16 October 2009 20:43 |
|
Hello Marc,
The error you are seeing wrt XmlID is most likely related to your id property not being of type String. One solution would be add a new String property to your model, or you could use EclipseLink 1.2 JAXB mechanisms with your existing model.
To use EclipseLink JAXB you require a jaxb.properties file in with your classes with the following entry:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Then instead of using XmlID/XmlIDREF you can use the EclipseLink equivalents through a customizer class:
import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping;
public class EmployeeCustomizer implements DescriptorCustomizer {
public void customize(ClassDescriptor descriptor) throws Exception {
// Equivalent to @XmlID
descriptor.addPrimaryKeyFieldName("@id");
// Equivalent to @XmlIDREF
XMLObjectReferenceMapping managerMapping = new XMLObjectReferenceMapping();
managerMapping.setAttributeName("manager");
managerMapping.addSourceToTargetKeyFieldAssociation("manager/text()", "@id");
managerMapping.setReferenceClass(Employee.class);
descriptor.addMapping(managerMapping);
}
}
The customizer class can be associated with your model using the @XmlCustomizer annotation:
@XmlCustomizer(EmployeeCustomizer.class)
public class Employee {
//@XmlID
@XmlAttribute
private long id;
//@XmlIDREF
@XmlTransient
private Employee manager;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Employee getManager() {
return manager;
}
public void setManager(Employee manager) {
this.manager = manager;
}
}
I have entered an enhancement request (https://bugs.eclipse.org/292581) to make this scenario easier in EclipseLink 2.0.
-Blaise
|
|
|
|
Re: JPA and JAXB Annotated POJOs [message #493057 is a reply to message #491926] |
Thu, 22 October 2009 21:05 |
|
Hi Marc,
We have made the fix in the EclipseLink 2.0 stream, using EclipseLink JAXB 2.1 the @XmlID annotation can now be used on a non-String field (this restriction still exists in the JAXB RI of course).
On the EclipseLink OXM side we're working on some improvements to make mapping JPA entity models to XML using JAXB easier (including things like mapping backpointers, and composite key support). I will be updating the following example as these become available:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
-Blaise
|
|
|
|
Powered by
FUDForum. Page generated in 0.04088 seconds