简体   繁体   中英

TomEE + OpenJPA: Error creating EntityManager using container managed DataSource

I'm trying to configure an example JPA application in Eclipse, and deploy it to TomEE+. The datasource is container managed. I keep seeing the following error when attempting to create the 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".

Any idea what is wrong with this configuration?

Below is the code.

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

}

You are using JTA-managed Entity Manager Factory, I think that instead of manual creation of EntityManagerFactory you should let application server do it for you, like this in 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();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM