Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA and JAXB Annotated POJOs
JPA and JAXB Annotated POJOs [message #491926] Fri, 16 October 2009 14:59 Go to next message
Marc Schlegel is currently offline Marc SchlegelFriend
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 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

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 #492042 is a reply to message #491975] Sat, 17 October 2009 14:03 Go to previous messageGo to next message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 69
Registered: July 2009
Member
Hi Blaise

Wow...thanks for that detailed answer.

Regarding the id being not a String, yes I got that error and I think this is kind of a design flaw. Your suggestion in the bugreport makes sense, lets hope there will be an improvement in 2.0.

For now, I think I will just make my IDs a String since it is just a little test-project and 2.0 should be released before the end of the year. I am actually waiting for JPA 2.0 and try not to waist to much time with 1.0 Smile

Thanks again
-- Marc
Re: JPA and JAXB Annotated POJOs [message #493057 is a reply to message #491926] Thu, 22 October 2009 21:05 Go to previous messageGo to next message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

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
Re: JPA and JAXB Annotated POJOs [message #493068 is a reply to message #491926] Thu, 22 October 2009 23:29 Go to previous message
Marc Schlegel is currently offline Marc SchlegelFriend
Messages: 69
Registered: July 2009
Member
This is great news. Thanks a lot, also for putting this to a wiki right away (what is the nicest feature if you dont know how to use it).

Cant wait for 2.0 Very Happy
Previous Topic:JPQL queries with IN operator and sub queries may be converted to incorrect SQL?
Next Topic: java.sql.SQLException: ORA-00900 calling StoredProcedure
Goto Forum:
  


Current Time: Fri Mar 29 07:30:02 GMT 2024

Powered by FUDForum. Page generated in 0.04088 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top