1 Reply Latest reply on Oct 3, 2012 1:17 PM by james_sutherland

    Issues with EclipseLink JPA 2.0

      This is probably very basic, but I will expose my ignorance about JPA nevertheless.

      I need to isolate JPA from the rest of the application. So I broke down the application into 3 different components. One has the JPA entities and depdencies, one has a service facade and the last one has a client (just a test client for now)

      Here is an example of an entity of the DAO JAR.

      package DataObjects;

      import java.io.Serializable;
      import java.math.BigInteger;
      import java.util.Date;
      import javax.persistence.Basic;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.Lob;
      import javax.persistence.NamedQueries;
      import javax.persistence.NamedQuery;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.xml.bind.annotation.XmlRootElement;

      @Table(name = "vendor")
      @NamedQuery(name = "Vendor.findAll";;, query = "SELECT v FROM Vendor v"),
      @NamedQuery(name = "Vendor.findById";;, query = "SELECT v FROM Vendor v WHERE v.id = :id"),
      @NamedQuery(name = "Vendor.findByTimeCreated";;, query = "SELECT v FROM Vendor v WHERE v.timeCreated = :timeCreated"),
      @NamedQuery(name = "Vendor.findByCreatedByUserID";;, query = "SELECT v FROM Vendor v WHERE v.createdByUserID = :createdByUserID"),
      @NamedQuery(name = "Vendor.findByLastChange";;, query = "SELECT v FROM Vendor v WHERE v.lastChange = :lastChange"),
      @NamedQuery(name = "Vendor.findByChangeByUserID";;, query = "SELECT v FROM Vendor v WHERE v.changeByUserID = :changeByUserID"),
      @NamedQuery(name = "Vendor.findByParentVendor";;, query = "SELECT v FROM Vendor v WHERE v.parentVendor = :parentVendor")})
      public class Vendor implements Serializable {
      private static final long serialVersionUID = 1L;
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "ID")
      private Integer id;
      @Column(name = "Name")
      private String name;
      @Column(name = "Description")
      private String description;
      @Column(name = "TimeCreated")
      private Date timeCreated;
      @Column(name = "CreatedByUserID")
      private BigInteger createdByUserID;
      @Column(name = "LastChange")
      private Date lastChange;
      @Column(name = "ChangeByUserID")
      private Integer changeByUserID;
      @Column(name = "ParentVendor")
      private Integer parentVendor;

      public Vendor() {

      public Vendor(Integer id) {
      this.id = id;

      public Integer getId() {
      return id;

      public void setId(Integer id) {
      this.id = id;

      public String getName() {
      return name;

      public void setName(String name) {
      this.name = name;

      public String getDescription() {
      return description;

      public void setDescription(String description) {
      this.description = description;


      Needles to say, my unit test for this project work just fine.

      My facade facade (yet another JAR) looks something like this:

      public class TransactionHelper {

      private static EntityManagerFactory emf;
      private static EntityManager em;

      public TransactionHelper(String persistanceUnit) {
      emf = Persistence.createEntityManagerFactory(persistanceUnit);
      em = emf.createEntityManager();

      public void startTransaction() {

      public void rollBackTransaction() {

      public List<Vendor> executeNameVendorQuery(String Query) {
      TypedQuery<Vendor> query = em.createNamedQuery("Vendor.findAll";;,Vendor.class);
      return query.getResultList();
      public void commitTransaction() {

      public void persist(Object object) {

      public void close() {

      And finally my client looks something like this:

      public class Vendors {
      static public List<Vendor> GetAllVendors() {
      TransactionHelper transaction = new TransactionHelper("DataObjectsPU");
      List<Vendor> vendors = transaction.executeNameVendorQuery("Vendor.findAll";;);
      return vendors;

      However, as I run my test client, the following error happens:

      NamedQuery of name:DataObjectsPU not found.

      The PU is defined on the DAO jar file and I would expect JPA to look for its definition there. Obviously this is not happening. How can I tell JPA where to find the PU definition?

      In the end of the day, I am trying to isolate JPA from the rest of the application components. But if I have to add dependencies on all tiers of the application, it seems to me that I have not chosen to right architectural pattern.

      All thoughts are welcome....
        • 1. Re: Issues with EclipseLink JPA 2.0
          You need a persistence.xml file on the classpath in a meta-inf directory with a persistence unit name "DataObjectsPU".
          Perhaps include your persistence.xml file. Is the jar on the same classpath as your client?

          In general in may be simpler just to use the standard JPA API from the client, instead of wrapping it in your own proprietary API.