* BAEL-399: A Guide to Multitenancy in Hibernate 5

* Removed unused properties in profile 2

* Changes after code review

* BAEL-113

* Changes after code review

* Added main method in spring boot application

* Removed extra files
This commit is contained in:
Jose Carvajal 2018-02-03 05:00:27 +01:00 committed by KevinGilmore
parent 2781e27d1d
commit ff76cbc1fe
11 changed files with 388 additions and 0 deletions

View File

@ -246,6 +246,7 @@
<module>spring-zuul</module> <module>spring-zuul</module>
<module>spring-reactor</module> <module>spring-reactor</module>
<module>spring-vertx</module> <module>spring-vertx</module>
<module>spring-jinq</module>
<module>spring-rest-embedded-tomcat</module> <module>spring-rest-embedded-tomcat</module>

83
spring-jinq/pom.xml Normal file
View File

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-jinq</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-jinq</name>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<jinq.version>1.8.22</jinq.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.9.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jinq</groupId>
<artifactId>jinq-jpa</artifactId>
<version>${jinq.version}</version>
</dependency>
<!-- Database Access -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<useIncrementalCompilation>false</useIncrementalCompilation>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,12 @@
package com.baeldung.spring.jinq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class JinqApplication {
public static void main(String[] args) {
SpringApplication.run(JinqApplication.class, args);
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.spring.jinq.config;
import javax.persistence.EntityManagerFactory;
import org.jinq.jpa.JinqJPAStreamProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JinqProviderConfiguration {
@Bean
@Autowired
JinqJPAStreamProvider jinqProvider(EntityManagerFactory emf) {
return new JinqJPAStreamProvider(emf);
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.spring.jinq.entities;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity(name = "CAR")
public class Car {
private String model;
private String description;
private int year;
private String engine;
private Manufacturer manufacturer;
@Id
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
@OneToOne
@JoinColumn(name = "name")
public Manufacturer getManufacturer() {
return manufacturer;
}
public void setManufacturer(Manufacturer manufacturer) {
this.manufacturer = manufacturer;
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.spring.jinq.entities;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity(name = "MANUFACTURER")
public class Manufacturer {
private String name;
private String city;
private List<Car> cars;
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@OneToMany(mappedBy = "model")
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.jinq.repositories;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jinq.jpa.JPAJinqStream;
import org.jinq.jpa.JinqJPAStreamProvider;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class BaseJinqRepositoryImpl<T> {
@Autowired
private JinqJPAStreamProvider jinqDataProvider;
@PersistenceContext
private EntityManager entityManager;
protected abstract Class<T> entityType();
public JPAJinqStream<T> stream() {
return streamOf(entityType());
}
protected <U> JPAJinqStream<U> streamOf(Class<U> clazz) {
return jinqDataProvider.streamAll(entityManager, clazz);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.spring.jinq.repositories;
import java.util.List;
import java.util.Optional;
import org.jinq.tuples.Pair;
import org.jinq.tuples.Tuple3;
import com.baeldung.spring.jinq.entities.Car;
import com.baeldung.spring.jinq.entities.Manufacturer;
public interface CarRepository {
Optional<Car> findByModel(String model);
List<Car> findByModelAndDescription(String model, String desc);
List<Tuple3<String, Integer, String>> findWithModelYearAndEngine();
Optional<Manufacturer> findManufacturerByModel(String model);
List<Pair<Manufacturer, Car>> findCarsPerManufacturer();
long countCarsByModel(String model);
List<Car> findAll(int skip, int limit);
}

View File

@ -0,0 +1,72 @@
package com.baeldung.spring.jinq.repositories;
import java.util.List;
import java.util.Optional;
import org.jinq.orm.stream.JinqStream;
import org.jinq.tuples.Pair;
import org.jinq.tuples.Tuple3;
import org.springframework.stereotype.Repository;
import com.baeldung.spring.jinq.entities.Car;
import com.baeldung.spring.jinq.entities.Manufacturer;
@Repository
public class CarRepositoryImpl extends BaseJinqRepositoryImpl<Car> implements CarRepository {
@Override
public Optional<Car> findByModel(String model) {
return stream().where(c -> c.getModel()
.equals(model))
.findFirst();
}
@Override
public List<Car> findByModelAndDescription(String model, String desc) {
return stream().where(c -> c.getModel()
.equals(model)
&& c.getDescription()
.contains(desc))
.toList();
}
@Override
public List<Tuple3<String, Integer, String>> findWithModelYearAndEngine() {
return stream().select(c -> new Tuple3<>(c.getModel(), c.getYear(), c.getEngine()))
.toList();
}
@Override
public Optional<Manufacturer> findManufacturerByModel(String model) {
return stream().where(c -> c.getModel()
.equals(model))
.select(c -> c.getManufacturer())
.findFirst();
}
@Override
public List<Pair<Manufacturer, Car>> findCarsPerManufacturer() {
return streamOf(Manufacturer.class).join(m -> JinqStream.from(m.getCars()))
.toList();
}
@Override
public long countCarsByModel(String model) {
return stream().where(c -> c.getModel()
.equals(model))
.count();
}
@Override
public List<Car> findAll(int skip, int limit) {
return stream().skip(skip)
.limit(limit)
.toList();
}
@Override
protected Class<Car> entityType() {
return Car.class;
}
}

View File

@ -0,0 +1,7 @@
spring.datasource.url=jdbc:h2:~/jinq
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

View File

@ -0,0 +1,42 @@
package com.baeldung.spring.jinq.repositories;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.jinq.JinqApplication;
@ContextConfiguration(classes = JinqApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class CarRepositoryIntegrationTest {
@Autowired
private CarRepository repository;
@Test
public void givenACar_whenFilter_thenShouldBeFound() {
assertThat(repository.findByModel("model1")
.isPresent()).isFalse();
}
@Test
public void givenACar_whenMultipleFilters_thenShouldBeFound() {
assertThat(repository.findByModelAndDescription("model1", "desc")
.isEmpty()).isTrue();
}
@Test
public void whenUseASelectClause() {
assertThat(repository.findWithModelYearAndEngine()
.isEmpty()).isTrue();
}
@Test
public void whenUsingOneToOneRelationship() {
assertThat(repository.findManufacturerByModel("model1")).isNotNull();
}
}