From a7f347194bc16dc725b5dab6a29a27d7833133cd Mon Sep 17 00:00:00 2001 From: Shubhra Srivastava Date: Fri, 10 May 2019 09:42:58 +0530 Subject: [PATCH] Bael 2812 jpa basic annotation (#6922) * BALE-2812 : JPA Basic Annotation * BAEL-2812 : JPA Basic Annotation moving code * BAEL-2812 : Removing changes from hibernate5 module * BAEL-2812 : Moving files to java-jpa --- .../baeldung/jpa/basicannotation/Course.java | 33 +++++++++++ .../BasicAnnotationIntegrationTest.java | 56 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/basicannotation/Course.java create mode 100644 persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/basicannotation/BasicAnnotationIntegrationTest.java diff --git a/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/basicannotation/Course.java b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/basicannotation/Course.java new file mode 100644 index 0000000000..cc5a83420c --- /dev/null +++ b/persistence-modules/java-jpa/src/main/java/com/baeldung/jpa/basicannotation/Course.java @@ -0,0 +1,33 @@ +package com.baeldung.jpa.basicannotation; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; + +@Entity +public class Course { + + @Id + private int id; + + @Basic(optional = false, fetch = FetchType.LAZY) + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} + diff --git a/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/basicannotation/BasicAnnotationIntegrationTest.java b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/basicannotation/BasicAnnotationIntegrationTest.java new file mode 100644 index 0000000000..8580aa6602 --- /dev/null +++ b/persistence-modules/java-jpa/src/test/java/com/baeldung/jpa/basicannotation/BasicAnnotationIntegrationTest.java @@ -0,0 +1,56 @@ +package com.baeldung.jpa.basicannotation; + +import org.hibernate.PropertyValueException; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.AfterClass; +import org.junit.Test; + +import java.io.IOException; + +public class BasicAnnotationIntegrationTest { + + private static EntityManager entityManager; + private static EntityManagerFactory entityManagerFactory; + + @BeforeClass + public void setup() { + entityManagerFactory = Persistence.createEntityManagerFactory("java-jpa-scheduled-day"); + entityManager = entityManagerFactory.createEntityManager(); + } + + @Test + public void givenACourse_whenCourseNamePresent_shouldPersist() { + Course course = new Course(); + course.setName("Computers"); + + entityManager.persist(course); + entityManager.flush(); + entityManager.clear(); + + } + + @Test(expected = PropertyValueException.class) + public void givenACourse_whenCourseNameAbsent_shouldFail() { + Course course = new Course(); + + entityManager.persist(course); + entityManager.flush(); + entityManager.clear(); + } + + @AfterClass + public void destroy() { + + if (entityManager != null) { + entityManager.close(); + } + if (entityManagerFactory != null) { + entityManagerFactory.close(); + } + } +}