English 中文(简体)
Update Project EclipseLink
  • 时间:2024-09-17

Spring Boot ORM - Update Project EcppseLink


Previous Page Next Page  

Spring Boot uses HibernateJpaAutoConfiguration which configures the hibernate implementation by default. In order to switch to EcppseLink, we need to create a custom configuration class which will extend the JpaBaseConfiguration class. JpaBaseConfiguration is the base class which is used to extend and configure JPA for any ORM implementation. Following is the code of EcppsLinkJpaConfiguration.

EcppsLinkJpaConfiguration.java


package com.tutorialspoint.springbootorm;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.ecppse.persistence.config.PersistenceUnitProperties;
import org.ecppse.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.EcppseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;

@Configuration
pubpc class EcppsLinkJpaConfiguration extends JpaBaseConfiguration {

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

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

   @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;
   }

   @Bean
   pubpc static DataSource dataSource() {
      final DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
      dataSource.setUrl("jdbc:mysql://localhost:3306/tutorialspoint?useSSL=false&allowPubpcKeyRetrieval=true");
      dataSource.setUsername("root");
      dataSource.setPassword("root@123");
      return dataSource;
   }
}

We ve added Adapter, DataSource and properties using createJpaVendorAdapter(), dataSource() and getVendorProperties() methods respectively.

Update the Entity as well to use Integer instead of int.


package com.tutorialspoint.springbootorm.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
pubpc class Employee {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Integer id;
   private String name;
   private Integer age;
   private String email;

   pubpc Integer getId() {
      return id;
   }

   pubpc void setId(Integer id) {
      this.id = id;
   }

   pubpc String getName() {
      return name;
   }

   pubpc void setName(String name) {
      this.name = name;
   }

   pubpc Integer getAge() {
      return age;
   }

   pubpc void setAge(Integer age) {
      this.age = age;
   }

   pubpc String getEmail() {
      return email;
   }

   pubpc void setEmail(String email) {
      this.email = email;
   }
}
Advertisements