INTSAMPLES-148: Make JPA Sample as Boot App

JIRA: https://jira.spring.io/browse/INTSAMPLES-148

Upgrade several dependencies, especially OpenJPA-2.4.0 to support Java 8 Class Transformation

Upgrade to DSL-1.1.2 and other polishing

Upgrade to SI-4.3.0.M1
This commit is contained in:
Artem Bilan
2015-12-18 15:34:34 -05:00
parent b52ee9ed9c
commit 6080639682
22 changed files with 234 additions and 250 deletions

View File

@@ -0,0 +1,64 @@
package org.springframework.integration.samples.jpa;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
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.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.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 {
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}
@Override
protected Map<String, Object> getVendorProperties() {
return new HashMap<String, Object>();
}
@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");
}
}
}
}

View File

@@ -13,13 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.jpa;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
import org.springframework.integration.samples.jpa.domain.Person;
import org.springframework.integration.samples.jpa.service.PersonService;
import org.springframework.util.StringUtils;
@@ -30,18 +35,16 @@ import org.springframework.util.StringUtils;
* @author Gunnar Hillert
* @author Amol Nayak
* @author Gary Russell
* @author Artem Bilan
* @version 1.0
*
*/
public final class Main {
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@ImportResource("spring-integration-context.xml")
public class Main {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
/**
* Prevent instantiation.
*/
private Main() {}
/**
* Load the Spring Integration Application Context
*
@@ -68,19 +71,19 @@ public final class Main {
System.out.println("\tq. Quit the application");
System.out.print("Enter you choice: ");
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Main.class).web(false);
while (true) {
final String input = scanner.nextLine();
if("1".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("hibernate");
springApplicationBuilder.sources(HibernateJpaAutoConfiguration.class);
break;
} else if("2".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("openjpa");
springApplicationBuilder.profiles("openJpa");
break;
} else if("3".equals(input.trim())) {
context.getEnvironment().setActiveProfiles("eclipselink");
springApplicationBuilder.profiles("eclipseLink");
break;
} else if("q".equals(input.trim())) {
System.out.println("Exiting application...bye.");
@@ -91,9 +94,7 @@ public final class Main {
}
}
context.load("classpath:META-INF/spring/integration/*-context.xml");
context.registerShutdownHook();
context.refresh();
ConfigurableApplicationContext context = springApplicationBuilder.run(args);
final PersonService personService = context.getBean(PersonService.class);
@@ -161,10 +162,7 @@ public final class Main {
}
}
}
/**
* @param service
* @param input
*/
private static void findPeople(final PersonService service) {
System.out.println("ID NAME CREATED");
@@ -183,6 +181,6 @@ public final class Main {
}
System.out.println("==================================\n\n");
}
}

View File

@@ -0,0 +1,64 @@
package org.springframework.integration.samples.jpa;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
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.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.OpenJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ClassUtils;
/**
* @author Artem Bilan
* @since 4.2
*/
@Configuration
@ConditionalOnClass({LocalContainerEntityManagerFactoryBean.class,
EnableTransactionManagement.class, EntityManager.class})
@Conditional(OpenJpaAutoConfiguration.OpenJpaEntityManagerCondition.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class})
@Profile("openJpa")
public class OpenJpaAutoConfiguration extends JpaBaseConfiguration {
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new OpenJpaVendorAdapter();
}
@Override
protected Map<String, Object> getVendorProperties() {
return new HashMap<String, Object>();
}
@Order(Ordered.HIGHEST_PRECEDENCE + 20)
static class OpenJpaEntityManagerCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (ClassUtils.isPresent("org.apache.openjpa.persistence.OpenJPAEntityManager", context.getClassLoader())) {
return ConditionOutcome.match("found OpenJPAEntityManager class");
}
else {
return ConditionOutcome.noMatch("did not find OpenJPAEntityManager class");
}
}
}
}

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.samples.jpa.domain;
import java.util.Date;
@@ -28,32 +29,29 @@ import javax.persistence.TemporalType;
/**
* A simple POJO representing a Person.
*
* @author Amol Nayak
* @author Gunnar Hillert
*
* @author Artem Bilan
*/
@Entity
@Table(name="PEOPLE")
@Table(name = "PEOPLE")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="CREATED_DATE_TIME")
@Column(name = "CREATED_DATE_TIME")
private Date createdDateTime;
public Person() {
super();
this.createdDateTime = new Date();
}
public Person(String name) {
super();
this.name = name;
this.createdDateTime = new Date();
}
@@ -106,7 +104,8 @@ public class Person {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
}
else if (!name.equals(other.name)) {
return false;
}
return true;

View File

@@ -1,2 +0,0 @@
drop table OPENJPA_SEQUENCE_TABLE;
drop table PEOPLE;

View File

@@ -1,2 +0,0 @@
insert into PEOPLE(id, name, CREATED_DATE_TIME)
values ('1001', 'Cartman', NOW());

View File

