我遇到了JPA方法的问题。我正在关注CustomerDB Netbeans教程(https://netbeans.org/kb/docs/websvc/rest.html)。问题是我无法在数据库中存储任何数据。持久化方法是执行并且不会抛出任何异常,因此我认为持久化正在完成。但是当我检查数据库时,已插入任何数据。
我的persistence.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="CustomerDBPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entities.Customer</class>
<class>entities.MicroMarket</class>
<class>entities.DiscountCode</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://192.168.29.130:3306/sample"/>
<property name="javax.persistence.jdbc.user" value="cosio"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="316723"/>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
该权利是MicroMarket.java:
package entities;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@Entity
@Table(name = "micro_market")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "MicroMarket.findAll", query = "SELECT m FROM MicroMarket m"),
@NamedQuery(name = "MicroMarket.findByZipCode", query = "SELECT m FROM MicroMarket m WHERE m.zipCode = :zipCode"),
@NamedQuery(name = "MicroMarket.findByRadius", query = "SELECT m FROM MicroMarket m WHERE m.radius = :radius"),
@NamedQuery(name = "MicroMarket.findByAreaLength", query = "SELECT m FROM MicroMarket m WHERE m.areaLength = :areaLength"),
@NamedQuery(name = "MicroMarket.findByAreaWidth", query = "SELECT m FROM MicroMarket m WHERE m.areaWidth = :areaWidth")})
public class MicroMarket implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "zip_code")
private String zipCode;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "radius")
private Double radius;
@Column(name = "area_length")
private Double areaLength;
@Column(name = "area_width")
private Double areaWidth;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "zip")
private Collection<Customer> customerCollection;
public MicroMarket() {
}
public MicroMarket(String zipCode) {
this.zipCode = zipCode;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public Double getRadius() {
return radius;
}
public void setRadius(Double radius) {
this.radius = radius;
}
public Double getAreaLength() {
return areaLength;
}
public void setAreaLength(Double areaLength) {
this.areaLength = areaLength;
}
public Double getAreaWidth() {
return areaWidth;
}
public void setAreaWidth(Double areaWidth) {
this.areaWidth = areaWidth;
}
@XmlTransient
public Collection<Customer> getCustomerCollection() {
return customerCollection;
}
public void setCustomerCollection(Collection<Customer> customerCollection) {
this.customerCollection = customerCollection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (zipCode != null ? zipCode.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof MicroMarket)) {
return false;
}
MicroMarket other = (MicroMarket) object;
if ((this.zipCode == null && other.zipCode != null) || (this.zipCode != null && !this.zipCode.equals(other.zipCode))) {
return false;
}
return true;
}
@Override
public String toString() {
return "entities.MicroMarket[ zipCode=" + zipCode + " ]";
}
}
我的MicroMarketFacadeRest是:
package entities.service;
import entities.MicroMarket;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@javax.ejb.Stateless
@Path("entities.micromarket")
public class MicroMarketFacadeREST extends AbstractFacade<MicroMarket> {
@PersistenceContext(unitName = "CustomerDBPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomerDBPU");
EntityManager em = emf.createEntityManager();
return em;
//return em;
}
public MicroMarketFacadeREST() {
super(MicroMarket.class);
}
@POST
@Path("persisting")
public Response persisting() {
MicroMarket testZip = new MicroMarket("10097");
testZip.setRadius(1987.854);
testZip.setAreaLength(975.875);
testZip.setAreaWidth(865.681);
EntityTransaction tx = getEntityManager().getTransaction();
try {
tx.begin();
getEntityManager().persist(testZip);
tx.commit();
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
getEntityManager().close();
}
return Response.status(200).entity("Persist is finishing").build();
}
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public MicroMarket find(@PathParam("id") String id) {
return super.find(id);
}
@GET
@Override
@Produces({"application/xml", "application/json"})
public List<MicroMarket> findAll() {
return super.findAll();
}
}
我的AbstractFacade.java:
package entities.service;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
//getEntityManager().remove(entity);
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
System.out.println("Id of Customer requesteddddddddd" + id);
System.out.println(id);
System.out.println(entityClass);
System.out.println("El puto objeto no esta vacio!!!");
//return getEntityManager().find(entityClass, (Integer)id);
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
//EntityManagerFactory emf = Persistence.createEntityManagerFactory("CustomerDBPU");
//EntityManager em = emf.createEntityManager();
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
所以问题是方法persisting()在没有异常的情况下正确执行,但是在数据库中没有插入数据。但是find和findAll正确执行并从数据库中正确返回MicroMarket,并且在日志中我可以查看:
[EL Fine]: sql: 2015-01-02 19:12:14.017--ServerSession(206951341)--Connection(496870266)--Thread(Thread[http-nio-8084-exec-81,5,main])--SELECT zip_code, area_length, area_width, radius FROM micro_market
有人能帮帮我吗?任何评论都非常感谢。非常感谢。
答案 0 :(得分:0)
主要错误是我的休息服务在Apache Tomcat上运行,而且它不支持EJB。我和TomEE一起尝试但是我对persist方法有同样的问题。默认情况下,TomEE具有OpenJPA而不是EclipseLink。最后,我尝试在GlassFish上部署我的应用程序,现在一切都正常运行。
答案 1 :(得分:0)
旁注:您可以在tomee中导入eclipselink(即使在你的战争中)也可以。关于openjpa你似乎没有使用jpa 2.1功能,所以只需将命名空间和jpa版本更改为2.0即可。