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
This commit is contained in:
Shubhra Srivastava 2019-05-10 09:42:58 +05:30 committed by maibin
parent e1c354f9df
commit a7f347194b
2 changed files with 89 additions and 0 deletions

View File

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

View File

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