JAVA-8357: Split or move hibernate-jpa module

This commit is contained in:
sampadawagde
2021-12-18 23:00:24 +05:30
parent b4e0ac6f1b
commit f401f6ba9e
9 changed files with 30 additions and 19 deletions

View File

@@ -13,5 +13,4 @@ This module contains articles specific to use of Hibernate as a JPA implementati
- [Enabling Transaction Locks in Spring Data JPA](https://www.baeldung.com/java-jpa-transaction-locks)
- [JPA/Hibernate Persistence Context](https://www.baeldung.com/jpa-hibernate-persistence-context)
- [Quick Guide to EntityManager#getReference()](https://www.baeldung.com/jpa-entity-manager-get-reference)
- [JPA Entities and the Serializable Interface](https://www.baeldung.com/jpa-entities-serializable)
- [EntityNotFoundException in Hibernate](https://www.baeldung.com/hibernate-entitynotfoundexception)
- [JPA Entities and the Serializable Interface](https://www.baeldung.com/jpa-entities-serializable)

View File

@@ -1,42 +0,0 @@
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

@@ -1,43 +0,0 @@
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))
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

@@ -1,28 +0,0 @@
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

@@ -120,21 +120,4 @@
</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:db3;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

@@ -1,45 +0,0 @@
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();
}
}