Skip to content

Commit 7ddf988

Browse files
authored
Model classes
1 parent 42bb5b2 commit 7ddf988

6 files changed

+298
-0
lines changed

Diff for: Address.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hibernate.dto;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Embeddable;
5+
6+
@Embeddable
7+
public class Address {
8+
9+
@Column(name="STREET_NAME")
10+
private String street;
11+
12+
@Column(name="TOWN_NAME")
13+
private String town;
14+
15+
public String getStreet() {
16+
return street;
17+
}
18+
19+
public void setStreet(String street) {
20+
this.street = street;
21+
}
22+
23+
public String getTown() {
24+
return town;
25+
}
26+
27+
public void setTown(String town) {
28+
this.town = town;
29+
}
30+
31+
}

Diff for: EmpDetails.java

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.hibernate.dto;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.AttributeOverride;
6+
import javax.persistence.AttributeOverrides;
7+
import javax.persistence.Basic;
8+
import javax.persistence.Column;
9+
import javax.persistence.Embedded;
10+
import javax.persistence.Entity;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.GenerationType;
13+
import javax.persistence.Id;
14+
import javax.persistence.Lob;
15+
import javax.persistence.Table;
16+
import javax.persistence.Temporal;
17+
import javax.persistence.TemporalType;
18+
import javax.persistence.Transient;
19+
20+
@Entity
21+
@Table(name = "EMP_DETAILS")
22+
public class EmpDetails {
23+
24+
/**
25+
* Below is the surrogate key whose values are generated by hibernate
26+
*/
27+
@Id
28+
@GeneratedValue(strategy = GenerationType.IDENTITY)
29+
@Column(name="EMP_ID")
30+
private int empId;
31+
32+
/**
33+
* This means create the property as it is by using its default
34+
* hibernate-sql compatibility
35+
*/
36+
@Basic
37+
@Column(name="EMP_NAME")
38+
private String name;
39+
40+
/**
41+
* Stores only the date and ignores the timestamp
42+
*/
43+
@Temporal(TemporalType.DATE)
44+
@Column(name="EMP_DOB")
45+
private Date dob;
46+
47+
48+
/**
49+
* Override default column names set in homeAddress
50+
*/
51+
@Embedded
52+
@AttributeOverrides({
53+
@AttributeOverride(name="street", column=@Column(name="HOME_STREET_NAME")),
54+
@AttributeOverride(name="town", column=@Column(name="HOME_TOWN_NAME"))
55+
})
56+
private Address homeAddress;
57+
58+
@Embedded
59+
private Address officeAddress;
60+
61+
/**
62+
* Mark as transient when you do not want hibernate to store this field in
63+
* db
64+
*/
65+
@Transient
66+
@Column(name="EMP_HOBBY")
67+
private String hobby;
68+
69+
public String getHobby() {
70+
return hobby;
71+
}
72+
73+
public void setHobby(String hobby) {
74+
this.hobby = hobby;
75+
}
76+
77+
public int getEmpId() {
78+
return empId;
79+
}
80+
81+
public void setEmpId(int empId) {
82+
this.empId = empId;
83+
}
84+
85+
public String getName() {
86+
return name;
87+
}
88+
89+
public void setName(String name) {
90+
this.name = name;
91+
}
92+
93+
public Date getDob() {
94+
return dob;
95+
}
96+
97+
public void setDob(Date dob) {
98+
this.dob = dob;
99+
}
100+
101+
public Address getHomeAddress() {
102+
return homeAddress;
103+
}
104+
105+
public void setHomeAddress(Address homeAddress) {
106+
this.homeAddress = homeAddress;
107+
}
108+
109+
public Address getOfficeAddress() {
110+
return officeAddress;
111+
}
112+
113+
public void setOfficeAddress(Address officeAddress) {
114+
this.officeAddress = officeAddress;
115+
}
116+
117+
}

Diff for: HobbyDetails.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.hibernate.dto;
2+
3+
import javax.persistence.Embeddable;
4+
5+
/**
6+
* Class embedded in StudentDetails class
7+
*/
8+
@Embeddable
9+
public class HobbyDetails {
10+
11+
private String hobbyName;
12+
13+
private String hobbyType;
14+
15+
public String getHobbyName() {
16+
return hobbyName;
17+
}
18+
19+
public void setHobbyName(String hobbyName) {
20+
this.hobbyName = hobbyName;
21+
}
22+
23+
public String getHobbyType() {
24+
return hobbyType;
25+
}
26+
27+
public void setHobbyType(String hobbyType) {
28+
this.hobbyType = hobbyType;
29+
}
30+
31+
32+
33+
}

Diff for: LoginDetails.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.hibernate.dto;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Embeddable;
7+
8+
@Embeddable
9+
public class LoginDetails implements Serializable{
10+
11+
@Column(name="LOGIN_ID")
12+
private String loginId;
13+
14+
@Column(name="LOGIN_NAME")
15+
private String loginName;
16+
17+
public String getLoginId() {
18+
return loginId;
19+
}
20+
21+
public void setLoginId(String loginId) {
22+
this.loginId = loginId;
23+
}
24+
25+
public String getLoginName() {
26+
return loginName;
27+
}
28+
29+
public void setLoginName(String loginName) {
30+
this.loginName = loginName;
31+
}
32+
33+
}

Diff for: StudentDetails.java

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.hibernate.dto;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import javax.persistence.ElementCollection;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.GenerationType;
9+
import javax.persistence.Id;
10+
import javax.persistence.JoinColumn;
11+
import javax.persistence.JoinTable;
12+
13+
@Entity
14+
public class StudentDetails {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
private int studentId;
19+
20+
private String studentName;
21+
22+
/* Indicates hibernate that this is a java collection */
23+
@ElementCollection
24+
@JoinTable(name = "STUDENT_HOBBIES", // customised table name
25+
joinColumns = @JoinColumn(name = "STUDENT_ID")) //customised joining column name
26+
private Collection<HobbyDetails> hobbies = new ArrayList<HobbyDetails>();
27+
28+
public int getStudentId() {
29+
return studentId;
30+
}
31+
32+
public void setStudentId(int studentId) {
33+
this.studentId = studentId;
34+
}
35+
36+
public String getStudentName() {
37+
return studentName;
38+
}
39+
40+
public void setStudentName(String studentName) {
41+
this.studentName = studentName;
42+
}
43+
44+
public Collection<HobbyDetails> getHobbies() {
45+
return hobbies;
46+
}
47+
48+
public void setHobbies(Collection<HobbyDetails> hobbies) {
49+
this.hobbies = hobbies;
50+
}
51+
}

Diff for: UserDetails.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.hibernate.dto;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.EmbeddedId;
5+
import javax.persistence.Entity;
6+
7+
@Entity(name = "USER_DETAILS")
8+
public class UserDetails {
9+
10+
/* Embedded primary key */
11+
@EmbeddedId
12+
private LoginDetails loginDetails;
13+
14+
@Column(name = "USER_NAME")
15+
private String userName;
16+
17+
public String getUserName() {
18+
return userName;
19+
}
20+
21+
public LoginDetails getLoginDetails() {
22+
return loginDetails;
23+
}
24+
25+
public void setLoginDetails(LoginDetails loginDetails) {
26+
this.loginDetails = loginDetails;
27+
}
28+
29+
public void setUserName(String userName) {
30+
this.userName = userName;
31+
}
32+
33+
}

0 commit comments

Comments
 (0)