Skip to content

Commit e4abb5a

Browse files
shubhi22maibin
authored andcommitted
BALE-2812 : JPA Basic Annotation (eugenp#6869)
* BALE-2812 : JPA Basic Annotation * BAEL-2812 : JPA Basic Annotation moving code * BAEL-2812 : Removing changes from hibernate5 module
1 parent fa4a343 commit e4abb5a

3 files changed

Lines changed: 96 additions & 3 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.hibernate.basicannotation;
2+
3+
import javax.persistence.Basic;
4+
import javax.persistence.Entity;
5+
import javax.persistence.FetchType;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Course {
11+
12+
@Id
13+
private int id;
14+
15+
@Basic(optional = false, fetch = FetchType.LAZY)
16+
private String name;
17+
18+
public int getId() {
19+
return id;
20+
}
21+
22+
public void setId(int id) {
23+
this.id = id;
24+
}
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
}
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.hibernate.basicannotation;
2+
3+
import com.baeldung.hibernate.HibernateUtil;
4+
import com.baeldung.hibernate.basicannotation.Course;
5+
import com.baeldung.hibernate.Strategy;
6+
import org.hibernate.PropertyValueException;
7+
import org.hibernate.Session;
8+
import org.hibernate.Transaction;
9+
import org.junit.After;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.hibernate.SessionFactory;
13+
import org.junit.BeforeClass;
14+
15+
import java.io.IOException;
16+
17+
public class BasicAnnotationIntegrationTest {
18+
19+
private static SessionFactory sessionFactory;
20+
private Session session;
21+
private Transaction transaction;
22+
23+
@BeforeClass
24+
public static void beforeTests() {
25+
sessionFactory = HibernateUtil.getSessionFactory(Strategy.MAP_KEY_COLUMN_BASED);
26+
}
27+
28+
@Before
29+
public void setUp() throws IOException {
30+
session = sessionFactory.openSession();
31+
transaction = session.beginTransaction();
32+
}
33+
34+
@After
35+
public void tearDown() {
36+
transaction.rollback();
37+
session.close();
38+
}
39+
40+
@Test
41+
public void givenACourse_whenCourseNamePresent_shouldPersist() {
42+
Course course = new Course();
43+
course.setName("Computers");
44+
45+
session.save(course);
46+
session.flush();
47+
session.clear();
48+
49+
}
50+
51+
@Test(expected = PropertyValueException.class)
52+
public void givenACourse_whenCourseNameAbsent_shouldFail() {
53+
Course course = new Course();
54+
55+
session.save(course);
56+
session.flush();
57+
session.clear();
58+
}
59+
}

persistence-modules/hibernate5/src/main/java/com/baeldung/hibernate/pojo/Course.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public UUID getCourseId() {
2020
public void setCourseId(UUID courseId) {
2121
this.courseId = courseId;
2222
}
23-
24-
2523

26-
24+
25+
26+
2727
}

0 commit comments

Comments
 (0)