java - TomEE + OpenJPA : Error creating EntityManager using container managed DataSource

标签 java openjpa openejb apache-tomee

我正在尝试在 Eclipse 中配置示例 JPA 应用程序,并将其部署到 TomEE+。数据源是容器管理的。尝试创建 EntityManager 时,我不断看到以下错误:

The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@414793b4".

知道这个配置有什么问题吗?

下面是代码。

tomee.xml

<tomee>
  <Resource id="jdbc/MyAppDS" type="DataSource">
    JdbcDriver          com.microsoft.sqlserver.jdbc.SQLServerDriver
    JdbcUrl             jdbc:sqlserver://localhost:1433/db_dev
    UserName            user
    Password            password
    JtaManaged          true
    DefaultAutoCommit   false
  </Resource>
</tomee>

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="2.0">

    <persistence-unit name="Simplest" transaction-type="JTA">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <jta-data-source>jdbc/MyAppDS</jta-data-source>
        <class>social.Media</class>
    </persistence-unit>
 </persistence>

Media.java

package social;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "media")
public class Media {

    @Id
    @Column(name = "id")
    private String id;

    private String description;

    private String title;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

}

Controller.java

package social;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Controller {

    @Inject private Media media;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Simplest");

        EntityManager em = emf.createEntityManager(); // exception reported on this line
        .
        .
        .

        return media.getDescription();
    }

}

最佳答案

您正在使用 JTA 管理的实体管理器工厂,我认为您应该让应用程序服务器为您执行此操作,而不是手动创建 EntityManagerFactory,就像在 Controller 中一样:

@Path("/hello")
public class Controller {

  @PersistenceContext(unitName="Simplest")
  private EntityManager em;

  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayHello() {
      // get Media from database - replace with your own code
      Media media = em.find(Media.class, "1");
      return media.getDescription();
  }
}

关于java - TomEE + OpenJPA : Error creating EntityManager using container managed DataSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984729/

相关文章:

java - OSGi 包之间的 OpenJPA 持久性

java - Apache OpenJPA - NamedQuery 异常

java - 如何在 Hibernate 中使用 TomEE

java - 如何在 JUnit 中构建自动回复 JMS 监听器(在 OpenEJB 中)

java - 具有多个分隔符的 Split()(不起作用)

java - 从对象的数组列表的数组列表中获取所有可能的组合

java - Spring Security @PreAuthorize 破坏 Jersey @Context UriInfo 注入(inject)

java - Errai 中@Default 字段的模糊解析

hibernate - JPA 是否支持数据库中没有外键约束的实体之间的 @OneToMany 映射?

java - OpenEJB 中的@Module 注解是什么以及如何使用它?