【问题标题】:TomEE + OpenJPA: Error creating EntityManager using container managed DataSourceTomEE + OpenJPA:使用容器管理的数据源创建 EntityManager 时出错
【发布时间】:2014-09-19 00:28:46
【问题描述】:

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

持久性提供程序正在尝试使用 persistence.xml 文件中的属性来解析数据源。 Java 数据库连接 (JDBC) 驱动程序或数据源类名称必须在 openjpa.ConnectionDriverName 或 javax.persistence.jdbc.driver 属性中指定。配置中提供了以下属性:“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();
    }

}

【问题讨论】:

    标签: java openjpa openejb apache-tomee


    【解决方案1】:

    您正在使用 JTA 管理的实体管理器工厂,我认为与其手动创建 EntityManagerFactory,不如让应用程序服务器为您完成,就像在控制器中这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多