Skip to content

Commit c9aaa0d

Browse files
lor6pivovarit
authored andcommitted
hibernate identifiers examples (eugenp#2977)
* hibernate identifiers examples * fix conflict * change to longstream * change to format
1 parent cfd3def commit c9aaa0d

12 files changed

Lines changed: 414 additions & 0 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
import com.baeldung.hibernate.pojo.Employee;
44
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.Product;
59
import com.baeldung.hibernate.pojo.Phone;
610
import com.baeldung.hibernate.pojo.TemporalValues;
11+
import com.baeldung.hibernate.pojo.Course;
12+
import com.baeldung.hibernate.pojo.Student;
13+
import com.baeldung.hibernate.pojo.User;
14+
import com.baeldung.hibernate.pojo.UserProfile;
15+
716
import org.hibernate.SessionFactory;
817
import org.hibernate.boot.Metadata;
918
import org.hibernate.boot.MetadataSources;
@@ -33,6 +42,14 @@ private static SessionFactory makeSessionFactory(ServiceRegistry serviceRegistry
3342
metadataSources.addAnnotatedClass(Phone.class);
3443
metadataSources.addAnnotatedClass(EntityDescription.class);
3544
metadataSources.addAnnotatedClass(TemporalValues.class);
45+
metadataSources.addAnnotatedClass(User.class);
46+
metadataSources.addAnnotatedClass(Student.class);
47+
metadataSources.addAnnotatedClass(Course.class);
48+
metadataSources.addAnnotatedClass(Product.class);
49+
metadataSources.addAnnotatedClass(OrderEntryPK.class);
50+
metadataSources.addAnnotatedClass(OrderEntry.class);
51+
metadataSources.addAnnotatedClass(OrderEntryIdClass.class);
52+
metadataSources.addAnnotatedClass(UserProfile.class);
3653

3754
Metadata metadata = metadataSources.buildMetadata();
3855
return metadata.getSessionFactoryBuilder()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import java.util.UUID;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Course {
11+
12+
@Id
13+
@GeneratedValue
14+
private UUID courseId;
15+
16+
public UUID getCourseId() {
17+
return courseId;
18+
}
19+
20+
public void setCourseId(UUID courseId) {
21+
this.courseId = courseId;
22+
}
23+
24+
25+
26+
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.TableGenerator;
8+
9+
@Entity
10+
public class Department {
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.TABLE, generator="table-generator")
13+
@TableGenerator (name="table-generator", table="dep_ids", pkColumnName="seq_id", valueColumnName="seq_value")
14+
private long depId;
15+
16+
public long getDepId() {
17+
return depId;
18+
}
19+
20+
public void setDepId(long depId) {
21+
this.depId = depId;
22+
}
23+
24+
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.EmbeddedId;
4+
import javax.persistence.Entity;
5+
6+
@Entity
7+
public class OrderEntry {
8+
9+
@EmbeddedId
10+
private OrderEntryPK entryId;
11+
12+
public OrderEntryPK getEntryId() {
13+
return entryId;
14+
}
15+
16+
public void setEntryId(OrderEntryPK entryId) {
17+
this.entryId = entryId;
18+
}
19+
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.IdClass;
6+
7+
@Entity
8+
@IdClass(OrderEntryPK.class)
9+
public class OrderEntryIdClass {
10+
@Id
11+
private long orderId;
12+
@Id
13+
private long productId;
14+
15+
public long getOrderId() {
16+
return orderId;
17+
}
18+
19+
public void setOrderId(long orderId) {
20+
this.orderId = orderId;
21+
}
22+
23+
public long getProductId() {
24+
return productId;
25+
}
26+
27+
public void setProductId(long productId) {
28+
this.productId = productId;
29+
}
30+
31+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import java.io.Serializable;
4+
import java.util.Objects;
5+
6+
import javax.persistence.Embeddable;
7+
8+
@Embeddable
9+
public class OrderEntryPK implements Serializable {
10+
11+
private long orderId;
12+
private long productId;
13+
14+
public long getOrderId() {
15+
return orderId;
16+
}
17+
18+
public void setOrderId(long orderId) {
19+
this.orderId = orderId;
20+
}
21+
22+
public long getProductId() {
23+
return productId;
24+
}
25+
26+
public void setProductId(long productId) {
27+
this.productId = productId;
28+
}
29+
30+
@Override
31+
public boolean equals(Object o) {
32+
if (this == o) {
33+
return true;
34+
}
35+
if (o == null || getClass() != o.getClass()) {
36+
return false;
37+
}
38+
OrderEntryPK pk = (OrderEntryPK) o;
39+
return Objects.equals(orderId, pk.orderId) && Objects.equals(productId, pk.productId);
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
return Objects.hash(orderId, productId);
45+
}
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
7+
import org.hibernate.annotations.GenericGenerator;
8+
import org.hibernate.annotations.Parameter;
9+
10+
@Entity
11+
public class Product {
12+
13+
@Id
14+
@GeneratedValue(generator = "prod-generator")
15+
@GenericGenerator(name = "prod-generator", parameters = @Parameter(name = "prefix", value = "prod"), strategy = "com.baeldung.hibernate.pojo.generator.MyGenerator")
16+
private String prodId;
17+
18+
public String getProdId() {
19+
return prodId;
20+
}
21+
22+
public void setProdId(String prodId) {
23+
this.prodId = prodId;
24+
}
25+
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
8+
@Entity
9+
public class Student {
10+
11+
@Id
12+
@GeneratedValue (strategy = GenerationType.SEQUENCE)
13+
private long studentId;
14+
15+
public long getStudentId() {
16+
return studentId;
17+
}
18+
19+
public void setStudent_id(long studentId) {
20+
this.studentId = studentId;
21+
}
22+
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.GenerationType;
6+
import javax.persistence.Id;
7+
import javax.persistence.SequenceGenerator;
8+
9+
@Entity
10+
public class User {
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence-generator")
13+
@SequenceGenerator(name = "sequence-generator", sequenceName = "user_sequence", initialValue = 4)
14+
private long userId;
15+
16+
public long getUserId() {
17+
return userId;
18+
}
19+
20+
public void setUserId(long userId) {
21+
this.userId = userId;
22+
}
23+
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.hibernate.pojo;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.MapsId;
6+
import javax.persistence.OneToOne;
7+
8+
@Entity
9+
public class UserProfile {
10+
11+
@Id
12+
private long profileId;
13+
14+
@OneToOne
15+
@MapsId
16+
private User user;
17+
18+
public long getProfileId() {
19+
return profileId;
20+
}
21+
22+
public void setProfileId(long profileId) {
23+
this.profileId = profileId;
24+
}
25+
26+
public User getUser() {
27+
return user;
28+
}
29+
30+
public void setUser(User user) {
31+
this.user = user;
32+
}
33+
34+
}

0 commit comments

Comments
 (0)