我现在正在使用Spring 3.1和JPA 2.0开发一个项目,我收到一条日志消息错误告诉我:“没有名为manager1的EntityManager的持久性提供程序”。
这是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="DemoSpringCore" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>app.demo.model.Operation</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3309/test"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
这是我的托管实体“Operation.java”: 包app.demo.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="operation")
public class Operation implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="a",length=9)
private int a;
@Column(name="b",length=9)
private int b;
@Column(name="c",length=10)
private int c;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
这是我的java配置文件“AppConfiguration.java”,它是Spring 3.1中“ApplicationContext.xml”的替代品:
package app.demo.config;
import java.util.Properties;
import com.jolbox.bonecp.BoneCPDataSource;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import app.demo.dao.OperationDAO;
import app.demo.dao.OperationDAOImpl;
import app.demo.service.OperationServiceImpl;
import app.demo.service.OperationService;
import org.hibernate.ejb.HibernatePersistence;
@Configuration
@PropertySource("/META-INF/application.properties")
public class AppConfiguration {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
// private static final String PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY =
// "hibernate.ejb.naming_strategy";
@Resource
private Environment environment;
@Bean(name = "operationServiceBean")
public OperationService operationService() {
return new OperationServiceImpl();
}
@Bean(name = "operationDAOBean")
public OperationDAO operationDAO() {
return new OperationDAOImpl();
}
@Bean
public DataSource dataSource() {
BoneCPDataSource dataSource = new BoneCPDataSource();
dataSource.setDriverClass(environment
.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
dataSource.setJdbcUrl(environment
.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
dataSource.setUsername(environment
.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
dataSource.setPassword(environment
.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));
return (DataSource) dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
throws ClassNotFoundException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean
.setPersistenceProviderClass(HibernatePersistence.class);
entityManagerFactoryBean.setDataSource(dataSource());
Properties jpaProterties = new Properties();
jpaProterties.put(PROPERTY_NAME_HIBERNATE_DIALECT, environment
.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProterties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL, environment
.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProterties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL, environment
.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
jpaProterties
.put(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN,
environment
.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
// jpaProterties.put(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY,
// environment
// .getRequiredProperty(PROPERTY_NAME_HIBERNATE_NAMING_STRATEGY));
entityManagerFactoryBean.setJpaProperties(jpaProterties);
return entityManagerFactoryBean;
}
}
最后是我的“application.properties”,其中我放了数据源连接属性:
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3309/test
db.username=root
db.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.format_sql=true
hibernate.show_sql=true
entitymanager.packages.to.scan=app.demo.model
P.S:我同时使用了两种方法“appliaction.properties”和“persistence.xml”,并且同时在两者之后使用这些方法希望它们能够正常工作但没有任何事情发生! 此外,我在“AppConfiguration.java”和“persistence.xml”中添加了provider属性,并验证了所有依赖jar文件的存在!
提前致谢:)
答案 0 :(得分:0)
您必须将软件包设置为直接在emf中扫描,并设置持久性单元名称:
entityManagerFactoryBean.setPackagesToScan(...);
entityManagerFactoryBean.setPersistenceUnitName(...);
之后你甚至不再需要persistence.xml了。
另外,我建议(如果你完全摆脱了persistence.xml)使用正确的jpa属性:
javax.persistence.jdbc.driver
javax.persistence.jdbc.url
javax.persistence.jdbc.user
javax.persistence.jdbc.password