Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6d4867

Browse files
author
Rajeev Kumar Singh
committed
New Example
1 parent cb4cd3d commit e6d4867

File tree

8 files changed

+317
-204
lines changed

8 files changed

+317
-204
lines changed
Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
package com.example.hibernate;
22

3-
import com.example.hibernate.model.Driver;
4-
import com.example.hibernate.model.DrivingLicense;
5-
import com.example.hibernate.repository.DriverRepository;
6-
import com.example.hibernate.repository.DrivingLicenseRepository;
3+
import com.example.hibernate.model.Gender;
4+
import com.example.hibernate.model.User;
5+
import com.example.hibernate.model.UserProfile;
6+
import com.example.hibernate.repository.UserRepository;
7+
import com.example.hibernate.repository.UserProfileRepository;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.CommandLineRunner;
910
import org.springframework.boot.SpringApplication;
1011
import org.springframework.boot.autoconfigure.SpringBootApplication;
12+
import org.springframework.transaction.annotation.Transactional;
1113

1214
import java.util.Calendar;
1315

1416
@SpringBootApplication
1517
public class HibernateOneToOneDemoApplication implements CommandLineRunner {
1618

1719
@Autowired
18-
private DriverRepositorydriverRepository;
20+
private UserRepositoryuserRepository;
1921

2022
@Autowired
21-
private DrivingLicenseRepositorydrivingLicenseRepository;
23+
private UserProfileRepositoryuserProfileRepository;
2224

2325
public static void main(String[] args) {
2426
SpringApplication.run(HibernateOneToOneDemoApplication.class, args);
2527
}
2628

2729
@Override
2830
public void run(String... args) throws Exception {
29-
// Clean up database tables
30-
drivingLicenseRepository.deleteAllInBatch();
31-
driverRepository.deleteAllInBatch();
31+
// Clean up database tables
32+
userProfileRepository.deleteAllInBatch();
33+
userRepository.deleteAllInBatch();
3234

33-
//=========================================
35+
//=========================================
3436

35-
// Create a Driver instance
36-
Driverdriver = new Driver("Rajeev Kumar Singh", "rajeev@callicoder.com",
37-
"+91-9999999999");
37+
// Create a User instance
38+
Useruser = new User("Rajeev", "Singh", "rajeev@callicoder.com",
39+
"MY_SUPER_SECRET_PASSWORD");
3840

39-
// Create a Driving License instance
40-
Calendar issueDate = Calendar.getInstance();
41-
issueDate.set(2017, 7, 21);
41+
Calendar dateOfBirth = Calendar.getInstance();
42+
dateOfBirth.set(1992, 7, 21);
4243

43-
Calendar expiryDate = Calendar.getInstance();
44-
expiryDate.set(2027, 7, 21);
44+
// Create a UserProfile instance
45+
UserProfile userProfile = new UserProfile("+91-8197882053", Gender.MALE, dateOfBirth.getTime(),
46+
"747", "2nd Cross", "Golf View Road, Kodihalli", "Bangalore",
47+
"Karnataka", "India", "560008");
4548

46-
DrivingLicense drivingLicense = new DrivingLicense("MH-383321-323-8080",
47-
issueDate.getTime(), expiryDate.getTime());
4849

49-
// Set child reference(drivingLicense) in parent entity(driver)
50-
driver.setDrivingLicense(drivingLicense);
50+
// Set child reference(userProfile) in parent entity(user)
51+
user.setUserProfile(userProfile);
5152

52-
// Set parent reference(driver) in child entity(drivingLicense)
53-
drivingLicense.setDriver(driver);
53+
// Set parent reference(user) in child entity(userProfile)
54+
userProfile.setUser(user);
5455

55-
// Save Parent Reference (which will save the child as well)
56-
driverRepository.save(driver);
56+
// Save Parent Reference (which will save the child as well)
57+
userRepository.save(user);
5758

58-
//=========================================
59+
//=========================================
5960
}
61+
62+
6063
}

‎hibernate-one-to-one-demo/src/main/java/com/example/hibernate/model/Driver.java‎

Lines changed: 0 additions & 89 deletions
This file was deleted.

‎hibernate-one-to-one-demo/src/main/java/com/example/hibernate/model/DrivingLicense.java‎

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.hibernate.model;
2+
3+
/**
4+
* Created by rajeevkumarsingh on 29/11/17.
5+
*/
6+
public enum Gender {
7+
MALE,
8+
FEMALE
9+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.example.hibernate.model;
2+
3+
import org.hibernate.annotations.Fetch;
4+
import org.hibernate.annotations.FetchMode;
5+
import org.hibernate.validator.constraints.Email;
6+
7+
import javax.persistence.*;
8+
import javax.validation.constraints.NotNull;
9+
import javax.validation.constraints.Size;
10+
import java.io.Serializable;
11+
12+
/**
13+
* Created by rajeevkumarsingh on 20/11/17.
14+
*/
15+
@Entity
16+
@Table(name = "users")
17+
public class User implements Serializable {
18+
@Id
19+
@GeneratedValue(strategy = GenerationType.AUTO)
20+
private Long id;
21+
22+
@NotNull
23+
@Size(max = 65)
24+
@Column(name = "first_name")
25+
private String firstName;
26+
27+
@Size(max = 65)
28+
@Column(name = "last_name")
29+
private String lastName;
30+
31+
@NotNull
32+
@Email
33+
@Size(max = 100)
34+
@Column(unique = true)
35+
private String email;
36+
37+
@NotNull
38+
@Size(max = 128)
39+
private String password;
40+
41+
@OneToOne(fetch = FetchType.LAZY,
42+
cascade = CascadeType.ALL,
43+
mappedBy = "user")
44+
private UserProfile userProfile;
45+
46+
// Hibernate requires a no-arg constructor
47+
public User() {
48+
49+
}
50+
51+
public User(String firstName, String lastName, String email, String password) {
52+
this.firstName = firstName;
53+
this.lastName = lastName;
54+
this.email = email;
55+
this.password = password;
56+
}
57+
58+
public Long getId() {
59+
return id;
60+
}
61+
62+
public void setId(Long id) {
63+
this.id = id;
64+
}
65+
66+
public String getFirstName() {
67+
return firstName;
68+
}
69+
70+
public void setFirstName(String firstName) {
71+
this.firstName = firstName;
72+
}
73+
74+
public String getLastName() {
75+
return lastName;
76+
}
77+
78+
public void setLastName(String lastName) {
79+
this.lastName = lastName;
80+
}
81+
82+
public String getEmail() {
83+
return email;
84+
}
85+
86+
public void setEmail(String email) {
87+
this.email = email;
88+
}
89+
90+
public String getPassword() {
91+
return password;
92+
}
93+
94+
public void setPassword(String password) {
95+
this.password = password;
96+
}
97+
98+
99+
public UserProfile getUserProfile() {
100+
return userProfile;
101+
}
102+
103+
public void setUserProfile(UserProfile userProfile) {
104+
this.userProfile = userProfile;
105+
}
106+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /