无法使用 PostgreSql 获取数据源。无法使用 JPA EntityManager
Cannot acquire data source whit PostgreSql. Can't use JPA EntityManager
我有下一个问题:
我正在使用 JPA 和 Restfull whit netbeasn 和 postgres 做一个项目,我有以下坚持
<?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="PruebaPersitenciaPU" transaction-type="JTA">
<jta-data-source>data_cotratacion</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
还有以下使用 EntityManager 的代码:
@GET
public String crearPersona(
@QueryParam("id") String id,
@QueryParam("name") String name,
@QueryParam("gender") String gender,
@QueryParam("date") String date){
Persona p = new Persona();
p.setId(id);
p.setName(name);
p.setGender(gender);
p.setDate(stringToDate(date));
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PruebaPersitenciaPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(p);
em.getTransaction().commit();
return "Usuario Creado "+id;
} catch (Exception e) {
return "Usuario no creado error: "+e;
}
}
但是试试这个会给我发送这个错误:
Usuario no creado error: javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException Exception Description: Cannot acquire data source [data_cotratacion]. Internal Exception: javax.naming.NamingException: Lookup failed for 'data_cotratacion' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: data_cotratacion not found]
听起来好像没有 JDBC 配置名称为 data_cotratacion 的数据源。您需要在您的应用程序服务器上配置此 JDBC 资源。
或者您需要使用 netbeans 中的向导创建您的持久性单元进入菜单文件 > 新建文件然后 select 左侧框中的持久性和右侧框中的持久性单元。
我有下一个问题:
我正在使用 JPA 和 Restfull whit netbeasn 和 postgres 做一个项目,我有以下坚持
<?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="PruebaPersitenciaPU" transaction-type="JTA">
<jta-data-source>data_cotratacion</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
还有以下使用 EntityManager 的代码:
@GET
public String crearPersona(
@QueryParam("id") String id,
@QueryParam("name") String name,
@QueryParam("gender") String gender,
@QueryParam("date") String date){
Persona p = new Persona();
p.setId(id);
p.setName(name);
p.setGender(gender);
p.setDate(stringToDate(date));
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PruebaPersitenciaPU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(p);
em.getTransaction().commit();
return "Usuario Creado "+id;
} catch (Exception e) {
return "Usuario no creado error: "+e;
}
}
但是试试这个会给我发送这个错误:
Usuario no creado error: javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException Exception Description: Cannot acquire data source [data_cotratacion]. Internal Exception: javax.naming.NamingException: Lookup failed for 'data_cotratacion' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: data_cotratacion not found]
听起来好像没有 JDBC 配置名称为 data_cotratacion 的数据源。您需要在您的应用程序服务器上配置此 JDBC 资源。
或者您需要使用 netbeans 中的向导创建您的持久性单元进入菜单文件 > 新建文件然后 select 左侧框中的持久性和右侧框中的持久性单元。