BALE-2812 : JPA Basic Annotation (#6869)
* BALE-2812 : JPA Basic Annotation * BAEL-2812 : JPA Basic Annotation moving code * BAEL-2812 : Removing changes from hibernate5 module
This commit is contained in:
parent
fa4a34317b
commit
e4abb5af73
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.hibernate.basicannotation;
|
||||||
|
|
||||||
|
import javax.persistence.Basic;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.hibernate.basicannotation;
|
||||||
|
|
||||||
|
import com.baeldung.hibernate.HibernateUtil;
|
||||||
|
import com.baeldung.hibernate.basicannotation.Course;
|
||||||
|
import com.baeldung.hibernate.Strategy;
|
||||||
|
import org.hibernate.PropertyValueException;
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class BasicAnnotationIntegrationTest {
|
||||||
|
|
||||||
|
private static SessionFactory sessionFactory;
|
||||||
|
private Session session;
|
||||||
|
private Transaction transaction;
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void beforeTests() {
|
||||||
|
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_COLUMN_BASED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws IOException {
|
||||||
|
session = sessionFactory.openSession();
|
||||||
|
transaction = session.beginTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
transaction.rollback();
|
||||||
|
session.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenACourse_whenCourseNamePresent_shouldPersist() {
|
||||||
|
Course course = new Course();
|
||||||
|
course.setName("Computers");
|
||||||
|
|
||||||
|
session.save(course);
|
||||||
|
session.flush();
|
||||||
|
session.clear();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = PropertyValueException.class)
|
||||||
|
public void givenACourse_whenCourseNameAbsent_shouldFail() {
|
||||||
|
Course course = new Course();
|
||||||
|
|
||||||
|
session.save(course);
|
||||||
|
session.flush();
|
||||||
|
session.clear();
|
||||||
|
}
|
||||||
|
}
|
@ -20,8 +20,8 @@ public class Course {
|
|||||||
public void setCourseId(UUID courseId) {
|
public void setCourseId(UUID courseId) {
|
||||||
this.courseId = courseId;
|
this.courseId = courseId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user