- Spring Boot ORM 教程
- Spring Boot ORM - 主页
- Spring Boot ORM - 概述
- 环境设置
- Spring Boot ORM - JPA
- Spring Boot ORM 和 Spring Data JPA
- Spring Boot ORM - 创建项目
- Application.properties
- Spring Boot ORM - 更新项目
- Spring Boot ORM - 测试 Hibernate
- Spring Boot ORM 和 EclipseLink
- Maven EclipseLink
- 更新 EclipseLink 项目
- Spring Boot ORM - 测试 EclipseLink
- Spring Boot ORM 有用资源
- Spring Boot ORM - 快速指南
- Spring Boot ORM - 有用资源
- Spring Boot ORM - 讨论
Spring Boot ORM - 更新 EclipseLink 项目
Spring Boot 使用 HibernateJpaAutoConfiguration,它默认配置 hibernate 实现。为了切换到 EclipseLink,我们需要创建一个自定义配置类,它将扩展 JpaBaseConfiguration 类。JpaBaseConfiguration 是用于扩展和配置针对任何 ORM 实现的 JPA 的基类。以下是 EclipsLinkJpaConfiguration 的代码。
JPA 配置
创建 EclipseLink 配置类。
EclipsLinkJpaConfiguration.java
package com.tutorialspoint.springbootorm;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.logging.SessionLog;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;
// EclipseLink Specific Configuration Class
@Configuration
public class EclipsLinkJpaConfiguration extends JpaBaseConfiguration {
protected EclipsLinkJpaConfiguration(DataSource dataSource, JpaProperties properties,
ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
super(dataSource, properties, jtaTransactionManager);
}
// EclipseLink JPA Adaptor
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
// EclipseLink Properties
@Override
protected Map<String, Object> getVendorProperties() {
Map<String, Object> map = new HashMap<>();
map.put(PersistenceUnitProperties.WEAVING, "false");
map.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);
map.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
map.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.FINER_LABEL);
return map;
}
// Database Connection properties setup
@Bean
public static DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://:3306/tutorialspoint");
dataSource.setUsername("root");
dataSource.setPassword("root@123");
return dataSource;
}
}
我们分别使用 createJpaVendorAdapter()、dataSource() 和 getVendorProperties() 方法添加了适配器、数据源和属性。
更新实体
还需要更新实体,用 Integer 代替 int。
Employee.java
package com.tutorialspoint.springbootorm.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
// entity class to persist object to Employee Table
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
private String email;
// setter, getter methods
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
广告