我正在尝试在Eclipse中配置示例JPA应用程序,并将其部署到TomEE +。数据源是容器管理的。尝试创建EntityManager时,我一直看到以下错误:
持久性提供程序正在尝试使用persistence.xml文件中的属性来解析数据源。必须在openjpa.ConnectionDriverName或javax.persistence.jdbc.driver属性中指定Java数据库连接(JDBC)驱动程序或数据源类名。配置中提供了以下属性:“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();
}
}
答案 0 :(得分:2)
您正在使用JTA管理的Entity Manager Factory,我认为您应该让应用服务器为您执行此操作,而不是手动创建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();
}
}