Skip to content

Commit a6d2b42

Browse files
committed
Add Hibernate examples
1 parent 90482dd commit a6d2b42

File tree

31 files changed

+1520
-5
lines changed

31 files changed

+1520
-5
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ Examples showing how to use some JVM annotations with **JUnit 5**.
4646
**[jvm-testng](jvm-testng)**<br>
4747
Examples showing how to use some JVM annotations with **TestNG**.
4848

49+
## Hibernate without Spring
50+
51+
The documentation of SQL annotations is [here](https://github.com/quick-perf/doc/wiki/SQL-annotations).
52+
53+
**[hibernate-junit4](hibernate-junit4)**
54+
Examples showing how to use some SQL annotations with *Hibernate* and **JUnit 4**.
55+
56+
**[hibernate-junit5](hibernate-junit5)**
57+
Examples showing how to use some SQL annotations with *Hibernate* and **JUnit 5**.
58+
59+
**[hibernate-testng](hibernate-testng)**
60+
Examples showing how to use some SQL annotations with *Hibernate* and **TestNG**.
61+
4962
## Spring Boot
5063
**[springboot-junit4](springboot-junit4)**<br>
5164
This Spring Boot project illustrates how to use QuickPerf with **JUnit 4**.

hibernate-junit4/pom.xml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
~ the License. You may obtain a copy of the License at
5+
~
6+
~ http://www.apache.org/licenses/LICENSE-2.0
7+
~
8+
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
~ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
~ specific language governing permissions and limitations under the License.
11+
~
12+
~ Copyright 2020-2020 the original author or authors.
13+
-->
14+
<project xmlns="http://maven.apache.org/POM/4.0.0"
15+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
16+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
17+
18+
<modelVersion>4.0.0</modelVersion>
19+
20+
<groupId>org.quickperf</groupId>
21+
<artifactId>hibernate-junit4</artifactId>
22+
<version>1.0-SNAPSHOT</version>
23+
24+
<properties>
25+
<maven.compiler.source>1.8</maven.compiler.source>
26+
<maven.compiler.target>1.8</maven.compiler.target>
27+
<maven.test.skip>true</maven.test.skip>
28+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29+
</properties>
30+
31+
<dependencies>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<version>4.12</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.quickperf</groupId>
40+
<artifactId>quick-perf-junit4</artifactId>
41+
<version>1.0.0-RC6</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.quickperf</groupId>
46+
<artifactId>quick-perf-sql-annotations</artifactId>
47+
<version>1.0.0-RC6</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.assertj</groupId>
52+
<artifactId>assertj-core</artifactId>
53+
<version>3.15.0</version>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>commons-dbcp</groupId>
58+
<artifactId>commons-dbcp</artifactId>
59+
<version>1.4</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.h2database</groupId>
63+
<artifactId>h2</artifactId>
64+
<version>1.4.199</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.apache.logging.log4j</groupId>
68+
<artifactId>log4j-core</artifactId>
69+
<version>2.13.0</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>com.lmax</groupId>
73+
<artifactId>disruptor</artifactId>
74+
<version>3.4.2</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.hibernate</groupId>
78+
<artifactId>hibernate-entitymanager</artifactId>
79+
<version>5.4.0.Final</version>
80+
</dependency>
81+
</dependencies>
82+
83+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2020-2020 the original author or authors.
12+
*/
13+
14+
package football;
15+
16+
import football.entity.Player;
17+
18+
import javax.persistence.EntityManager;
19+
import javax.persistence.Query;
20+
import javax.persistence.TypedQuery;
21+
import java.util.List;
22+
23+
public class PlayerRepository {
24+
25+
private final EntityManager entityManager;
26+
27+
public PlayerRepository(EntityManager entityManager) {
28+
this.entityManager = entityManager;
29+
}
30+
31+
public List<Player> findAll() {
32+
TypedQuery<Player> fromPlayer
33+
= entityManager.createQuery("FROM Player", Player.class);
34+
return fromPlayer.getResultList();
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2020-2020 the original author or authors.
12+
*/
13+
14+
package football.entity;
15+
16+
import javax.persistence.*;
17+
import java.io.Serializable;
18+
19+
@Entity
20+
public class Player implements Serializable {
21+
22+
@Id
23+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
24+
private Long id;
25+
26+
private String firstName;
27+
28+
private String lastName;
29+
30+
@ManyToOne(targetEntity = Team.class)
31+
@JoinColumn(name = "team_id")
32+
private Team team;
33+
34+
public Long getId() {
35+
return id;
36+
}
37+
38+
public void setId(Long id) {
39+
this.id = id;
40+
}
41+
42+
public String getFirstName() {
43+
return firstName;
44+
}
45+
46+
public void setFirstName(String firstName) {
47+
this.firstName = firstName;
48+
}
49+
50+
public String getLastName() {
51+
return lastName;
52+
}
53+
54+
public void setLastName(String lastName) {
55+
this.lastName = lastName;
56+
}
57+
58+
public Team getTeam() {
59+
return team;
60+
}
61+
62+
public void setTeam(Team team) {
63+
this.team = team;
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2020-2020 the original author or authors.
12+
*/
13+
14+
package football.entity;
15+
16+
import javax.persistence.Entity;
17+
import javax.persistence.GeneratedValue;
18+
import javax.persistence.GenerationType;
19+
import javax.persistence.Id;
20+
import java.io.Serializable;
21+
22+
@Entity
23+
public class Team implements Serializable {
24+
25+
@Id
26+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
27+
private Long id;
28+
29+
private String name;
30+
31+
public Long getId() {
32+
return id;
33+
}
34+
35+
public void setId(Long id) {
36+
this.id = id;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2020-2020 the original author or authors.
12+
*/
13+
14+
package org.quickperf.sql;
15+
16+
import football.PlayerRepository;
17+
import football.entity.Player;
18+
import net.ttddyy.dsproxy.support.ProxyDataSource;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
import org.quickperf.junit4.QuickPerfJUnitRunner;
22+
import org.quickperf.sql.annotation.ExpectSelect;
23+
import org.quickperf.sql.config.QuickPerfSqlDataSourceBuilder;
24+
25+
import javax.persistence.EntityManager;
26+
import javax.sql.DataSource;
27+
import java.util.List;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.quickperf.sql.config.HibernateEntityManagerBuilder.anHibernateEntityManager;
31+
import static org.quickperf.sql.config.TestDataSourceBuilder.aDataSource;
32+
33+
@RunWith(QuickPerfJUnitRunner.class)
34+
public class HibernateJUnit4Test {
35+
36+
@ExpectSelect(1)
37+
@Test
38+
public void should_find_all_players() {
39+
40+
PlayerRepository playerRepository = new PlayerRepository(entityManager);
41+
42+
List<Player> players = playerRepository.findAll();
43+
44+
assertThat(players).hasSize(2);
45+
46+
}
47+
48+
private EntityManager entityManager;
49+
50+
{
51+
DataSource dataSource = aDataSource().build();
52+
ProxyDataSource proxyDataSource = QuickPerfSqlDataSourceBuilder.aDataSourceBuilder()
53+
.buildProxy(dataSource);
54+
entityManager = anHibernateEntityManager(proxyDataSource);
55+
}
56+
57+
}

0 commit comments

Comments
 (0)