Skip to content

Hibernate returns null instead of throwing an exception when using @N… #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions orm/hibernate-orm-6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@
<artifactId>junit</artifactId>
<version>${version.junit}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${version.org.slf4j}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.hibernate.bugs;

import java.time.LocalDate;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Version;

@Entity
public class ChessGame {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@NotFound(action = NotFoundAction.EXCEPTION)
private ChessPlayer playerWhite;

@ManyToOne(fetch = FetchType.LAZY)
@NotFound(action = NotFoundAction.IGNORE)
private ChessPlayer playerBlack;

@Version
private int version;

public Long getId() {
return id;
}

public int getVersion() {
return version;
}

public ChessPlayer getPlayerWhite() {
return playerWhite;
}

public void setPlayerWhite(ChessPlayer playerWhite) {
this.playerWhite = playerWhite;
}

public ChessPlayer getPlayerBlack() {
return playerBlack;
}

public void setPlayerBlack(ChessPlayer playerBlack) {
this.playerBlack = playerBlack;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.hibernate.bugs;

import java.time.LocalDate;
import java.util.Set;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Version;

@Entity
public class ChessPlayer {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;

@OneToMany(mappedBy = "playerWhite")
private Set<ChessGame> gamesWhite;

@OneToMany(mappedBy = "playerBlack")
private Set<ChessGame> gamesBlack;

@Version
private int version;

public Long getId() {
return id;
}

public Set<ChessGame> getGamesWhite() {
return gamesWhite;
}

public void setGamesWhite(Set<ChessGame> gamesWhite) {
this.gamesWhite = gamesWhite;
}

public Set<ChessGame> getGamesBlack() {
return gamesBlack;
}

public void setGamesBlack(Set<ChessGame> gamesBlack) {
this.gamesBlack = gamesBlack;
}

public int getVersion() {
return version;
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package org.hibernate.bugs;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;

/**
* This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API.
*/
public class JPAUnitTestCase {

Logger log = LogManager.getLogger(this.getClass().getName());

private EntityManagerFactory entityManagerFactory;

@Before
Expand All @@ -28,10 +36,20 @@ public void destroy() {
// Entities are auto-discovered, so just add them anywhere on class-path
// Add your tests, using standard JUnit.
@Test
public void hhh123Test() throws Exception {
public void notFoundIssue() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// Do stuff...

ChessGame game = entityManager.find(ChessGame.class, 1L);
assertNotNull("Returned entity shouldn't be null", game);
assertNull(game.getPlayerBlack(), "Broken foreign key reference with NotFoundAction.IGNORE should return null");
try {
game.getPlayerWhite();
fail("Accessing a broken foreign key reference should throw an exception.");
} catch (Exception e) {
// Exception expected
}

entityManager.getTransaction().commit();
entityManager.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="javax.persistence.sql-load-script-source" value="data.sql"/>

<property name="hibernate.max_fetch_depth" value="5"/>

Expand Down
1 change: 1 addition & 0 deletions orm/hibernate-orm-6/src/test/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO ChessGame(id, version, playerwhite_id, playerblack_id) VALUES (1, 0, 1, 2);
30 changes: 0 additions & 30 deletions orm/hibernate-orm-6/src/test/resources/hibernate.properties

This file was deleted.

8 changes: 0 additions & 8 deletions orm/hibernate-orm-6/src/test/resources/log4j.properties

This file was deleted.

17 changes: 17 additions & 0 deletions orm/hibernate-orm-6/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss,SSS} %-5p [%c] - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.hibernate.stat" level="debug"/>
<Logger name="org.hibernate.SQL" level="debug"/>

<Logger name="org.hibernate" level="info" />
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>