Skip to content

Commit c0c319b

Browse files
BAEL-1713: Pessimistic Locking in JPA
Tests for the article.
1 parent 7140229 commit c0c319b

18 files changed

Lines changed: 906 additions & 26 deletions

File tree

‎hibernate5/src/main/java/com/baeldung/hibernate/HibernateUtil.java‎

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
package com.baeldung.hibernate;
22

3-
import com.baeldung.hibernate.pojo.Employee;
4-
import com.baeldung.hibernate.pojo.EntityDescription;
5-
import com.baeldung.hibernate.pojo.OrderEntry;
6-
import com.baeldung.hibernate.pojo.OrderEntryIdClass;
7-
import com.baeldung.hibernate.pojo.OrderEntryPK;
8-
import com.baeldung.hibernate.pojo.PointEntity;
9-
import com.baeldung.hibernate.pojo.PolygonEntity;
10-
import com.baeldung.hibernate.pojo.Product;
11-
import com.baeldung.hibernate.pojo.Phone;
12-
import com.baeldung.hibernate.pojo.TemporalValues;
13-
import com.baeldung.hibernate.pojo.Course;
14-
import com.baeldung.hibernate.pojo.Student;
15-
import com.baeldung.hibernate.pojo.User;
16-
import com.baeldung.hibernate.pojo.UserProfile;
17-
import com.baeldung.hibernate.pojo.inheritance.Animal;
18-
import com.baeldung.hibernate.pojo.inheritance.Bag;
19-
import com.baeldung.hibernate.pojo.inheritance.Book;
20-
import com.baeldung.hibernate.pojo.inheritance.Car;
21-
import com.baeldung.hibernate.pojo.inheritance.MyEmployee;
22-
import com.baeldung.hibernate.pojo.inheritance.MyProduct;
23-
import com.baeldung.hibernate.pojo.inheritance.Pen;
24-
import com.baeldung.hibernate.pojo.inheritance.Person;
25-
import com.baeldung.hibernate.pojo.inheritance.Pet;
26-
import com.baeldung.hibernate.pojo.inheritance.Vehicle;
27-
3+
import com.baeldung.hibernate.pessimisticlocking.Individual;
4+
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingCourse;
5+
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingEmployee;
6+
import com.baeldung.hibernate.pessimisticlocking.PessimisticLockingStudent;
7+
import com.baeldung.hibernate.pojo.*;
8+
import com.baeldung.hibernate.pojo.Person;
9+
import com.baeldung.hibernate.pojo.inheritance.*;
2810
import org.apache.commons.lang3.StringUtils;
2911
import org.hibernate.SessionFactory;
3012
import org.hibernate.boot.Metadata;
@@ -82,6 +64,12 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
8264
metadataSources.addAnnotatedClass(PointEntity.class);
8365
metadataSources.addAnnotatedClass(PolygonEntity.class);
8466
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pojo.Person.class);
67+
metadataSources.addAnnotatedClass(Individual.class);
68+
metadataSources.addAnnotatedClass(PessimisticLockingEmployee.class);
69+
metadataSources.addAnnotatedClass(PessimisticLockingStudent.class);
70+
metadataSources.addAnnotatedClass(PessimisticLockingCourse.class);
71+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Customer.class);
72+
metadataSources.addAnnotatedClass(com.baeldung.hibernate.pessimisticlocking.Address.class);
8573

