SSCCE在这里:https://github.com/dims12/TrySpringJpaPlusHibernate
我试图在没有persistence.xml的情况下运行Spring,并具有以下配置:
@Configuration
@ComponentScan
@ImportResource("classpath:data_source.xml")
@EnableJpaRepositories("org.inthemoon.train.chinese.repositories")
public class BaseConfig {
@Autowired
private DataSource dataSource;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean ans =
new LocalContainerEntityManagerFactoryBean();
ans.setDataSource(dataSource);
ans.setJpaVendorAdapter(jpaVendorAdapter());
ans.setPackagesToScan("org.inthemoon.train.chinese.data");
return ans;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter ans = new HibernateJpaVendorAdapter();
ans.setShowSql(false);
ans.setGenerateDdl(true);
ans.setDatabase(Database.H2);
return ans;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager ans = new JpaTransactionManager();
ans.setEntityManagerFactory(emf);
return ans;
}
}
它会导致以下异常
Exception in thread "main" org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
...
有什么方法可以在第一次尝试中配置IoC
吗?
更新
我正在使用以下语言:
compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.5.Final'
compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.10.5.RELEASE'
更新2
我尝试了8种不同版本的hibernate-core
来构建1.10.5.RELEASE
的spring。
从5.2.1
到5.2.6
的版本都会导致相同的异常。
NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
版本5.1.3
和5.0.11
导致
ClassNotFoundException: org.hibernate.ejb.HibernateEntityManagerFactory
唯一的版本导致了更复杂的事情是5.2.0
。它引起了
SchemaManagementException: Attempt to resolve foreign key metadata from JDBC metadata failed to find column mappings for foreign key named [FKLOK22W31RKBMIIC2J96T9LTCN
出现了以下问题:
1)这是否意味着5.2.0
版本与1.10.5
兼容?
2)如果没有实验,我怎么知道这一点?
3)用这种方式猜测版本是正常的吗?依赖管理工具的目的不是为了避免这样的事情吗?如果spring-data-jpa:1.10.5
依赖于5.2.0
的hibernate,那么为什么它的POM
中没有描述这一点?
更新3
开箱即用的示例:https://github.com/dims12/TrySpringJpaPlusHibernate
它不起作用。
发布于 2017-01-16 04:10:45
Spring数据JPA v1.10.6依赖于SpringV4.2(准确地说是4.2.9),SpringV4.2不支持Hibernate v5.2。对Hibernate v5.2的支持是添加了仅在春季v4.3。因此,您必须将Spring依赖项升级到v4.3。
将下列依赖项添加到Gradle构建文件中应该有效:
compile 'org.springframework:spring-beans:4.3.4.RELEASE'
compile 'org.springframework:spring-context:4.3.4.RELEASE'
compile 'org.springframework:spring-context-support:4.3.4.RELEASE'
compile 'org.springframework:spring-core:4.3.4.RELEASE'
compile 'org.springframework:spring-jdbc:4.3.4.RELEASE'
compile 'org.springframework:spring-orm:4.3.4.RELEASE'
compile 'org.springframework:spring-tx:4.3.4.RELEASE'
修改后的代码可在Github上查阅。以gradle test
的形式运行Gradle测试,以验证一切是否正常。
发布于 2017-01-12 15:11:01
通常,当您有此异常时,这意味着您应该有hibernate-entitymanager到您的端口。如果您使用的是maven,则可以将其添加到pom中。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
我还用一个bean来定义我的dataSource,我不像你那样给它注入自动头发,我不知道它是否那样工作。我宁愿这样做
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/gescable");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
发布于 2017-01-13 11:53:11
我刚刚在我的项目中测试了这个版本:
<hibernate.version>5.2.5.Final</hibernate.version>
<version>1.10.5.RELEASE</version>
对我来说很管用。
我的配置文件如下:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "ge.shemo.repositories")
@EnableJpaAuditing
public class PersistenceConfig {
@Autowired
private Environment env;
@Value("${init-db:false}")
private String initDatabase;
@Bean
public PlatformTransactionManager transactionManager() {
EntityManagerFactory factory = entityManagerFactory().getObject();
return new JpaTransactionManager(factory);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(Boolean.FALSE);
vendorAdapter.setShowSql(Boolean.FALSE);
factory.setDataSource(dataSource());
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("ge.shemo");
Properties jpaProperties = getHibernateProperties();
factory.setJpaProperties(jpaProperties);
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory;
}
private Properties getHibernateProperties() {
Properties prop = new Properties();
/*prop.put("hibernate.format_sql", "true");*/
/*prop.put("hibernate.hbm2ddl.auto", "update");*/
//prop.put("hibernate.hbm2ddl.auto", "validate");
// prop.put("hibernate.hbm2ddl.import_files","sql/import.sql");
/*prop.put("hibernate.ejb.entitymanager_factory_name", "irakli");*/
//prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect", "ge.shemo.config.SQLServerUnicodeDialect");
return prop;
}
@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
@Bean
public DataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ds.setUrl("jdbc:sqlserver://LinkToDB:1433;useUnicode=true;characterEncoding=UTF-8;DatabaseName=Your_DB");
ds.setUsername("USER");
ds.setPassword("PASS");
return ds;
}
@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
//databasePopulator.addScript(new ClassPathResource("db.sql"));
dataSourceInitializer.setDatabasePopulator(databasePopulator);
dataSourceInitializer.setEnabled(Boolean.parseBoolean(initDatabase));
return dataSourceInitializer;
}
@Bean
public AuditorAware<Long> createAuditorProvider() {
return new SecurityAuditor();
}
@Bean
public AuditingEntityListener createAuditingListener() {
return new AuditingEntityListener();
}
}
https://stackoverflow.com/questions/41550583
复制相似问题