Skip to content

Commit ff76cbc

Browse files
SgitarioKevinGilmore
authored andcommitted
Bael 113 (eugenp#3484)
* 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
1 parent 2781e27 commit ff76cbc

11 files changed

Lines changed: 388 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
<module>spring-zuul</module>
247247
<module>spring-reactor</module>
248248
<module>spring-vertx</module>
249+
<module>spring-jinq</module>
249250

250251
<module>spring-rest-embedded-tomcat</module>
251252

‎spring-jinq/pom.xml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<parent>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>parent-modules</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<modelVersion>4.0.0</modelVersion>
11+
<artifactId>spring-jinq</artifactId>
12+
<version>0.1-SNAPSHOT</version>
13+
14+
<name>spring-jinq</name>
15+
16+
<packaging>jar</packaging>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<java.version>1.8</java.version>
21+
22+
<jinq.version>1.8.22</jinq.version>
23+
</properties>
24+
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-dependencies</artifactId>
30+
<version>1.5.9.RELEASE</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
</dependencies>
35+
</dependencyManagement>
36+
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.jinq</groupId>
40+
<artifactId>jinq-jpa</artifactId>
41+
<version>${jinq.version}</version>
42+
</dependency>
43+
44+
<!-- Database Access -->
45+
<dependency>
46+
<groupId>com.h2database</groupId>
47+
<artifactId>h2</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.hibernate</groupId>
52+
<artifactId>hibernate-entitymanager</artifactId>
53+
</dependency>
54+
55+
<!-- Spring -->
56+
<dependency>
57+
<groupId>org.springframework</groupId>
58+
<artifactId>spring-orm</artifactId>
59+
</dependency>
60+
61+
<!-- Testing -->
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-test</artifactId>
65+
</dependency>
66+
</dependencies>
67+
68+
<build>
69+
<plugins>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-compiler-plugin</artifactId>
73+
<configuration>
74+
<source>${java.version}</source>
75+
<target>${java.version}</target>
76+
<encoding>${project.build.sourceEncoding}</encoding>
77+
<useIncrementalCompilation>false</useIncrementalCompilation>
78+
</configuration>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
83+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.spring.jinq;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class JinqApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(JinqApplication.class, args);
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.spring.jinq.config;
2+
3+
import javax.persistence.EntityManagerFactory;
4+
5+
import org.jinq.jpa.JinqJPAStreamProvider;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
public class JinqProviderConfiguration {
12+
13+
@Bean
14+
@Autowired
15+
JinqJPAStreamProvider jinqProvider(EntityManagerFactory emf) {
16+
return new JinqJPAStreamProvider(emf);
17+
}
18+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.spring.jinq.entities;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.JoinColumn;
6+
import javax.persistence.OneToOne;
7+
8+
@Entity(name = "CAR")
9+
public class Car {
10+
private String model;
11+
private String description;
12+
private int year;
13+
private String engine;
14+
private Manufacturer manufacturer;
15+
16+
@Id
17+
public String getModel() {
18+
return model;
19+
}
20+
21+
public void setModel(String model) {
22+
this.model = model;
23+
}
24+
25+
public String getDescription() {
26+
return description;
27+
}
28+
29+
public void setDescription(String description) {
30+
this.description = description;
31+
}
32+
33+
public int getYear() {
34+
return year;
35+
}
36+
37+
public void setYear(int year) {
38+
this.year = year;
39+
}
40+
41+
public String getEngine() {
42+
return engine;
43+
}
44+
45+
public void setEngine(String engine) {
46+
this.engine = engine;
47+
}
48+
49+
@OneToOne
50+
@JoinColumn(name = "name")
51+
public Manufacturer getManufacturer() {
52+
return manufacturer;
53+
}
54+
55+
public void setManufacturer(Manufacturer manufacturer) {
56+
this.manufacturer = manufacturer;
57+
}
58+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.spring.jinq.entities;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.Id;
7+
import javax.persistence.OneToMany;
8+
9+
@Entity(name = "MANUFACTURER")
10+
public class Manufacturer {
11+
12+
private String name;
13+
private String city;
14+
private List<Car> cars;
15+
16+
@Id
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public String getCity() {
26+
return city;
27+
}
28+
29+
public void setCity(String city) {
30+
this.city = city;
31+
}
32+
33+
@OneToMany(mappedBy = "model")
34+
public List<Car> getCars() {
35+
return cars;
36+
}
37+
38+
public void setCars(List<Car> cars) {
39+
this.cars = cars;
40+
}
41+
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.spring.jinq.repositories;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.PersistenceContext;
5+
6+
import org.jinq.jpa.JPAJinqStream;
7+
import org.jinq.jpa.JinqJPAStreamProvider;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
10+
public abstract class BaseJinqRepositoryImpl<T> {
11+
@Autowired
12+
private JinqJPAStreamProvider jinqDataProvider;
13+
14+
@PersistenceContext
15+
private EntityManager entityManager;
16+
17+
protected abstract Class<T> entityType();
18+
19+
public JPAJinqStream<T> stream() {
20+
return streamOf(entityType());
21+
}
22+
23+
protected <U> JPAJinqStream<U> streamOf(Class<U> clazz) {
24+
return jinqDataProvider.streamAll(entityManager, clazz);
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.spring.jinq.repositories;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
6+
import org.jinq.tuples.Pair;
7+
import org.jinq.tuples.Tuple3;
8+
9+
import com.baeldung.spring.jinq.entities.Car;
10+
import com.baeldung.spring.jinq.entities.Manufacturer;
11+
12+
public interface CarRepository {
13+
14+
Optional<Car> findByModel(String model);
15+
16+
List<Car> findByModelAndDescription(String model, String desc);
17+
18+
List<Tuple3<String, Integer, String>> findWithModelYearAndEngine();
19+
20+
Optional<Manufacturer> findManufacturerByModel(String model);
21+
22+
List<Pair<Manufacturer, Car>> findCarsPerManufacturer();
23+
24+
long countCarsByModel(String model);
25+
26+
List<Car> findAll(int skip, int limit);
27+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.baeldung.spring.jinq.repositories;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
6+
import org.jinq.orm.stream.JinqStream;
7+
import org.jinq.tuples.Pair;
8+
import org.jinq.tuples.Tuple3;
9+
import org.springframework.stereotype.Repository;
10+
11+
import com.baeldung.spring.jinq.entities.Car;
12+
import com.baeldung.spring.jinq.entities.Manufacturer;
13+
14+
@Repository
15+
public class CarRepositoryImpl extends BaseJinqRepositoryImpl<Car> implements CarRepository {
16+
17+
@Override
18+
public Optional<Car> findByModel(String model) {
19+
return stream().where(c -> c.getModel()
20+
.equals(model))
21+
.findFirst();
22+
}
23+
24+
@Override
25+
public List<Car> findByModelAndDescription(String model, String desc) {
26+
return stream().where(c -> c.getModel()
27+
.equals(model)
28+
&& c.getDescription()
29+
.contains(desc))
30+
.toList();
31+
}
32+
33+
@Override
34+
public List<Tuple3<String, Integer, String>> findWithModelYearAndEngine() {
35+
return stream().select(c -> new Tuple3<>(c.getModel(), c.getYear(), c.getEngine()))
36+
.toList();
37+
}
38+
39+
@Override
40+
public Optional<Manufacturer> findManufacturerByModel(String model) {
41+
return stream().where(c -> c.getModel()
42+
.equals(model))
43+
.select(c -> c.getManufacturer())
44+
.findFirst();
45+
}
46+
47+
@Override
48+
public List<Pair<Manufacturer, Car>> findCarsPerManufacturer() {
49+
return streamOf(Manufacturer.class).join(m -> JinqStream.from(m.getCars()))
50+
.toList();
51+
}
52+
53+
@Override
54+
public long countCarsByModel(String model) {
55+
return stream().where(c -> c.getModel()
56+
.equals(model))
57+
.count();
58+
}
59+
60+
@Override
61+
public List<Car> findAll(int skip, int limit) {
62+
return stream().skip(skip)
63+
.limit(limit)
64+
.toList();
65+
}
66+
67+
@Override
68+
protected Class<Car> entityType() {
69+
return Car.class;
70+
}
71+
72+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring.datasource.url=jdbc:h2:~/jinq
2+
spring.datasource.username=sa
3+
spring.datasource.password=
4+
5+
spring.jpa.hibernate.ddl-auto=create-drop
6+
spring.jpa.show-sql=true
7+
spring.jpa.properties.hibernate.format_sql=true

0 commit comments

Comments
 (0)