From 69be42810394a67831c0fae190fd541ae5633863 Mon Sep 17 00:00:00 2001 From: Mladen Savic Date: Thu, 29 Jul 2021 15:41:01 +0200 Subject: [PATCH] EntityNotFoundException in Hibernate --- .../entitynotfoundexception/Category.java | 42 +++++++++++++++++ .../entitynotfoundexception/Item.java | 44 ++++++++++++++++++ .../entitynotfoundexception/User.java | 28 ++++++++++++ .../main/resources/META-INF/persistence.xml | 18 ++++++++ ...ntityNotFoundExceptionIntegrationTest.java | 45 +++++++++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java create mode 100644 persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java create mode 100644 persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java new file mode 100644 index 0000000000..25d31d50c7 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Category.java @@ -0,0 +1,42 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +@Entity +public class Category implements Serializable { + + @Id + @Column(unique = true, nullable = false) + private long id; + private String name; + @OneToMany + @JoinColumn(name = "category_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) + private List items = new ArrayList<>(); + + 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 List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java new file mode 100644 index 0000000000..ee6f677d7d --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/Item.java @@ -0,0 +1,44 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import org.hibernate.annotations.NotFound; +import org.hibernate.annotations.NotFoundAction; + +import javax.persistence.*; +import java.io.Serializable; + +@Entity +public class Item implements Serializable { + + @Id + @Column(unique = true, nullable = false) + private long id; + private String name; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "category_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT)) + @NotFound(action = NotFoundAction.IGNORE) + private Category category; + + 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 Category getCategory() { + return category; + } + + public void setCategory(Category category) { + this.category = category; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java new file mode 100644 index 0000000000..d89047195c --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/main/java/com/baeldung/hibernate/entitynotfoundexception/User.java @@ -0,0 +1,28 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class User { + + @Id + private long id; + private String 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; + } +} diff --git a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml index 3482414b9d..29cd4faf05 100644 --- a/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml +++ b/persistence-modules/hibernate-jpa/src/main/resources/META-INF/persistence.xml @@ -119,4 +119,22 @@ + + + EntityManager EntityNotFoundException persistence unit + com.baeldung.hibernate.entitynotfoundexception.Category + com.baeldung.hibernate.entitynotfoundexception.Item + com.baeldung.hibernate.entitynotfoundexception.User + true + + + + + + + + + + + \ No newline at end of file diff --git a/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java new file mode 100644 index 0000000000..bcb4e3eb95 --- /dev/null +++ b/persistence-modules/hibernate-jpa/src/test/java/com/baeldung/hibernate/entitynotfoundexception/EntityNotFoundExceptionIntegrationTest.java @@ -0,0 +1,45 @@ +package com.baeldung.hibernate.entitynotfoundexception; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityNotFoundException; +import javax.persistence.Persistence; +import java.io.IOException; + +public class EntityNotFoundExceptionIntegrationTest { + + private static EntityManager entityManager; + + @Before + public void setUp() throws IOException { + EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.baeldung.hibernate.entitynotfoundexception.h2_persistence_unit"); + entityManager = entityManagerFactory.createEntityManager(); + entityManager.getTransaction().begin(); + + } + + + @Test(expected = EntityNotFoundException.class) + public void givenNonExistingUserId_whenGetReferenceIsUsed_thenExceptionIsThrown() { + User user = entityManager.getReference(User.class, 1L); + user.getName(); + } + + @Test(expected = EntityNotFoundException.class) + public void givenItem_whenManyToOneEntityIsMissing_thenExceptionIsThrown() { + entityManager.createNativeQuery("Insert into Item (category_id, name, id) values (1, 'test', 1)").executeUpdate(); + entityManager.flush(); + Item item = entityManager.find(Item.class, 1L); + item.getCategory().getName(); + } + + @After + public void tearDown() { + entityManager.close(); + } + +}