在persistence.xml中运行带有数据源的独立JPA

时间:2012-12-15 01:24:44

标签: eclipselink

Below is my perisistence.xml, having the datasource.

<persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/MyDS</non-jta-data-source>
</persistence-unit>

我正在尝试独立创建EntityManager并且无法执行示例查询,  请帮帮我。

public static void main(String[] args) {

Map<String, String> properties = new HashMap<String, String>();

        // Ensure RESOURCE_LOCAL transactions is used.
        properties.put("javax.persistence.transactionType",
            PersistenceUnitTransactionType.RESOURCE_LOCAL.name());

        // Configure the internal EclipseLink connection pool
        properties.put("javax.persistence.jdbc.driver" , "oracle.jdbc.OracleDriver");
        properties.put("javax.persistence.jdbc.url", "jdbc:oracle:thin:@localhost:1521:ORCL");
        properties.put("javax.persistence.jdbc.user", "scott");
        properties.put("javax.persistence.jdbc.password", "tiger");

        // Ensure that no server-platform is configured
        properties.put("eclipselink.target-server", TargetServer.None);

        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME,properties);
        entityManager = entityManagerFactory.createEntityManager();                 
        Query query = entityManager.createQuery(" select d from  XyzEntity d");
        List<XyzEntity> xyzEntityList = query.getResultList();
        System.out.println("size is"+xyzEntityList.size());
    }

以上代码仍在寻找数据源。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

删除&lt;非jta-data-source&gt;标记,删除放在类中的属性,并将它们添加到persistence.xml文件中,如下所示:

<properties>
  <property name="javax.persistence.jdbc.driver" value="the.driver"/>
  <property name="javax.persistence.jdbc.url" value="the url"/>
  <property name="javax.persistence.jdbc.user" value="the user"/>
  <property name="javax.persistence.jdbc.password" value="the pass"/>
</properties>

您还需要在&lt; persistence-unit&gt;下添加您的持久性提供程序。标记:

<provider>bla.bla.bla.bla.BlaProvider</provider>

希望这有帮助。