2016-08-20 112 views
3

我有一個Spring Boot 1.4應用程序,我試圖用Eclipselink而不是休眠。
現在,這是我做的:
從DEPS不能使用彈簧啓動eclipselink

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-data-jpa</artifactId> 
    <exclusions> 
    <exclusion> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
    </exclusion> 
    <exclusion> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
    </exclusion> 
    </exclusions> 
</dependency> 

新增的EclipseLink

<dependency> 
    <groupId>org.eclipse.persistence</groupId> 
    <artifactId>eclipselink</artifactId> 
    <version>2.6.3</version> 
</dependency> 

配置JpaBaseConfiguration

@Configuration 
public class JpaConfiguration extends JpaBaseConfiguration { 

protected JpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider) { 
    super(dataSource, properties, jtaTransactionManagerProvider); 
} 

@Bean 
public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder, @Autowired DataSource dataSource) { 
    final LocalContainerEntityManagerFactoryBean ret = builder.dataSource(dataSource) 
      .packages("com.inkdrop.app.domain.models") 
      .persistenceUnit("chathub-perstence-unit") 
      .properties(getVendorProperties()) 
      .build(); 

    return ret; 
} 

@Override 
protected AbstractJpaVendorAdapter createJpaVendorAdapter() { 
    return new EclipseLinkJpaVendorAdapter(); 
} 

@Override 
protected Map<String, Object> getVendorProperties() { 
    return Collections.singletonMap("eclipselink.weaving", "false"); 
} 

當我運行的應用程序,它創建刪除休眠數據庫中的表(代表我的模型),然後拋出這個錯誤:

Caused by: java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.metamodel.SingularAttributeImpl cannot be cast to javax.persistence.metamodel.PluralAttribute 
     at org.eclipse.persistence.internal.jpa.querydef.FromImpl.join(FromImpl.java:485) 
     at org.springframework.data.jpa.repository.query.QueryUtils.getOrCreateJoin(QueryUtils.java:596) 
     at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:524) 
     at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:332) 
     at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:275) 
     at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:180) 
     at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:109) 
     at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:49) 
     at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109) 
     at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88) 
     at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73) 
     at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:118) 
     at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:241) 
     at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:68) 
     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) 
     at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214) 

我嘗試以下thisthis教程,但並沒有幫助

回答

2

我覺得你的問題是春天的數據版本,您使用的EclipseLink版本之間的不兼容。

SpringBoot中的Eclipselink支持看起來很糟糕。
在彈簧引導依賴pom.xml中:

<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-dependencies</artifactId> 
<version>1.4.0.RELEASE</version> 

properties標籤,你可以看到:有關的EclipseLink

<properties> 
... 
<hibernate.version>5.0.9.Final</hibernate.version> 
<hibernate-validator.version>5.2.4.Final</hibernate-validator.version> 
... 
</properties> 

但零信息。
以同樣的方式,在spring-boot-starter-data-jpa POM,你必須在dependencies標籤爲hibernate-core的直接依賴:

<dependencies> 
    ... 
     <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <exclusions> 
      <exclusion> 
       <groupId>org.apache.geronimo.specs</groupId> 
       <artifactId>geronimo-jta_1.1_spec</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
    ... 
<dependencies> 

dependenciesManagement標籤爲EclipseLink的沒有依賴性。很遺憾,因爲它可以給一個方式,projets按需使用與當前春季啓動版本以這種方式兼容的EclipseLink版本:

<dependency> 
    <groupId>org.eclipse.persistence</groupId> 
    <artifactId>eclipselink</artifactId> 
</dependency> 

但由於此功能缺失,必須指定的版本Eclipslink在你的pom.xml:

<dependency> 
    <groupId>org.eclipse.persistence</groupId> 
    <artifactId>eclipselink</artifactId> 
    <version>2.6.3</version> 
</dependency> 

在你的情況下,它可能是複雜的猜測,因爲您的教程使用1.3.2彈簧引導版本其中的EclipseLink的版本與1.4彈簧引導版本兼容。

爲了解決您的問題,我建議您嘗試使用與您的教程或任何使用SpringBoot和EclipseLink的工作應用程序相同的版本(EclipseLink和Spring Boot)來檢查版本兼容性問題。
如果沒關係,那麼如果你想更新你的Spring Boot或者EclipseLink版本,或者兩者兼得,你必須進行測試來驗證Spring Boot和EclipseLink版本之間的獲勝組合。

+0

我做了它的工作,但發現很多問題和不兼容,我剛回去休眠 –