坚持方法不起作用
Persist method is not working
我在使用 JPA 方法时遇到问题。我正在学习 CustomerDB Netbeans 教程 (https://netbeans.org/kb/docs/websvc/rest.html)。问题是我无法在数据库中存储任何数据。 Persist 方法已执行并且不会抛出任何异常,因此我认为 persist 正确完成。但是当我检查数据库时,已经插入了任何数据。
我的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 被正确执行并且 returns 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
有人能帮帮我吗?非常感谢任何评论。非常感谢。
主要错误是我的休息服务是 Apache Tomcat 上的 运行,并且不支持 EJB。我在 TomEE 上尝试过,但我在 persist 方法上遇到了同样的问题。默认情况下,TomEE 使用 OpenJPA 而不是 EclipseLink。最后,我尝试在 GlassFish 上部署我的应用程序,现在一切正常 运行。
旁注:您可以在 tomee 中导入 eclipselinks(甚至在您的 war 中),它会起作用。关于 openjpa,您似乎没有使用 jpa 2.1 功能,因此只需将命名空间和 jpa 版本更改为 2.0 即可。