@@ -1,48 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa.xsd">
<!-- Setting Up the Database -->
<jdbc:embedded-database id="dataSource" type="H2"/>
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS" >
<jdbc:script location="classpath:H2-DropTables.sql" />
<jdbc:script location="classpath:H2-CreateTables.sql" />
<jdbc:script location="classpath:H2-PopulateData.sql" />
</jdbc:initialize-database>
<!-- Define the JPA transaction manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<constructor-arg ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="vendorAdaptor" />
<property name="packagesToScan" value="org.springframework.integration.samples.jpa"/>
</bean>
<bean id="abstractVendorAdaptor" abstract="true">
<property name="generateDdl" value="true" />
<property name="database" value="H2" />
<property name="showSql" value="false"/>
</bean>
<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>

View File

@@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa.xsd">
<import resource="classpath:/META-INF/spring/integration/commonJpa-context.xml" />
<bean id="vendorAdaptor" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
parent="abstractVendorAdaptor">
</bean>
</beans>

View File

@@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa.xsd">
<import resource="classpath:/META-INF/spring/integration/commonJpa-context.xml" />
<bean id="vendorAdaptor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
parent="abstractVendorAdaptor">
</bean>
</beans>

View File

@@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa.xsd">
<import resource="classpath:/META-INF/spring/integration/commonJpa-context.xml" />
<bean id="vendorAdaptor" class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"
parent="abstractVendorAdaptor">
</bean>
</beans>

View File

@@ -0,0 +1,8 @@
spring:
jpa:
generate-ddl: false
hibernate:
ddl-auto: false
show-sql: true
main:
banner-mode: "off"

View File

@@ -0,0 +1,3 @@
insert into PEOPLE(id, name, CREATED_DATE_TIME) values ('1001', 'Cartman', NOW());
INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0);

View File

@@ -1,28 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss.SSS} %-5p [%t][%c] %m%n" />
</layout>
</appender>
<!-- Loggers -->
<logger name="org.springframework.integration">
<level value="warn" />
</logger>
<logger name="org.springframework.integration.samples">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@@ -1,6 +1,8 @@
drop table PEOPLE;
DROP TABLE OPENJPA_SEQUENCE_TABLE;
drop table if EXISTS PEOPLE;
DROP TABLE if EXISTS OPENJPA_SEQUENCE_TABLE;
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 TABLE SEQUENCE (SEQ_NAME VARCHAR(50), SEQ_COUNT DECIMAL(15));
CREATE TABLE PEOPLE (id BIGINT generated by default as identity, name VARCHAR(255), CREATED_DATE_TIME TIMESTAMP, PRIMARY KEY (id));

View File

@@ -1,6 +0,0 @@
create table IF NOT EXISTS USERS(USERNAME varchar(100),PASSWORD varchar(100), EMAIL varchar(100));
create table IF NOT EXISTS DUMMY(DUMMY_VALUE varchar(10));
create table IF NOT EXISTS Person(id integer identity primary key , name varchar(100), gender varchar(1), dateOfBirth date);
INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('a', 'secret', 'spring-integration@awesome.com');
INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('b', 's3cr3t', 'spring@rocks.com');
INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('foo', 'bar', 'foo@bar.de');

View File

@@ -3,17 +3,12 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa.xsd">
<int:channel id="createPersonRequestChannel"/>
<int:channel id="listPeopleRequestChannel"/>
<int:gateway id="personService"
@@ -33,15 +28,4 @@
<int-jpa:transactional transaction-manager="transactionManager" />
</int-jpa:updating-outbound-gateway>
<!-- Depending on the selected profile, users can use different JPA Providers -->
<beans profile="default, hibernate">
<import resource="classpath:/META-INF/spring/integration/hibernate-context.xml"/>
</beans>
<beans profile="openjpa">
<import resource="classpath:/META-INF/spring/integration/openjpa-context.xml"/>
</beans>
<beans profile="eclipselink">
<import resource="classpath:/META-INF/spring/integration/eclipselink-context.xml"/>
</beans>
</beans>

View File

@@ -15,44 +15,48 @@
*/
package org.springframework.integration.samples.jpa;
import java.util.Calendar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Calendar;
import java.util.List;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.integration.samples.jpa.domain.Person;
import org.springframework.integration.samples.jpa.service.PersonService;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author Amol Nayak
* @author Artem Bilan
*
*/
public class OutboundGatewayTest {
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {Main.class, HibernateJpaAutoConfiguration.class})
public class JpaTests {
private static final Logger LOGGER = Logger.getLogger(OutboundGatewayTest.class);
@Autowired
private PersonService personService;
@Test
public void insertPersonRecord() {
final ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:/META-INF/spring/integration/spring-integration-context.xml");
final PersonService service = context.getBean(PersonService.class);
LOGGER.info("Creating person Instance");
final Person person = new Person();
Person person = new Person();
Calendar createdDateTime = Calendar.getInstance();
createdDateTime.set(1980, 0, 1);
createdDateTime.set(1980, Calendar.JANUARY, 1);
person.setCreatedDateTime(createdDateTime.getTime());
person.setName("Name Of The Person");
final Person persistedPerson = service.createPerson(person);
Assert.assertNotNull("Expected a non null instance of Person, got null", persistedPerson);
LOGGER.info("\n\tGenerated person with id: " + persistedPerson.getId() + ", with name: " + persistedPerson.getName());
this.personService.createPerson(person);
List<Person> people = this.personService.findPeople();
assertNotNull(people);
assertEquals(2, people.size());
assertEquals(person.getName(), people.get(1).getName());
}
}