sample codes for article BAEL-3581 (#8942)
This commit is contained in:
parent
277e596071
commit
c5ba081f7c
|
@ -81,11 +81,17 @@
|
|||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${openjdk-jmh.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${postgresql.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<hibernate.version>5.3.7.Final</hibernate.version>
|
||||
<mysql.version>6.0.6</mysql.version>
|
||||
<mysql.version>8.0.13</mysql.version>
|
||||
<postgresql.version>42.2.11</postgresql.version>
|
||||
<mariaDB4j.version>2.2.3</mariaDB4j.version>
|
||||
<assertj-core.version>3.8.0</assertj-core.version>
|
||||
<openjdk-jmh.version>1.21</openjdk-jmh.version>
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.hibernate.entitymanager.getreference;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Game {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public Game() {
|
||||
}
|
||||
|
||||
public Game(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.hibernate.entitymanager.getreference;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Player {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
private Game game;
|
||||
|
||||
public Player() {
|
||||
}
|
||||
|
||||
public Player(Long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
public void setGame(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
create table Game (id bigint not null, name varchar(255), primary key (id));
|
||||
create table Player (id bigint not null, name varchar(255), game_id bigint, primary key (id));
|
||||
alter table Player add constraint FKohr86afuapoujklti79wo27aa foreign key (game_id) references Game(id);
|
|
@ -0,0 +1,5 @@
|
|||
insert into Game (id, name) values (1, 'Game 1');
|
||||
insert into Game (id, name) values (2, 'Game 2');
|
||||
insert into Player (game_id, name, id) values (null, 'Player 1', 1);
|
||||
insert into Player (game_id, name, id) values (null, 'Player 2', 2);
|
||||
insert into Player (game_id, name, id) values (null, 'Player 3', 3);
|
|
@ -0,0 +1,2 @@
|
|||
drop table if exists Player;
|
||||
drop table if exists Game;
|
|
@ -5,7 +5,7 @@
|
|||
version="2.0">
|
||||
<persistence-unit name="com.baeldung.movie_catalog">
|
||||
<description>Hibernate EntityManager Demo</description>
|
||||
<class>com.baeldung.hibernate.pojo.Movie</class>
|
||||
<class>com.baeldung.hibernate.pojo.Movie</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
|
||||
|
@ -16,4 +16,88 @@
|
|||
<property name="javax.persistence.jdbc.password" value="root"/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
<!-- Persistence unit for H2 -->
|
||||
<persistence-unit name="com.baeldung.hibernate.entitymanager.game_player_h2">
|
||||
<description>EntityManager getReference persistence unit</description>
|
||||
<class>com.baeldung.hibernate.entitymanager.getreference.Game</class>
|
||||
<class>com.baeldung.hibernate.entitymanager.getreference.Player</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<!-- even if we set exclude-unlisted-classes=true, strangely it's not enough to exclude defined hbm files in hibernate.cfg.xml.
|
||||
So, some entities out of the scope unwillingly managed by our persistence unit. In order to prevent this, we disable autodetection
|
||||
of the hbm files completely. When we set this property, hibernate ignores the exclude-unlisted-classes property. -->
|
||||
<property name="hibernate.archive.autodetection" value=""/>
|
||||
|
||||
<property name="hibernate.jpa.compliance.proxy" value="false"/>
|
||||
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
<property name="hibernate.generate_statistics" value="false"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1"/>
|
||||
<property name="javax.persistence.jdbc.user" value="sa"/>
|
||||
<property name="javax.persistence.jdbc.password" value=""/>
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
<!-- Persistence unit for MySQL -->
|
||||
<persistence-unit name="com.baeldung.hibernate.entitymanager.game_player_mysql">
|
||||
<description>EntityManager getReference persistence unit</description>
|
||||
<class>com.baeldung.hibernate.entitymanager.getreference.Game</class>
|
||||
<class>com.baeldung.hibernate.entitymanager.getreference.Player</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<!-- even if we set exclude-unlisted-classes=true, strangely it's not enough to exclude defined hbm files in hibernate.cfg.xml.
|
||||
So, some entities out of the scope unwillingly managed by our persistence unit. In order to prevent this, we disable autodetection
|
||||
of the hbm files completely. When we set this property, hibernate ignores the exclude-unlisted-classes property. -->
|
||||
<property name="hibernate.archive.autodetection" value=""/>
|
||||
|
||||
<property name="hibernate.jpa.compliance.proxy" value="false"/>
|
||||
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
<property name="hibernate.generate_statistics" value="false"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/baeldung"/>
|
||||
<property name="javax.persistence.jdbc.user" value="root"/>
|
||||
<property name="javax.persistence.jdbc.password" value="password"/>
|
||||
|
||||
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
|
||||
<property name="javax.persistence.schema-generation.create-script-source" value="/META-INF/create-db.sql"/>
|
||||
<property name="javax.persistence.schema-generation.drop-script-source" value="/META-INF/drop-db.sql"/>
|
||||
<property name="javax.persistence.sql-load-script-source" value="/META-INF/data.sql"/>
|
||||
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
<!-- Persistence unit for PostgreSQL -->
|
||||
<persistence-unit name="com.baeldung.hibernate.entitymanager.game_player_postgresql">
|
||||
<description>EntityManager getReference persistence unit</description>
|
||||
<class>com.baeldung.hibernate.entitymanager.getreference.Game</class>
|
||||
<class>com.baeldung.hibernate.entitymanager.getreference.Player</class>
|
||||
<exclude-unlisted-classes>true</exclude-unlisted-classes>
|
||||
<properties>
|
||||
<!-- even if we set exclude-unlisted-classes=true, strangely it's not enough to exclude defined hbm files in hibernate.cfg.xml.
|
||||
So, some entities out of the scope unwillingly managed by our persistence unit. In order to prevent this, we disable autodetection
|
||||
of the hbm files completely. When we set this property, hibernate ignores the exclude-unlisted-classes property. -->
|
||||
<property name="hibernate.archive.autodetection" value=""/>
|
||||
|
||||
<property name="hibernate.jpa.compliance.proxy" value="false"/>
|
||||
|
||||
<property name="hibernate.hbm2ddl.auto" value="update"/>
|
||||
<property name="hibernate.show_sql" value="true"/>
|
||||
<property name="hibernate.generate_statistics" value="false"/>
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
|
||||
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/>
|
||||
<property name="javax.persistence.jdbc.user" value="postgres"/>
|
||||
<property name="javax.persistence.jdbc.password" value=""/>
|
||||
|
||||
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
|
||||
<property name="javax.persistence.schema-generation.create-script-source" value="/META-INF/create-db.sql"/>
|
||||
<property name="javax.persistence.schema-generation.drop-script-source" value="/META-INF/drop-db.sql"/>
|
||||
<property name="javax.persistence.sql-load-script-source" value="/META-INF/data.sql"/>
|
||||
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
|
@ -0,0 +1,194 @@
|
|||
package com.baeldung.hibernate.entitymanager.getreference;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class GetReferenceH2IntegrationTest {
|
||||
|
||||
private static EntityManagerFactory entityManagerFactory;
|
||||
private static EntityManager entityManager;
|
||||
|
||||
private static final PrintStream SystemOut = System.out;
|
||||
private static OutputStream output;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
// close some specific loggers so that we can clearly see Hibernate: SQL queries
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.SQL")).setLevel(Level.OFF);
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.type.descriptor.sql")).setLevel(Level.OFF);
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.stat")).setLevel(Level.OFF);
|
||||
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("com.baeldung.hibernate.entitymanager.game_player_h2");
|
||||
entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
|
||||
entityManager.persist(new Game(1L, "Game 1"));
|
||||
entityManager.persist(new Game(2L, "Game 2"));
|
||||
entityManager.persist(new Player(1L, "Player 1"));
|
||||
entityManager.persist(new Player(2L, "Player 2"));
|
||||
entityManager.persist(new Player(3L, "Player 3"));
|
||||
|
||||
entityManager.getTransaction().commit();
|
||||
entityManager.clear();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
private void runInTransaction(Runnable task) {
|
||||
// We create new persistence context for each test method to discard Hibernate first level cache.
|
||||
// So that we can see the behavior of getReference() method in a non-cached environment.
|
||||
entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
task.run();
|
||||
entityManager.getTransaction().commit();
|
||||
// In any case, we use clear() and close() to make all the managed entities detached and cleared.
|
||||
// So, we can be sure we test always on a clear persistence context.
|
||||
entityManager.clear();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEach() {
|
||||
// stubbing System.out printStream
|
||||
output = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(output));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void afterEach() {
|
||||
System.setOut(SystemOut);
|
||||
// we print to see original output after each test method
|
||||
System.out.print(output.toString());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToUpdateGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game1 = entityManager.find(Game.class, 1L);
|
||||
game1.setName("Game Updated 1");
|
||||
|
||||
entityManager.persist(game1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: update Game set name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToUpdateGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game1 = entityManager.getReference(Game.class, 1L);
|
||||
game1.setName("Game Updated 2");
|
||||
|
||||
entityManager.persist(game1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: update Game set name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToDeletePlayer_thenExecutesSelectForPlayer() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Player player2 = entityManager.find(Player.class, 2L);
|
||||
entityManager.remove(player2);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: delete from Player where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToDeletePlayer_thenExecutesSelectForPlayer() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Player player3 = entityManager.getReference(Player.class, 3L);
|
||||
entityManager.remove(player3);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: delete from Player where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToUpdatePlayersGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction((() -> {
|
||||
Game game1 = entityManager.find(Game.class, 1L);
|
||||
|
||||
Player player1 = entityManager.find(Player.class, 1L);
|
||||
player1.setGame(game1);
|
||||
|
||||
entityManager.persist(player1);
|
||||
}));
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: update Player set game_id=?, name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToUpdatePlayersGame_thenDoesNotExecuteSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game2 = entityManager.getReference(Game.class, 2L);
|
||||
|
||||
Player player1 = entityManager.find(Player.class, 1L);
|
||||
player1.setGame(game2);
|
||||
|
||||
entityManager.persist(player1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: update Player set game_id=?, name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
package com.baeldung.hibernate.entitymanager.getreference;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* We need to have a running MySQL instance and a schema named "baeldung" ready in order to execute this integration test
|
||||
*/
|
||||
public class GetReferenceMySQLManualTest {
|
||||
|
||||
private static EntityManagerFactory entityManagerFactory;
|
||||
private static EntityManager entityManager;
|
||||
|
||||
private static final PrintStream SystemOut = System.out;
|
||||
private static OutputStream output;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
// close some specific loggers so that we can clearly see Hibernate: SQL queries
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.SQL")).setLevel(Level.OFF);
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.type.descriptor.sql")).setLevel(Level.OFF);
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.stat")).setLevel(Level.OFF);
|
||||
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("com.baeldung.hibernate.entitymanager.game_player_mysql");
|
||||
}
|
||||
|
||||
private void runInTransaction(Runnable task) {
|
||||
// We create new persistence context for each test method to discard Hibernate first level cache.
|
||||
// So that we can see the behavior of getReference() method in a non-cached environment.
|
||||
entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
task.run();
|
||||
entityManager.getTransaction().commit();
|
||||
// In any case, we use clear() and close() to make all the managed entities detached and cleared.
|
||||
// So, we can be sure we test always on a clear persistence context.
|
||||
entityManager.clear();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEach() {
|
||||
// stubbing System.out printStream
|
||||
output = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(output));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void afterEach() {
|
||||
System.setOut(SystemOut);
|
||||
// we print to see original output after each test method
|
||||
System.out.print(output.toString());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToUpdateGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game1 = entityManager.find(Game.class, 1L);
|
||||
game1.setName("Game Updated 1");
|
||||
|
||||
entityManager.persist(game1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: update Game set name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToUpdateGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game1 = entityManager.getReference(Game.class, 1L);
|
||||
game1.setName("Game Updated 2");
|
||||
|
||||
entityManager.persist(game1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: update Game set name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToDeletePlayer_thenExecutesSelectForPlayer() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Player player2 = entityManager.find(Player.class, 2L);
|
||||
entityManager.remove(player2);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: delete from Player where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToDeletePlayer_thenExecutesSelectForPlayer() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Player player3 = entityManager.getReference(Player.class, 3L);
|
||||
entityManager.remove(player3);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: delete from Player where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToUpdatePlayersGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction((() -> {
|
||||
Game game1 = entityManager.find(Game.class, 1L);
|
||||
|
||||
Player player1 = entityManager.find(Player.class, 1L);
|
||||
player1.setGame(game1);
|
||||
|
||||
entityManager.persist(player1);
|
||||
}));
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: update Player set game_id=?, name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToUpdatePlayersGame_thenDoesNotExecuteSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game2 = entityManager.getReference(Game.class, 2L);
|
||||
|
||||
Player player1 = entityManager.find(Player.class, 1L);
|
||||
player1.setGame(game2);
|
||||
|
||||
entityManager.persist(player1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: update Player set game_id=?, name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,185 @@
|
|||
package com.baeldung.hibernate.entitymanager.getreference;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* We need to have a running PostgreSQL instance in order to execute this integration test
|
||||
*/
|
||||
public class GetReferencePostgreSQLManualTest {
|
||||
|
||||
private static EntityManagerFactory entityManagerFactory;
|
||||
private static EntityManager entityManager;
|
||||
|
||||
private static final PrintStream SystemOut = System.out;
|
||||
private static OutputStream output;
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() {
|
||||
// close some specific loggers so that we can clearly see Hibernate: SQL queries
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.SQL")).setLevel(Level.OFF);
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.type.descriptor.sql")).setLevel(Level.OFF);
|
||||
((Logger) LoggerFactory.getLogger("org.hibernate.stat")).setLevel(Level.OFF);
|
||||
|
||||
entityManagerFactory = Persistence.createEntityManagerFactory("com.baeldung.hibernate.entitymanager.game_player_postgresql");
|
||||
}
|
||||
|
||||
private void runInTransaction(Runnable task) {
|
||||
// We create new persistence context for each test method to discard Hibernate first level cache.
|
||||
// So that we can see the behavior of getReference() method in a non-cached environment.
|
||||
entityManager = entityManagerFactory.createEntityManager();
|
||||
entityManager.getTransaction().begin();
|
||||
task.run();
|
||||
entityManager.getTransaction().commit();
|
||||
// In any case, we use clear() and close() to make all the managed entities detached and cleared.
|
||||
// So, we can be sure we test always on a clear persistence context.
|
||||
entityManager.clear();
|
||||
entityManager.close();
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void beforeEach() {
|
||||
// stubbing System.out printStream
|
||||
output = new ByteArrayOutputStream();
|
||||
System.setOut(new PrintStream(output));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void afterEach() {
|
||||
System.setOut(SystemOut);
|
||||
// we print to see original output after each test method
|
||||
System.out.print(output.toString());
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() {
|
||||
entityManagerFactory.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToUpdateGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game1 = entityManager.find(Game.class, 1L);
|
||||
game1.setName("Game Updated 1");
|
||||
|
||||
entityManager.persist(game1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: update Game set name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToUpdateGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game1 = entityManager.getReference(Game.class, 1L);
|
||||
game1.setName("Game Updated 2");
|
||||
|
||||
entityManager.persist(game1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: update Game set name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToDeletePlayer_thenExecutesSelectForPlayer() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Player player2 = entityManager.find(Player.class, 2L);
|
||||
entityManager.remove(player2);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: delete from Player where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToDeletePlayer_thenExecutesSelectForPlayer() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Player player3 = entityManager.getReference(Player.class, 3L);
|
||||
entityManager.remove(player3);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: delete from Player where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFindMethodToUpdatePlayersGame_thenExecutesSelectForGame() {
|
||||
|
||||
runInTransaction((() -> {
|
||||
Game game1 = entityManager.find(Game.class, 1L);
|
||||
|
||||
Player player1 = entityManager.find(Player.class, 1L);
|
||||
player1.setGame(game1);
|
||||
|
||||
entityManager.persist(player1);
|
||||
}));
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select game0_.id as id1_0_0_, game0_.name as name2_0_0_ from Game game0_ where game0_.id=?\n");
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: update Player set game_id=?, name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingGetReferenceMethodToUpdatePlayersGame_thenDoesNotExecuteSelectForGame() {
|
||||
|
||||
runInTransaction(() -> {
|
||||
Game game2 = entityManager.getReference(Game.class, 2L);
|
||||
|
||||
Player player1 = entityManager.find(Player.class, 1L);
|
||||
player1.setGame(game2);
|
||||
|
||||
entityManager.persist(player1);
|
||||
});
|
||||
|
||||
StringBuilder expected = new StringBuilder();
|
||||
expected.append("Hibernate: select ");
|
||||
expected.append("player0_.id as id1_1_0_, player0_.game_id as game_id3_1_0_, ");
|
||||
expected.append("player0_.name as name2_1_0_, game1_.id as id1_0_1_, game1_.name as name2_0_1_ ");
|
||||
expected.append("from Player player0_ left outer join Game game1_ on player0_.game_id=game1_.id where player0_.id=?\n");
|
||||
expected.append("Hibernate: update Player set game_id=?, name=? where id=?\n");
|
||||
|
||||
assertEquals(expected.toString(), output.toString());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue