This discussion is archived
1 Reply Latest reply: Oct 3, 2012 6:17 AM by JamesSutherland RSS

Issues with EclipseLink JPA 2.0

965539 Newbie
Currently Being Moderated
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;

@Entity
@Table(name = "vendor")
@XmlRootElement
@NamedQueries({
@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;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Lob
@Column(name = "Name")
private String name;
@Lob
@Column(name = "Description")
private String description;
@Column(name = "TimeCreated")
@Temporal(TemporalType.TIMESTAMP)
private Date timeCreated;
@Column(name = "CreatedByUserID")
private BigInteger createdByUserID;
@Column(name = "LastChange")
@Temporal(TemporalType.TIMESTAMP)
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();
em.createNamedQuery(persistanceUnit);
}

public void startTransaction() {
em.getTransaction().begin();
}

public void rollBackTransaction() {
em.getTransaction().rollback();
}

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

public void persist(Object object) {
em.persist(object);
}

public void close() {
em.close();
emf.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";;);
transaction.close();
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
    JamesSutherland Pro
    Currently Being Moderated
    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points