我正在尝试使用jersey在数据库(休眠)中创建json发布请求中提供的新数据。因此,我创建了一个新的动态Web项目并将其更改为Maven。
在一个测试测试类中,我可以创建一个新用户并成功坚持。到目前为止,没有任何逻辑但只返回ok的post请求就可以了。但是,如果我发布部署并尝试保留数据,则会出现错误
javax.servlet.ServletException:javax.persistence.PersistenceException:名为shareiffUnit的EntityManager的持久性提供程序。
我觉得有些设置必须更改或重新放置。
UserAPI
@Path("/message")
public class UserAPI
{
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createUser(String inputStream) {
/*
* gson to map json to user class
*/
Gson gson = new Gson();
User newUser = new User();
newUser = gson.fromJson(inputStream, User.class);
/*
* trying to persist
*/
GenericDao dao = new GenericDao(User.class);
dao.create(newUser);
return Response.ok().build();
}
}
GenericDAO
public class GenericDao<E> implements IGenericDao<E> {
private Class<E> entityClass;
private final EntityManagerFactory enityManagerFactory = Persistence.createEntityManagerFactory("shareiffUnit");
private EntityManager entityManager = null;
private EntityManager getEntityManager() {
if(entityManager == null) {
return enityManagerFactory.createEntityManager();
}
return entityManager;
}
public GenericDao(Class<E> thisClass) {
this.entityClass = (Class<E>) thisClass;
}
public void setEntity(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void create(E entity) {
getEntityManager().persist(entity);
}
public E read(Integer id) {
return getEntityManager().find(this.entityClass, id);
}
public void update(E entity) {
getEntityManager().refresh(entity);
}
public void delete(E entity) {
getEntityManager().remove(entity);
}
}
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="shareiffUnit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/docsheriffdb" />
<property name="connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
我在这里错过任何配置吗?在war文件(sheriff-0.0.1.war \ WEB-INF \ classes \ META-INF)中,存在persistence.xml。
有任何提示吗?