1
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());
    }

上面的代码仍在寻找数据源。

请帮我。

4

1 回答 1

1

删除<non-jta-data-source>标签,删除你放在类中的属性,添加到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>

您还需要在 <persistence-unit> 标记下方添加持久性提供程序:

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

希望这可以帮助。

于 2013-02-26T22:40:23.707 回答