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 78a0877

Browse files
author
Rajeev Kumar Singh
committed
hibernate-embeddable-demo
1 parent bbfc7e6 commit 78a0877

File tree

6 files changed

+260
-1
lines changed

6 files changed

+260
-1
lines changed
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
package com.example.hibernateembeddabledemo;
22

3+
import com.example.hibernateembeddabledemo.model.Address;
4+
import com.example.hibernateembeddabledemo.model.Name;
5+
import com.example.hibernateembeddabledemo.model.User;
6+
import com.example.hibernateembeddabledemo.repository.UserRepository;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.CommandLineRunner;
39
import org.springframework.boot.SpringApplication;
410
import org.springframework.boot.autoconfigure.SpringBootApplication;
511

612
@SpringBootApplication
7-
public class HibernateEmbeddableDemoApplication {
13+
public class HibernateEmbeddableDemoApplication implements CommandLineRunner {
14+
15+
@Autowired
16+
private UserRepository userRepository;
817

918
public static void main(String[] args) {
1019
SpringApplication.run(HibernateEmbeddableDemoApplication.class, args);
1120
}
21+
22+
@Override
23+
public void run(String... args) throws Exception {
24+
userRepository.deleteAllInBatch();
25+
26+
Name name = new Name("Rajeev", "Kumar", "Singh");
27+
Address address = new Address("747", "Golf View Road", "Bangalore", "Karnataka", "India", "560008");
28+
User user = new User(name, "rajeev@callicoder.com", address);
29+
30+
userRepository.save(user);
31+
}
1232
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.example.hibernateembeddabledemo.model;
2+
3+
import javax.persistence.Embeddable;
4+
import javax.validation.constraints.NotNull;
5+
6+
/**
7+
* Created by rajeevkumarsingh on 22/11/17.
8+
*/
9+
@Embeddable
10+
public class Address {
11+
@NotNull
12+
private String houseNumber;
13+
14+
@NotNull
15+
private String street;
16+
17+
@NotNull
18+
private String city;
19+
20+
@NotNull
21+
private String state;
22+
23+
@NotNull
24+
private String country;
25+
26+
@NotNull
27+
private String zipCode;
28+
29+
30+
public Address() {
31+
32+
}
33+
34+
public Address(String houseNumber, String street, String city, String state, String country, String zipCode) {
35+
this.houseNumber = houseNumber;
36+
this.street = street;
37+
this.city = city;
38+
this.state = state;
39+
this.country = country;
40+
this.zipCode = zipCode;
41+
}
42+
43+
public String getHouseNumber() {
44+
return houseNumber;
45+
}
46+
47+
public void setHouseNumber(String houseNumber) {
48+
this.houseNumber = houseNumber;
49+
}
50+
51+
public String getStreet() {
52+
return street;
53+
}
54+
55+
public void setStreet(String street) {
56+
this.street = street;
57+
}
58+
59+
public String getCity() {
60+
return city;
61+
}
62+
63+
public void setCity(String city) {
64+
this.city = city;
65+
}
66+
67+
public String getState() {
68+
return state;
69+
}
70+
71+
public void setState(String state) {
72+
this.state = state;
73+
}
74+
75+
public String getCountry() {
76+
return country;
77+
}
78+
79+
public void setCountry(String country) {
80+
this.country = country;
81+
}
82+
83+
public String getZipCode() {
84+
return zipCode;
85+
}
86+
87+
public void setZipCode(String zipCode) {
88+
this.zipCode = zipCode;
89+
}
90+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example.hibernateembeddabledemo.model;
2+
3+
import javax.persistence.Embeddable;
4+
import javax.validation.constraints.NotNull;
5+
6+
/**
7+
* Created by rajeevkumarsingh on 22/11/17.
8+
*/
9+
@Embeddable
10+
public class Name {
11+
@NotNull
12+
private String firstName;
13+
private String middleName;
14+
private String lastName;
15+
16+
public Name() {
17+
18+
}
19+
20+
public Name(String firstName, String middleName, String lastName) {
21+
this.firstName = firstName;
22+
this.middleName = middleName;
23+
this.lastName = lastName;
24+
}
25+
26+
public String getFirstName() {
27+
return firstName;
28+
}
29+
30+
public void setFirstName(String firstName) {
31+
this.firstName = firstName;
32+
}
33+
34+
public String getMiddleName() {
35+
return middleName;
36+
}
37+
38+
public void setMiddleName(String middleName) {
39+
this.middleName = middleName;
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: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.example.hibernateembeddabledemo.model;
2+
3+
import org.hibernate.validator.constraints.Email;
4+
5+
import javax.persistence.*;
6+
import javax.validation.Valid;
7+
import javax.validation.constraints.NotNull;
8+
9+
/**
10+
* Created by rajeevkumarsingh on 22/11/17.
11+
*/
12+
@Entity
13+
@Table(name = "users")
14+
public class User {
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Long id;
18+
19+
@Valid
20+
@Embedded
21+
private Name name;
22+
23+
@NotNull
24+
@Email
25+
private String email;
26+
27+
@Valid
28+
@Embedded
29+
private Address address;
30+
31+
public User() {
32+
33+
}
34+
35+
public User(Name name, String email, Address address) {
36+
this.name = name;
37+
this.email = email;
38+
this.address = address;
39+
}
40+
41+
public Long getId() {
42+
return id;
43+
}
44+
45+
public void setId(Long id) {
46+
this.id = id;
47+
}
48+
49+
public Name getName() {
50+
return name;
51+
}
52+
53+
public void setName(Name name) {
54+
this.name = name;
55+
}
56+
57+
public String getEmail() {
58+
return email;
59+
}
60+
61+
public void setEmail(String email) {
62+
this.email = email;
63+
}
64+
65+
public Address getAddress() {
66+
return address;
67+
}
68+
69+
public void setAddress(Address address) {
70+
this.address = address;
71+
}
72+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.hibernateembeddabledemo.repository;
2+
3+
import com.example.hibernateembeddabledemo.model.User;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
/**
8+
* Created by rajeevkumarsingh on 22/11/17.
9+
*/
10+
@Repository
11+
public interface UserRepository extends JpaRepository<User, Long> {
12+
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
2+
spring.datasource.url=jdbc:mysql://localhost:3306/hibernate_embeddable_demo?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
3+
spring.datasource.username=root
4+
spring.datasource.password=root
5+
6+
# Hibernate
7+
8+
# The SQL dialect makes Hibernate generate better SQL for the chosen database
9+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
10+
11+
# Hibernate ddl auto (create, create-drop, validate, update)
12+
spring.jpa.hibernate.ddl-auto = update
13+
14+
logging.level.org.hibernate.SQL=DEBUG
15+
logging.level.org.hibernate.type=TRACE

0 commit comments

Comments
(0)

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