2

I'm diving into Java EE, learning JPA and JAX-RS. I want to expose all OneToMany entities when a GET request is done on the "owned" side of the relation.

I created the entities Company and CompanyAddress, the company can have multiple addresses (OneToMany) and is mapped by the field companyId in the CompanyAddress entity.

I want to expose my company including all addresses when a GET is done on the company, other way around i only want the companyaddress to show when a GET is done on this field. How can I make this work?

in my Facade Class for Company:

@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Company find(@PathParam("id") Integer id) {
    return super.find(id);
}

In my entity class for Company:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "companyId",fetch=FetchType.EAGER)
private Collection<CompanyAddress> companyAddressCollection;

and

@XmlTransient
public Collection<CompanyAddress> getCompanyAddressCollection() {
    return companyAddressCollection;
}

And in my entity class for CompanyAddress

@JoinColumn(name = "companyId", referencedColumnName = "id")
@ManyToOne(optional = false)
private Company companyId;
2
  • What are your results when accessing the GET path?
    – Smutje
    Feb 13, 2014 at 11:42
  • 2
    Won't the XmlTransient discard the address from the XML, the exact opposite of what you want to achieve?
    – Gimby
    Feb 13, 2014 at 11:52

1 Answer 1

1

The @XmlTransient annotation is useful for resolving name collisions between a JavaBean property name and a field name or preventing the mapping of a field/property.

Remove the @XmlTransient annotation and the companyAddressCollection will be represented within the XML doc.

1
  • 1
    This worked for me, I moved the XMLTransient tag to the CompanyAddress entity! I am going to take a deeper look into all the annotations and their functionality.
    – RoyB
    Feb 16, 2014 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.