EntityNotFoundException in Hibernate (#11085)

* JPA Entities and the Serializable Interface

* JPA Entities and the Serializable Interface - edit after review

* JPA Entities and the Serializable Interface - formatting

* JPA Entities and the Serializable Interface - indentation

* EntityNotFoundException in Hibernate
This commit is contained in:
Mladen Savic 2021-07-29 17:15:46 +02:00 committed by GitHub
parent 1409769a18
commit ef23c27c9e
5 changed files with 177 additions and 0 deletions

View File

@ -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<Item> 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<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -119,4 +119,22 @@
<property name="javax.persistence.jdbc.password" value=""/>
</properties>
</persistence-unit>
<persistence-unit name="com.baeldung.hibernate.entitynotfoundexception.h2_persistence_unit">
<description>EntityManager EntityNotFoundException persistence unit</description>
<class>com.baeldung.hibernate.entitynotfoundexception.Category</class>
<class>com.baeldung.hibernate.entitynotfoundexception.Item</class>
<class>com.baeldung.hibernate.entitynotfoundexception.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<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>

View File

@ -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();
}
}