Skip to content

Commit 85dfbca

Browse files
authored
Merge pull request eugenp#6715 from vimde/master
BAEL-2857 Defining a JPA Entity
2 parents d24015f + 3f29c9a commit 85dfbca

4 files changed

Lines changed: 188 additions & 1 deletion

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.jpa.entity;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.EnumType;
8+
import javax.persistence.Enumerated;
9+
import javax.persistence.GeneratedValue;
10+
import javax.persistence.GenerationType;
11+
import javax.persistence.Id;
12+
import javax.persistence.Table;
13+
import javax.persistence.Temporal;
14+
import javax.persistence.TemporalType;
15+
import javax.persistence.Transient;
16+
17+
import com.baeldung.util.Gender;
18+
19+
@Entity
20+
@Table(name="STUDENT")
21+
public class Student {
22+
23+
@Id
24+
@GeneratedValue(strategy = GenerationType.AUTO)
25+
private Long id;
26+
@Column(name = "STUDENT_NAME", length = 50, nullable = false, unique = false)
27+
private String name;
28+
@Transient
29+
private Integer age;
30+
@Temporal(TemporalType.DATE)
31+
private Date birthDate;
32+
@Enumerated(EnumType.STRING)
33+
private Gender gender;
34+
35+
public Long getId() {
36+
return id;
37+
}
38+
39+
public void setId(Long id) {
40+
this.id = id;
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
51+
public Integer getAge() {
52+
return age;
53+
}
54+
55+
public void setAge(Integer age) {
56+
this.age = age;
57+
}
58+
59+
public Date getBirthDate() {
60+
return birthDate;
61+
}
62+
63+
public void setBirthDate(Date birthDate) {
64+
this.birthDate = birthDate;
65+
}
66+
67+
public Gender getGender() {
68+
return gender;
69+
}
70+
71+
public void setGender(Gender gender) {
72+
this.gender = gender;
73+
}
74+
75+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.util;
2+
3+
public enum Gender {
4+
MALE,
5+
FEMALE
6+
}

persistence-modules/java-jpa/src/main/resources/META-INF/persistence.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,20 @@
147147
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
148148
</properties>
149149
</persistence-unit>
150-
150+
151+
<persistence-unit name="jpa-entity-definition">
152+
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
153+
<class>com.baeldung.jpa.entity.Student</class>
154+
<exclude-unlisted-classes>true</exclude-unlisted-classes>
155+
<properties>
156+
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
157+
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test" />
158+
<property name="javax.persistence.jdbc.user" value="sa" />
159+
<property name="javax.persistence.jdbc.password" value="" />
160+
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
161+
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
162+
<property name="show_sql" value="true" />
163+
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" />
164+
</properties>
165+
</persistence-unit>
151166
</persistence>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.jpa.entity;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.time.LocalDate;
6+
import java.time.ZoneId;
7+
import java.util.Date;
8+
import java.util.List;
9+
10+
import javax.persistence.EntityManager;
11+
import javax.persistence.EntityManagerFactory;
12+
import javax.persistence.Persistence;
13+
import javax.persistence.TypedQuery;
14+
15+
import org.junit.After;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
19+
import com.baeldung.util.Gender;
20+
21+
public class StudentEntityIntegrationTest {
22+
23+
private EntityManagerFactory emf;
24+
private EntityManager em;
25+
26+
@Before
27+
public void setup() {
28+
emf = Persistence.createEntityManagerFactory("jpa-entity-definition");
29+
em = emf.createEntityManager();
30+
}
31+
32+
@Test
33+
public void persistStudentThenRetrieveTheDetails() {
34+
Student student = createStudentWithRelevantDetails();
35+
persist(student);
36+
clearThePersistenceContext();
37+
List<Student> students = getStudentsFromTable();
38+
checkAssertionsWith(students);
39+
}
40+
41+
@After
42+
public void destroy() {
43+
if (em != null) {
44+
em.close();
45+
}
46+
if (emf != null) {
47+
emf.close();
48+
}
49+
}
50+
51+
private void clearThePersistenceContext() {
52+
em.clear();
53+
}
54+
55+
private void checkAssertionsWith(List<Student> students) {
56+
assertEquals(1, students.size());
57+
Student john = students.get(0);
58+
assertEquals(1L, john.getId().longValue());
59+
assertEquals(null, john.getAge());
60+
assertEquals("John", john.getName());
61+
}
62+
63+
private List<Student> getStudentsFromTable() {
64+
String selectQuery = "SELECT student FROM Student student";
65+
TypedQuery<Student> selectFromStudentTypedQuery = em.createQuery(selectQuery, Student.class);
66+
List<Student> students = selectFromStudentTypedQuery.getResultList();
67+
return students;
68+
}
69+
70+
private void persist(Student student) {
71+
em.getTransaction().begin();
72+
em.persist(student);
73+
em.getTransaction().commit();
74+
}
75+
76+
private Student createStudentWithRelevantDetails() {
77+
Student student = new Student();
78+
student.setAge(20); // the 'age' field has been annotated with @Transient
79+
student.setName("John");
80+
Date date = getDate();
81+
student.setBirthDate(date);
82+
student.setGender(Gender.MALE);
83+
return student;
84+
}
85+
86+
private Date getDate() {
87+
LocalDate localDate = LocalDate.of(2008, 7, 20);
88+
return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
89+
}
90+
91+
}

0 commit comments

Comments
 (0)