Simple Springboot JPA Eclipeslink Example

Now we will try to run the same example Springboot JPA Example with eclipselink :

1. We need to exclude hibernate library and add eclipselink dependency:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.6.4</version>
        </dependency>

2. add two new methods for Eclipselink JPA vendor in @SpringBootApplication (ExampleApplication) class:

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

    @Override
    protected Map<String, Object> getVendorProperties() {
        // Turn off dynamic weaving to disable LTW lookup in static weaving mode
        return Collections.singletonMap("eclipselink.weaving", "false");
    }

new we should be ready to test it , so we will create same bean as command line runner :

    @Bean
    public CommandLineRunner exampleRunner(CustomerRepository customerRepository) {
        return (args) -> {
            Customer customer = new Customer("Customer 1", new ArrayList<>());
            customer.getOrders().add(new Order(customer, "order 1"));
            customer.getOrders().add(new Order(customer, "order 2"));
            customerRepository.save(customer);

            customerRepository.findAll().forEach(c -> {
                logger.info(c.toString());
            });

            final Iterable<Customer> customers = customerRepository.findAll();
            customers.forEach(c -> {
                c.getOrders().forEach(order -> {
                    order.setOrderName(order.getOrderName() + "1");
                });
                customerRepository.save(c);
            });

            customerRepository.findAll().forEach(c -> {
                logger.info(c.toString());
            });
            customer = customerRepository.findOne(customer.getId());
            customer.setName(customer.getName() + "1");
            customerRepository.save(customer);
            customerRepository.findAll().forEach(c -> {
                logger.info(c.toString());
            });

        };
    }

the optup will be almost the same as Springboot JPA Example , except the version , in Eclipselink the version will start with 1.

Thanks for reading this .

source code : sources

Category: Java Tutorials