From 107ef1f305f3704c2fba1a42bb1d359c7b620271 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 4 Oct 2017 13:08:19 -0400 Subject: [PATCH] Remove EclipseLink from JPA sample https://build.spring.io/browse/INTSAMPLES-NIGHTLY-2239/ Having Hibernate and EclipseLink in the same application isn't a goal. On the other hand the sample must be concentrate on the Spring Integration, not a complexity brought by the Spring Boot auto-configuration override. * Remove the EclipseLink support from the JPA samples and rely only on the Hibernate auto-configuration * Remove an appropriate message from the README --- basic/jpa/README.md | 17 ---- .../jpa/EclipseLinkAutoConfiguration.java | 91 ------------------- .../integration/samples/jpa/Main.java | 39 +------- basic/jpa/src/main/resources/schema.sql | 6 +- .../integration/samples/jpa/JpaTests.java | 3 +- build.gradle | 11 +-- 6 files changed, 9 insertions(+), 158 deletions(-) delete mode 100644 basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java diff --git a/basic/jpa/README.md b/basic/jpa/README.md index a2802e49..d54501a9 100644 --- a/basic/jpa/README.md +++ b/basic/jpa/README.md @@ -10,29 +10,12 @@ This sample illustrates how the JPA Components can be used. The example presente The first example demonstrates the use of an JPA Outbound gateway to retrieve a list of people. The second example uses an JPA Outbound Gateway in order to create a new Person record and then return the newly created Person record. -You have the option to choose between the following 3 persistence providers: - -* [Hibernate](http://www.hibernate.org/) -* [OpenJPA](http://openjpa.apache.org/) -* [EclipseLink](http://www.eclipse.org/eclipselink/) - -# Getting Started - Hibernate works out of the box and there are 2 options on how to execute the sample: * running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application) * or from the command line: $ gradlew :jpa:run -For **OpenJPA** and **EclipseLink** to work, you must provide a [Java Agent](http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html). -When using the Gradle Application Plugin, this is taken care of for you behind the scenes automatically. However, -when running the sample from within STS start the Main class with the following JVM flags: - - -javaagent:/path/to/.m2/repository/org/springframework/spring-instrument/4.2.4.RELEASE/spring-instrument-4.2.4.RELEASE.jar - -javaagent:/path/to/.m2/repository/org/apache/openjpa/openjpa/2.4.0/openjpa-2.4.0.jar - -With these flags you will be able to use all 3 persistence providers at once. - # Resources For help please take a look at the Spring Integration documentation: diff --git a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java deleted file mode 100644 index 13b993d8..00000000 --- a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright 2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.integration.samples.jpa; - -import java.util.HashMap; -import java.util.Map; -import javax.persistence.EntityManager; -import javax.sql.DataSource; - -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionOutcome; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.SpringBootCondition; -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; -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.ConditionContext; -import org.springframework.context.annotation.Conditional; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; -import org.springframework.core.type.AnnotatedTypeMetadata; -import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; -import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; -import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; -import org.springframework.transaction.annotation.EnableTransactionManagement; -import org.springframework.transaction.jta.JtaTransactionManager; -import org.springframework.util.ClassUtils; - -/** - * @author Artem Bilan - * @since 4.2 - */ -@Configuration -@ConditionalOnClass({LocalContainerEntityManagerFactoryBean.class, - EnableTransactionManagement.class, EntityManager.class}) -@Conditional(EclipseLinkAutoConfiguration.EclipseLinkEntityManagerCondition.class) -@AutoConfigureAfter({DataSourceAutoConfiguration.class}) -@Profile("eclipseLink") -public class EclipseLinkAutoConfiguration extends JpaBaseConfiguration { - - public EclipseLinkAutoConfiguration(DataSource dataSource, JpaProperties properties, - ObjectProvider jtaTransactionManagerProvider, - ObjectProvider transactionManagerCustomizers) { - super(dataSource, properties, jtaTransactionManagerProvider, transactionManagerCustomizers); - } - - @Override - protected AbstractJpaVendorAdapter createJpaVendorAdapter() { - return new EclipseLinkJpaVendorAdapter(); - } - - @Override - protected Map getVendorProperties() { - return new HashMap(); - } - - - @Order(Ordered.HIGHEST_PRECEDENCE + 20) - static class EclipseLinkEntityManagerCondition extends SpringBootCondition { - - @Override - public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { - if (ClassUtils.isPresent("org.eclipse.persistence.jpa.JpaEntityManager", context.getClassLoader())) { - return ConditionOutcome.match("found JpaEntityManager class"); - } - else { - return ConditionOutcome.noMatch("did not find JpaEntityManager class"); - } - } - - } - -} diff --git a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java index 511adfd9..bec7f3d8 100644 --- a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java +++ b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java @@ -22,7 +22,7 @@ import java.util.Scanner; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ImportResource; @@ -40,7 +40,7 @@ import org.springframework.util.StringUtils; * @version 1.0 * */ -@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class) +@SpringBootApplication(exclude = JpaRepositoriesAutoConfiguration.class) @ImportResource("spring-integration-context.xml") public class Main { @@ -64,38 +64,9 @@ public class Main { + "\n " + "\n========================================================="); - System.out.println("Please enter a choice and press : "); - System.out.println("\t1. Use Hibernate"); - System.out.println("\t2. Use EclipseLink"); - - System.out.println("\tq. Quit the application"); - System.out.print("Enter you choice: "); - - SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Main.class) - .web(WebApplicationType.NONE); - - while (true) { - final String input = scanner.nextLine(); - - if ("1".equals(input.trim())) { - springApplicationBuilder.sources(HibernateJpaAutoConfiguration.class); - break; - } - else if ("2".equals(input.trim())) { - springApplicationBuilder.profiles("eclipseLink"); - break; - } - else if ("q".equals(input.trim())) { - System.out.println("Exiting application...bye."); - System.exit(0); - } - else { - System.out.println("Invalid choice\n\n"); - System.out.print("Enter you choice: "); - } - } - - ConfigurableApplicationContext context = springApplicationBuilder.run(args); + ConfigurableApplicationContext context = new SpringApplicationBuilder(Main.class) + .web(WebApplicationType.NONE) + .run(args); final PersonService personService = context.getBean(PersonService.class); diff --git a/basic/jpa/src/main/resources/schema.sql b/basic/jpa/src/main/resources/schema.sql index 45bab574..87a0eb53 100644 --- a/basic/jpa/src/main/resources/schema.sql +++ b/basic/jpa/src/main/resources/schema.sql @@ -1,9 +1,5 @@ drop table if EXISTS PEOPLE; -DROP TABLE if EXISTS OPENJPA_SEQUENCE_TABLE; -DROP TABLE if EXISTS SEQUENCE; - -CREATE TABLE OPENJPA_SEQUENCE_TABLE (ID TINYINT NOT NULL, SEQUENCE_VALUE BIGINT, PRIMARY KEY (ID)); CREATE TABLE PEOPLE (id BIGINT generated by default as identity, name VARCHAR(255), CREATED_DATE_TIME TIMESTAMP, PRIMARY KEY (id)); -CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1 INCREMENT BY 1; \ No newline at end of file +CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1 INCREMENT BY 1; diff --git a/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/JpaTests.java b/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/JpaTests.java index 6209874d..ee0feeac 100644 --- a/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/JpaTests.java +++ b/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/JpaTests.java @@ -38,8 +38,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; * */ @RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, - classes = {Main.class, HibernateJpaAutoConfiguration.class}) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) public class JpaTests { @Autowired diff --git a/build.gradle b/build.gradle index af6a76a2..ab5d6b00 100644 --- a/build.gradle +++ b/build.gradle @@ -783,10 +783,10 @@ project('jmx') { project('jpa') { description = 'JPA Basic Sample' -// apply plugin: 'application' + apply plugin: 'application' apply plugin: 'org.springframework.boot' -// mainClassName = 'org.springframework.integration.samples.jpa.Main' + mainClassName = 'org.springframework.integration.samples.jpa.Main' dependencies { compile 'org.springframework.boot:spring-boot-starter-data-jpa' @@ -795,19 +795,12 @@ project('jpa') { runtime "org.springframework:spring-instrument" runtime "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$jpa21ApiVersion" - runtime "org.eclipse.persistence:org.eclipse.persistence.jpa:$eclipseLinkVersion" testCompile 'org.springframework.boot:spring-boot-starter-test' } -/* bootRun { - main = 'org.springframework.integration.samples.jpa.Main' - }*/ tasks.withType(JavaExec) { standardInput = System.in - jvmArgs classpath.files.findAll { - it.name ==~ /(spring-instrument.+)/}.collect{"-javaagent:$it" - } } }