无法获取数据源whitgreSql。无法使用JPA EntityManager

时间:2015-03-23 05:18:51

标签: java postgresql 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没有creado错误:javax.persistence.PersistenceException:Exception [EclipseLink-7060](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.ValidationException异常描述:无法获取数据源[data_cotratacion]。内部异常:javax.naming.NamingException:SerialContext中的'data_cotratacion'查找失败[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} [根异常是javax.naming.NameNotFoundException:找不到data_cotratacion]

1 个答案:

答案 0 :(得分:0)

听起来没有使用名称data_cotratacion配置的JDBC数据源。您需要在应用程序服务器上配置此JDBC资源。

或者您需要使用netbeans中的向导创建持久性单元,然后在菜单中转到文件&gt;新文件然后在左框中选择Persistence,在右框中选择Persistence Unit。