2

I have a problem when trying to return an object defined in an external .jar from a Java EE 6 Web service.

The Web service looks like the following:

@Stateless
@Path("book")
@Produces({"application/json", "application/xml"})
@Consumes({"application/json", "application/xml"})
public class NewWebService {

    @PersistenceContext(unitName = "EnterpriseApplication3-warPU")
    private EntityManager em;

    @GET
    public List<Foo> getBookTitle() {
        Query query = em.createNamedQuery("Foo.findAll");
        List<Foo> foo = query.getResultList();
        return foo;
    }
}

When I define the "Foo" class in the same .jar File as the Web service everything works fine. However, I'd like to define "Foo" in an .jar of it's own, as "Foo" is also a JPA bean, and as different components of the application (packaged as .ear) need to be able to access "Foo".

When defining "Foo" in another .jar file (that is packages in the same .ear) Glassfish returns the following error message:

javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: class org.example.entity.Foo nor any of its super class is known to this context.

Any hints on how I can workaround this error? "Foo" is a standard JPA bean with the "@XmlRootElement" annotation.

bamoida2010
  • 361
  • 1
  • 3
  • 4
  • possible duplicate of [Sharing a persistence unit across components in a .ear file](http://stackoverflow.com/questions/4073635/sharing-a-persistence-unit-across-components-in-a-ear-file) – Pascal Thivent Nov 02 '10 at 19:09

1 Answers1

0

If you have external jar with @Entity objects then add <jar-file>relative/path/to/your/external.jar</jar-file> to your persistence.xml.

It's not required for @MappedSuperclass objects in external jars.

More information is in this answer: Sharing a persistence unit across components in a .ear file

ALso take a look at external jar usage with @MappedSuperclass and @PersistenceContext injection on Github.

Community
  • 1
  • 1
Viacheslav Dobromyslov
  • 3,168
  • 1
  • 33
  • 45