8674
Metadata metadata = metadataSources.buildMetadata();
8775
return metadata.getSessionFactoryBuilder()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.hibernate.pessimisticlocking;
2+
3+
import javax.persistence.Embeddable;
4+
5+
@Embeddable
6+
public class Address {
7+
8+
private String country;
9+
private String city;
10+
11+
public Address(String country, String city) {
12+
this.country = country;
13+
this.city = city;
14+
}
15+
16+
public Address() {
17+
}
18+
19+
public String getCountry() {
20+
return country;
21+
}
22+
23+
public void setCountry(String country) {
24+
this.country = country;
25+
}
26+
27+
public String getCity() {
28+
return city;
29+
}
30+
31+
public void setCity(String city) {
32+
this.city = city;
33+
}
34+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.hibernate.pessimisticlocking;
2+
3+
import javax.persistence.*;
4+
import java.util.List;
5+
6+
@Entity
7+
public class Customer {
8+
9+
@Id
10+
private Long customerId;
11+
private String name;
12+
private String lastName;
13+
@ElementCollection
14+
@CollectionTable(name = "customer_address")
15+
private List<Address> addressList;
16+
17+
public Customer() {
18+
}
19+
20+
public Customer(Long customerId, String name, String lastName, List<Address> addressList) {
21+
this.customerId = customerId;
22+
this.name = name;
23+
this.lastName = lastName;
24+
this.addressList = addressList;
25+
}
26+
27+
public Long getCustomerId() {
28+
return customerId;
29+
}
30+
31+
public void setCustomerId(Long customerId) {
32+
this.customerId = customerId;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public String getLastName() {
44+
return lastName;
45+
}
46+
47+
public void setLastName(String lastName) {
48+
this.lastName = lastName;
49+
}
50+
51+
public List<Address> getAddressList() {
52+
return addressList;
53+
}
54+
55+
public void setAddressList(List<Address> addressList) {
56+
this.addressList = addressList;
57+
}
58+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.hibernate.pessimisticlocking;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.Inheritance;
6+
import javax.persistence.InheritanceType;
7+
8+
@Entity
9+
@Inheritance(strategy = InheritanceType.JOINED)
10+
public class Individual {
11+
12+
@Id
13+
private Long id;
14+
private String name;
15+
private String lastName;
16+
17+
public Individual(Long id, String name, String lastName) {
18+
this.id = id;
19+
this.name = name;
20+
this.lastName = lastName;
21+
}
22+
23+
public Individual() {
24+
}
25+
26+
public Long getId() {
27+
return id;
28+
}
29+
30+
public void setId(Long id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public String getLastName() {
43+
return lastName;
44+
}
45+
46+
public void setLastName(String lastName) {
47+
this.lastName = lastName;
48+
}
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.hibernate.pessimisticlocking;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
public class PessimisticLockingCourse {
7+
8+
@Id
9+
private Long courseId;
10+
private String name;
11+
@ManyToOne
12+
@JoinTable(name = "student_course")
13+
private PessimisticLockingStudent student;
14+
15+
public PessimisticLockingCourse(Long courseId, String name, PessimisticLockingStudent student) {
16+
this.courseId = courseId;
17+
this.name = name;
18+
this.student = student;
19+
}
20+
21+
public PessimisticLockingCourse() {
22+
}
23+
24+
public Long getCourseId() {
25+
return courseId;
26+
}
27+
28+
public void setCourseId(Long courseId) {
29+
this.courseId = courseId;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public PessimisticLockingStudent getStudent() {
41+
return student;
42+
}
43+
44+
public void setStudent(PessimisticLockingStudent students) {
45+
this.student = students;
46+
}
47+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.hibernate.pessimisticlocking;
2+
3+
import javax.persistence.Entity;
4+
import java.math.BigDecimal;
5+
6+
@Entity
7+
public class PessimisticLockingEmployee extends Individual {
8+
9+
private BigDecimal salary;
10+
11+
public PessimisticLockingEmployee(Long id, String name, String lastName, BigDecimal salary) {
12+
super(id, name, lastName);
13+
this.salary = salary;
14+
}
15+
16+
public PessimisticLockingEmployee() {
17+
super();
18+
}
19+
20+
public BigDecimal getSalary() {
21+
return salary;
22+
}
23+
24+
public void setSalary(BigDecimal average) {
25+
this.salary = average;
26+
}
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.hibernate.pessimisticlocking;
2+
3+
import javax.persistence.*;
4+
import java.util.List;
5+
6+
@Entity
7+
public class PessimisticLockingStudent {
8+
9+
@Id
10+
private Long id;
11+
private String name;
12+
@OneToMany(mappedBy = "student")
13+
private List<PessimisticLockingCourse> courses;
14+
15+
public PessimisticLockingStudent(Long id, String name) {
16+
this.id = id;
17+
this.name = name;
18+
}
19+
20+
public PessimisticLockingStudent() {
21+
}
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public List<PessimisticLockingCourse> getCourses() {
40+
return courses;
41+
}
42+
43+
public void setCourses(List<PessimisticLockingCourse> courses) {
44+
this.courses = courses;
45+
}
46+
}

0 commit comments

Comments
 (0)