#50 - Simplified EclipseLink example.
Switched to a much more simplified domain model as the primary focus of the sample is on the infrastructure setup, not a sophisticated domain model. Added dedicated build profiles for static (compile-time) weaving and load-time weaving. Simplified application configuration to use more of Spring Boot's auto-configuration features. Ported Markdown README to Asciidoc. Original pull request: #68.
This commit is contained in:
20
jpa/eclipselink/README.adoc
Normal file
20
jpa/eclipselink/README.adoc
Normal file
@@ -0,0 +1,20 @@
|
||||
# Spring Data JPA - EclipseLink
|
||||
|
||||
This project contains an example of how to use Spring Data JPA in combination with Eclipselink. EclipseLink requires the domain types to be instrumented to implement lazy-loading. This can be achieved either through static weaving at compile time or dynamically at class loading time (load-time weaving). The build is set up to allow both modes of execution by selecting a certain build profile.
|
||||
|
||||
## Static weaving
|
||||
|
||||
To run the project with static weaving enable the corresponding build profile on the command line:
|
||||
|
||||
[source, bash]
|
||||
----
|
||||
$ mvn clean test -Pstatic-weaving
|
||||
----
|
||||
|
||||
Note, that you need to make sure the weaving process is run by your IDE, before executing test cases either by making sure the declared EclipseLink plugin is executed during compile or running `mvn clean test-compile` before running the tests.
|
||||
|
||||
## Load-time weaving
|
||||
|
||||
By default, the projects builds and runs with load-time weaving enabled. This is achieved by configuring the Spring instrumentation agent for both the Surefire and Spring Boot Maven plugin. The build profile for load-time weaving is enabled by default.
|
||||
|
||||
For execution within the IDE, make sure you configure the Spring instrumentation agent to be applied.
|
||||
@@ -1,7 +0,0 @@
|
||||
# Spring Data JPA - Eclipselink + Static Weaving Example
|
||||
|
||||
This project contains an example of using Eclipselink with static weaving with Spring Data. Static weaving is
|
||||
accomplished with a maven plugin that requires a persistence.xml, so a persistence.xml exists in this project.
|
||||
The persistence.xml is not actually used by the Spring Boot example and is only used at build time for the maven
|
||||
plugin. To use the persistence.xml file, please see instructions on how to [use a traditional persistence.xm](http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-use-traditional-persistence-xml).
|
||||
|
||||
@@ -2,44 +2,35 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-jpa-examples</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>com.ethlo.eclipselink.tools</id>
|
||||
<url>http://ethlo.com/maven</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
<artifactId>spring-data-jpa-eclipselink</artifactId>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>com.ethlo.eclipselink.tools</id>
|
||||
<url>http://ethlo.com/maven</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>example.springdata.jpa</groupId>
|
||||
<artifactId>eclipselink</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Spring Data JPA - Spring Data JPA w/ Eclipse Link and Static Weaving</name>
|
||||
<description>Example Spring Data JPA Project using EclipseLink and Static Weaving</description>
|
||||
<name>Spring Data JPA - EclipseLink setup example</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<start-class>example.springdata.jpa.eclipselink.SpringDataJpaEclipseLinkApplication</start-class>
|
||||
<java.version>1.8</java.version>
|
||||
<eclipselink.version>2.6.0</eclipselink.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<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>
|
||||
@@ -47,35 +38,121 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<!-- Static weaver for EclipseLink -->
|
||||
<plugin>
|
||||
<groupId>com.ethlo.persistence.tools</groupId>
|
||||
<artifactId>eclipselink-maven-plugin</artifactId>
|
||||
<version>1.1-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>weave</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<profiles>
|
||||
<profile>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>org.eclipse.persistence.jpa</artifactId>
|
||||
<version>${eclipselink.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<!--
|
||||
|
||||
Sets up the build to run the EclipseLink Maven plugin at compile time and instrument
|
||||
domain types. This will prevent the need for load-time weaving when running the app.
|
||||
|
||||
-->
|
||||
|
||||
<id>static-weaving</id>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
<!-- Static weaver for EclipseLink -->
|
||||
<plugin>
|
||||
<groupId>com.ethlo.persistence.tools</groupId>
|
||||
<artifactId>eclipselink-maven-plugin</artifactId>
|
||||
<version>1.1-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>weave</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.persistence</groupId>
|
||||
<artifactId>org.eclipse.persistence.jpa</artifactId>
|
||||
<version>${eclipselink.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>com.ethlo.eclipselink.tools</id>
|
||||
<url>http://ethlo.com/maven</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>com.ethlo.eclipselink.tools</id>
|
||||
<url>http://ethlo.com/maven</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
|
||||
<!--
|
||||
|
||||
Sets up the build to use load-time weaving by configuring the Surefire and Boot
|
||||
plugins to run with Spring's instrumentation agent. They will automatically pick
|
||||
up the EclipseLink weaver and instrument domain types at load time.
|
||||
|
||||
-->
|
||||
|
||||
<id>load-time-weaving</id>
|
||||
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-instrument</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<configuration>
|
||||
<argLine>-javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
||||
<!--
|
||||
|
||||
Not strictly required by the project but needed if you actually want to
|
||||
run the application.
|
||||
|
||||
-->
|
||||
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-instrument</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<configuration>
|
||||
<agent>${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar</agent>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
</profiles>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2015 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 example.eclipselink;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
|
||||
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
|
||||
|
||||
/**
|
||||
* Spring Boot application that uses EclipseLink as the JPA provider.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Jeremy Rickard
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application extends JpaBaseConfiguration {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration#createJpaVendorAdapter()
|
||||
*/
|
||||
@Override
|
||||
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
|
||||
return new EclipseLinkJpaVendorAdapter();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration#getVendorProperties()
|
||||
*/
|
||||
@Override
|
||||
protected Map<String, Object> getVendorProperties() {
|
||||
|
||||
// Turn off dynamic weaving to disable LTW lookup in static weaving mode
|
||||
return Collections.singletonMap("eclipselink.weaving", "false");
|
||||
}
|
||||
}
|
||||
@@ -13,28 +13,28 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.jpa.eclipselink.repositories;
|
||||
package example.eclipselink;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Repository to manage {@link example.springdata.jpa.eclipselink.domain.Race} instances.
|
||||
*
|
||||
* @author Jeremy Rickard
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@Data
|
||||
@Entity
|
||||
@RequiredArgsConstructor
|
||||
public class Customer {
|
||||
|
||||
import example.springdata.jpa.eclipselink.domain.Race;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
private @Id @GeneratedValue Long id;
|
||||
private final String firstname, lastname;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public interface RaceRepository extends CrudRepository<Race, String> {
|
||||
|
||||
List<Race> findByName(String name);
|
||||
|
||||
List<Race> findByDate(Date date);
|
||||
|
||||
List<Race> findByDateBefore(Date date);
|
||||
|
||||
List<Race> findByDistanceBetween(double start, double end);
|
||||
Customer() {
|
||||
this.firstname = null;
|
||||
this.lastname = null;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
package example.springdata.jpa.eclipselink.domain;
|
||||
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
@@ -15,13 +13,11 @@ package example.springdata.jpa.eclipselink.domain;
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.eclipselink;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
* @author Jeremy Rickard
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
|
||||
public enum Gender {
|
||||
FEMALE,
|
||||
MALE
|
||||
}
|
||||
public interface CustomerRepository extends CrudRepository<Customer, Long> {}
|
||||
@@ -1,94 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 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 example.springdata.jpa.eclipselink;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.Database;
|
||||
import org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect;
|
||||
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Spring Boot application with Spring Java Config annotations that use EclipseLink as the JPA Provider
|
||||
* and turn on Static Weaving of the entity classes by disabling dynamic weaving and utilizing a maven
|
||||
* build that includes a static weaving plugin.
|
||||
*
|
||||
* @author Jeremy Rickard
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
@Configuration
|
||||
@EnableJpaRepositories("example.springdata.jpa.eclipselink.repositories")
|
||||
public class SpringDataJpaEclipseLinkApplication {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).build();
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
LocalContainerEntityManagerFactoryBean lemfb = new LocalContainerEntityManagerFactoryBean();
|
||||
lemfb.setDataSource(dataSource());
|
||||
lemfb.setJpaVendorAdapter(jpaVendorAdapter());
|
||||
lemfb.setJpaDialect(eclipseLinkJpaDialect());
|
||||
lemfb.setJpaPropertyMap(jpaProperties());
|
||||
lemfb.setPackagesToScan("example.springdata.jpa.eclipselink.domain");
|
||||
return lemfb;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EclipseLinkJpaDialect eclipseLinkJpaDialect() {
|
||||
return new EclipseLinkJpaDialect();
|
||||
}
|
||||
|
||||
/*
|
||||
* Set this property to disable LoadTimeWeaver (i.e. Dynamic Weaving) for EclipseLink.
|
||||
* Otherwise, you'll get: Cannot apply class transformer without LoadTimeWeaver specified
|
||||
*/
|
||||
@Bean
|
||||
public Map<String, String> jpaProperties() {
|
||||
Map<String, String> props = new HashMap<>();
|
||||
props.put("eclipselink.weaving", "false");
|
||||
return props;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JpaVendorAdapter jpaVendorAdapter() {
|
||||
EclipseLinkJpaVendorAdapter jpaVendorAdapter = new EclipseLinkJpaVendorAdapter();
|
||||
jpaVendorAdapter.setDatabase(Database.HSQL);
|
||||
jpaVendorAdapter.setGenerateDdl(true);
|
||||
return jpaVendorAdapter;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDataJpaEclipseLinkApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 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 example.springdata.jpa.eclipselink.domain;
|
||||
|
||||
/**
|
||||
* Entity showing use of {@link org.eclipse.persistence.annotations.UuidGenerator} from
|
||||
* the EclipseLink JPA Provider. Does not extend the AbstractPersistable because of the UUID generator
|
||||
*
|
||||
* @author Jeremy Rickard
|
||||
*/
|
||||
|
||||
import org.eclipse.persistence.annotations.UuidGenerator;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Participant {
|
||||
@Id
|
||||
@UuidGenerator(name = "UUID")
|
||||
@GeneratedValue(generator = "UUID")
|
||||
private String uuid;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String emailAddress;
|
||||
|
||||
private String phoneNumber;
|
||||
|
||||
private int age;
|
||||
|
||||
@Enumerated
|
||||
private Gender gender;
|
||||
|
||||
@ManyToMany(mappedBy = "registrations")
|
||||
private List<Race> raceRegistrations;
|
||||
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getEmailAddress() {
|
||||
return emailAddress;
|
||||
}
|
||||
|
||||
public void setEmailAddress(String emailAddress) {
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
|
||||
public String getPhoneNumber() {
|
||||
return phoneNumber;
|
||||
}
|
||||
|
||||
public void setPhoneNumber(String phoneNumber) {
|
||||
this.phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public List<Race> getRaceRegistrations() {
|
||||
return raceRegistrations;
|
||||
}
|
||||
|
||||
public void setRaceRegistrations(List<Race> raceRegistrations) {
|
||||
this.raceRegistrations = raceRegistrations;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (null == obj) {
|
||||
return false;
|
||||
} else if (this == obj) {
|
||||
return true;
|
||||
} else if (!this.getClass().equals(obj.getClass())) {
|
||||
return false;
|
||||
} else {
|
||||
Participant that = (Participant) obj;
|
||||
return null == this.getUuid() ? false : this.getUuid().equals(that.getUuid());
|
||||
}
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
byte hashCode = 17;
|
||||
int hashCode1 = hashCode + (null == this.getUuid() ? 0 : this.getUuid().hashCode() * 31);
|
||||
return hashCode1;
|
||||
}
|
||||
}
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 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 example.springdata.jpa.eclipselink.domain;
|
||||
|
||||
|
||||
/**
|
||||
* Entity showing use of {@link org.eclipse.persistence.annotations.UuidGenerator} from
|
||||
* the EclipseLink JPA Provider. Does not extend the AbstractPersistable because of the UUID generator
|
||||
*
|
||||
* @author Jeremy Rickard
|
||||
*/
|
||||
|
||||
import org.eclipse.persistence.annotations.UuidGenerator;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
public class Race {
|
||||
@Id
|
||||
@UuidGenerator(name = "UUID")
|
||||
@GeneratedValue(generator = "UUID")
|
||||
private String uuid;
|
||||
|
||||
private double distance;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date date;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false, length = 5000)
|
||||
private String description;
|
||||
|
||||
@ManyToMany
|
||||
@JoinTable(name = "RACE_REG",
|
||||
joinColumns = {@JoinColumn(name = "RACE_ID", referencedColumnName = "uuid")},
|
||||
inverseJoinColumns = {@JoinColumn(name = "PART_ID", referencedColumnName = "uuid", unique = true)})
|
||||
private List<Participant> registrations;
|
||||
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public double getDistance() {
|
||||
return distance;
|
||||
}
|
||||
|
||||
public void setDistance(double distance) {
|
||||
this.distance = distance;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public List<Participant> getRegistrations() {
|
||||
return registrations;
|
||||
}
|
||||
|
||||
public void setRegistrations(List<Participant> registrations) {
|
||||
this.registrations = registrations;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if (null == obj) {
|
||||
return false;
|
||||
} else if (this == obj) {
|
||||
return true;
|
||||
} else if (!this.getClass().equals(obj.getClass())) {
|
||||
return false;
|
||||
} else {
|
||||
Race that = (Race) obj;
|
||||
return null == this.getUuid() ? false : this.getUuid().equals(that.getUuid());
|
||||
}
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
byte hashCode = 17;
|
||||
int hashCode1 = hashCode + (null == this.getUuid() ? 0 : this.getUuid().hashCode() * 31);
|
||||
return hashCode1;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 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 example.springdata.jpa.eclipselink.repositories;
|
||||
|
||||
|
||||
import example.springdata.jpa.eclipselink.domain.Participant;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Repository to manage {@link example.springdata.jpa.eclipselink.domain.Participant} instances.
|
||||
*
|
||||
* @author Jeremy Rickard
|
||||
*/
|
||||
public interface ParticipantRepository extends CrudRepository<Participant, String> {
|
||||
|
||||
@Query("SELECT p from Participant p")
|
||||
List<Participant> findAll();
|
||||
|
||||
List<Participant> findByLastName(String lastName);
|
||||
|
||||
List<Participant> findByAgeLessThan(int age);
|
||||
|
||||
List<Participant> findByAgeBetween(int age1, int age2);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.jpa.generate-ddl=true
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2015 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 example.eclipselink;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* Integration test for {@link CustomerRepository}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
public class CustomerRepositoryIntegrationTests {
|
||||
|
||||
@Autowired CustomerRepository customers;
|
||||
|
||||
@Test
|
||||
public void createsCustomer() {
|
||||
|
||||
Customer dave = customers.save(new Customer("Dave", "Matthews"));
|
||||
|
||||
assertThat(customers.findOne(dave.getId()), is(dave));
|
||||
}
|
||||
}
|
||||
@@ -1,264 +0,0 @@
|
||||
package example.springdata.jpa.eclipselink;
|
||||
|
||||
import example.springdata.jpa.eclipselink.domain.Gender;
|
||||
import example.springdata.jpa.eclipselink.domain.Participant;
|
||||
import example.springdata.jpa.eclipselink.domain.Race;
|
||||
import example.springdata.jpa.eclipselink.repositories.ParticipantRepository;
|
||||
import example.springdata.jpa.eclipselink.repositories.RaceRepository;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Test for Spring Data JPA EclipseLink w/ Static Weaving example
|
||||
*
|
||||
* @author Jeremy Rickard
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = SpringDataJpaEclipseLinkApplication.class)
|
||||
public class SpringDataJpaEclipseLinkApplicationTests {
|
||||
|
||||
@Autowired
|
||||
RaceRepository raceRepository;
|
||||
|
||||
@Autowired
|
||||
ParticipantRepository participantRepository;
|
||||
|
||||
Race pikesPeakAscent;
|
||||
|
||||
Race pikesPeakMarathon;
|
||||
|
||||
Race summerRoundUp;
|
||||
|
||||
Race gardenOfTheGods10Mile;
|
||||
|
||||
Participant one;
|
||||
|
||||
Participant two;
|
||||
|
||||
Participant three;
|
||||
|
||||
Date ascentStartDate;
|
||||
|
||||
SimpleDateFormat sdf;
|
||||
|
||||
double ascentDistanceInKM = 21.4364621;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
sdf = new SimpleDateFormat("MM/dd/yyyy HH:MM z");
|
||||
|
||||
|
||||
ascentStartDate = sdf.parse("08/15/2015 07:00 MDT");
|
||||
|
||||
pikesPeakAscent = new Race();
|
||||
pikesPeakAscent.setName("Pikes Peak Ascent");
|
||||
pikesPeakAscent.setDistance(ascentDistanceInKM);
|
||||
pikesPeakAscent.setDate(ascentStartDate);
|
||||
pikesPeakAscent.setDescription("Started in 1956 as a challenge between smokers and non-smokers, " +
|
||||
"the Pikes Peak Marathon is #3 on the list of longest running US marathons behind Boston and Yonkers.");
|
||||
|
||||
pikesPeakMarathon = new Race();
|
||||
pikesPeakMarathon.setName("Pikes Peak Marathon");
|
||||
pikesPeakMarathon.setDistance(ascentDistanceInKM * 2);
|
||||
pikesPeakMarathon.setDate(ascentStartDate);
|
||||
pikesPeakMarathon.setDescription("Started in 1956 as a challenge between smokers and non-smokers, " +
|
||||
"the Pikes Peak Marathon is #3 on the list of longest running US marathons behind Boston and Yonkers.");
|
||||
|
||||
|
||||
summerRoundUp = new Race();
|
||||
summerRoundUp.setName("Summer Round Up");
|
||||
summerRoundUp.setDescription("2nd Leg of the Triple Crown of Running");
|
||||
summerRoundUp.setDate(sdf.parse("07/12/2015 07:00 MDT"));
|
||||
summerRoundUp.setDistance(12.0);
|
||||
|
||||
gardenOfTheGods10Mile = new Race();
|
||||
gardenOfTheGods10Mile.setName("Garden of the Gods 10 Miler");
|
||||
gardenOfTheGods10Mile.setDescription("1st Leg of the Triple Crown of Running");
|
||||
gardenOfTheGods10Mile.setDate(sdf.parse("06/15/2015 07:00 MDT"));
|
||||
gardenOfTheGods10Mile.setDistance(16.0934);
|
||||
|
||||
|
||||
one = new Participant();
|
||||
one.setAge(25);
|
||||
one.setEmailAddress("john.doe@gmail.com");
|
||||
one.setFirstName("John");
|
||||
one.setLastName("Doe");
|
||||
one.setPhoneNumber("1-123-234-5679");
|
||||
one.setGender(Gender.MALE);
|
||||
|
||||
two = new Participant();
|
||||
two.setAge(21);
|
||||
two.setEmailAddress("jane.doe@gmail.com");
|
||||
two.setFirstName("Jane");
|
||||
two.setLastName("Doe");
|
||||
two.setPhoneNumber("1-123-234-5678");
|
||||
two.setGender(Gender.FEMALE);
|
||||
|
||||
three = new Participant();
|
||||
three.setAge(62);
|
||||
three.setEmailAddress("sam.smith@gmail.com");
|
||||
three.setFirstName("Samantha");
|
||||
three.setLastName("Smith");
|
||||
three.setPhoneNumber("1-234-911-5678");
|
||||
three.setGender(Gender.FEMALE);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsertRace() {
|
||||
|
||||
pikesPeakAscent = raceRepository.save(pikesPeakAscent);
|
||||
pikesPeakMarathon = raceRepository.save(pikesPeakMarathon);
|
||||
summerRoundUp = raceRepository.save(summerRoundUp);
|
||||
gardenOfTheGods10Mile = raceRepository.save(gardenOfTheGods10Mile);
|
||||
|
||||
assertNotNull(pikesPeakAscent.getUuid());
|
||||
assertNotNull(pikesPeakMarathon.getUuid());
|
||||
assertNotNull(summerRoundUp.getUuid());
|
||||
assertNotNull(gardenOfTheGods10Mile.getUuid());
|
||||
|
||||
assertNotEquals(pikesPeakAscent.getUuid(), pikesPeakMarathon.getUuid());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindRacesByDates() {
|
||||
pikesPeakAscent = raceRepository.save(pikesPeakAscent);
|
||||
pikesPeakMarathon = raceRepository.save(pikesPeakMarathon);
|
||||
summerRoundUp = raceRepository.save(summerRoundUp);
|
||||
gardenOfTheGods10Mile = raceRepository.save(gardenOfTheGods10Mile);
|
||||
|
||||
List<Race> races = raceRepository.findByDate(ascentStartDate);
|
||||
assertThat(races.contains(pikesPeakAscent), is(true));
|
||||
assertThat(races.contains(pikesPeakMarathon), is(true));
|
||||
|
||||
races = raceRepository.findByDateBefore(ascentStartDate);
|
||||
assertThat(races.contains(pikesPeakAscent), is(false));
|
||||
assertThat(races.contains(pikesPeakMarathon), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindRacesByDistance() {
|
||||
pikesPeakAscent = raceRepository.save(pikesPeakAscent);
|
||||
pikesPeakMarathon = raceRepository.save(pikesPeakMarathon);
|
||||
summerRoundUp = raceRepository.save(summerRoundUp);
|
||||
gardenOfTheGods10Mile = raceRepository.save(gardenOfTheGods10Mile);
|
||||
|
||||
List<Race> races = raceRepository.findByDistanceBetween(20, 100);
|
||||
assertThat(races.contains(pikesPeakAscent), is(true));
|
||||
assertThat(races.contains(pikesPeakMarathon), is(true));
|
||||
|
||||
races = raceRepository.findByDistanceBetween(10, 19);
|
||||
assertThat(races.contains(pikesPeakAscent), is(false));
|
||||
assertThat(races.contains(pikesPeakMarathon), is(false));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRaceByName() {
|
||||
pikesPeakAscent = raceRepository.save(pikesPeakAscent);
|
||||
pikesPeakMarathon = raceRepository.save(pikesPeakMarathon);
|
||||
summerRoundUp = raceRepository.save(summerRoundUp);
|
||||
gardenOfTheGods10Mile = raceRepository.save(gardenOfTheGods10Mile);
|
||||
|
||||
List<Race> races = raceRepository.findByName("Summer Round Up");
|
||||
assertThat(races.contains(pikesPeakAscent), is(false));
|
||||
assertThat(races.contains(pikesPeakMarathon), is(false));
|
||||
assertThat(races.contains(summerRoundUp), is(true));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInsertParticipant() {
|
||||
|
||||
one = participantRepository.save(one);
|
||||
two = participantRepository.save(two);
|
||||
three = participantRepository.save(three);
|
||||
|
||||
assertNotNull(one.getUuid());
|
||||
assertNotNull(two.getUuid());
|
||||
assertNotNull(three.getUuid());
|
||||
|
||||
assertNotEquals(one.getUuid(), two.getUuid());
|
||||
assertNotEquals(two.getUuid(), three.getUuid());
|
||||
|
||||
assertThat(participantRepository.findAll().size(), is(3));
|
||||
|
||||
List<Participant> twentySomethings = participantRepository.findByAgeLessThan(40);
|
||||
|
||||
for (Participant participant : twentySomethings) {
|
||||
System.err.println(participant.getUuid());
|
||||
}
|
||||
|
||||
System.err.println(one.getUuid());
|
||||
System.err.println(two.getUuid());
|
||||
System.err.println(three.getUuid());
|
||||
|
||||
assertThat(twentySomethings.contains(one), is(true));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByAgeLessThan() {
|
||||
|
||||
one = participantRepository.save(one);
|
||||
two = participantRepository.save(two);
|
||||
three = participantRepository.save(three);
|
||||
|
||||
List<Participant> twentySomethings = participantRepository.findByAgeLessThan(40);
|
||||
assertThat(twentySomethings.contains(one), is(true));
|
||||
assertThat(twentySomethings.contains(three), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAgeBetween() {
|
||||
one = participantRepository.save(one);
|
||||
two = participantRepository.save(two);
|
||||
three = participantRepository.save(three);
|
||||
|
||||
List<Participant> twentySomethings = participantRepository.findByAgeBetween(40, 70);
|
||||
assertThat(twentySomethings.contains(one), is(false));
|
||||
assertThat(twentySomethings.contains(three), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByLastName() {
|
||||
|
||||
one = participantRepository.save(one);
|
||||
two = participantRepository.save(two);
|
||||
three = participantRepository.save(three);
|
||||
|
||||
List<Participant> theDoes = participantRepository.findByLastName("Doe");
|
||||
|
||||
assertThat(theDoes.contains(one), is(true));
|
||||
assertThat(theDoes.contains(three), is(false));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
participantRepository.deleteAll();
|
||||
raceRepository.deleteAll();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user