【发布时间】:2021-08-31 04:30:15
【问题描述】:
我想在我的 spring 应用程序中使用 EclipseLink ORM,但我不知道如何将默认 Hibernate 更改为 EclipseLink。我将依赖项添加到我的 pom.xml 文件中。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
接下来我用 EclipseLinkJpaConfiguration 创建了配置文件
package com.example.configuration;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
import org.springframework.context.annotation.Configuration;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {
protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManager,
ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
super(dataSource, properties, jtaTransactionManager);
}
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
@Override
protected Map<String, Object> getVendorProperties() {
HashMap<String, Object> map = new HashMap<>();
map.put(PersistenceUnitProperties.WEAVING, true);
map.put(PersistenceUnitProperties.DDL_GENERATION, "drop-and-create-tables");
return map;
}
private String detectWeavingMode() {
return InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? "true" : "static";
}
}
最后我创建了 EclipseLinkFactory
package com.example.demo;
import java.lang.module.Configuration;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class EclipseLinkFactory{
private EntityManagerFactory efact;
private EntityManager eman;
public EntityManager getSessionFactory() {
efact = Persistence.createEntityManagerFactory("cars-pu");
eman = efact.createEntityManager();
return eman;
}
public EntityManager getEntityManager() {
return this.eman;
}
public EntityManagerFactory getEntityManagerFactory() {
return this.efact;
}
public EntityTransaction getTransaction() {
return this.eman.getTransaction();
}
}
但是当我运行服务器时,我得到了这个错误
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project demo: Compilation failure: Compilation failure:
[ERROR] /D:/test/java-eclipselink/src/main/java/com/example/configuration/EclipseLinkJpaConfiguration.java:[13,42] package org.springframework.orm.jpa.vendor does not exist
[ERROR] /D:/test/java-eclipselink/src/main/java/com/example/configuration/EclipseLinkJpaConfiguration.java:[14,42] package org.springframework.orm.jpa.vendor does not exist
[ERROR] /D:/test/compare_frameworks_orm/java-eclipselink/src/main/java/com/example/configuration/EclipseLinkJpaConfiguration.java:[15,43] package org.springframework.transaction.jta does not exist
[ERROR] /D:/test/compare_frameworks_orm/java-eclipselink/src/main/java/com/example/configuration/EclipseLinkJpaConfiguration.java:[26,40] cannot find symbol
[ERROR] symbol: class JtaTransactionManager
[ERROR] location: class com.example.configuration.EclipseLinkJpaConfiguration
[ERROR] /D:/test/compare_frameworks_orm/java-eclipselink/src/main/java/com/example/configuration/EclipseLinkJpaConfiguration.java:[32,15] cannot find symbol
[ERROR] symbol: class AbstractJpaVendorAdapter
[ERROR] location: class com.example.configuration.EclipseLinkJpaConfiguration
[ERROR] /D:/test/compare_frameworks_orm/java-eclipselink/src/main/java/com/example/configuration/EclipseLinkJpaConfiguration.java:[33,20] cannot find symbol
[ERROR] symbol: class EclipseLinkJpaVendorAdapter
[ERROR] location: class com.example.configuration.EclipseLinkJpaConfiguration
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
我做错了什么?
第二种方法
我还找到了 persistence.xml 文件的方法,它可能更容易,但我不知道如何正确地做到这一点。我只需要在 src 中创建 META-INF 文件并在其中创建文件 persistence.xml?接下来在pom.xml 中添加依赖项org.eclipse.persistence.jpa 和mysql-connector-java,所以当我运行服务器maven 时应该下载所有包。最后我应该在persistence.xml 中添加模型并使用EntityManager eman 来获取具有关系等的实体?
为了生成项目结构,我使用了 Spring Initializr。 我有问题,因为我发现了太多信息,我不知道如何处理它。请帮帮我。
【问题讨论】:
标签: java spring spring-boot hibernate maven