Skip to content

Commit bbfc7e6

Browse files
author
Rajeev Kumar Singh
committed
element-collection-demo
1 parent 3afc78f commit bbfc7e6

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ build/
2121
nbbuild/
2222
dist/
2323
nbdist/
24-
.nb-gradle/
24+
.nb-gradle/
25+
26+
.DS_Store
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,40 @@
11
package com.example.hibernateelementcollectiondemo;
22

3+
import com.example.hibernateelementcollectiondemo.model.Address;
4+
import com.example.hibernateelementcollectiondemo.model.User;
5+
import com.example.hibernateelementcollectiondemo.repository.UserRepository;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.CommandLineRunner;
38
import org.springframework.boot.SpringApplication;
49
import org.springframework.boot.autoconfigure.SpringBootApplication;
510

11+
import java.util.HashSet;
12+
import java.util.Set;
13+
614
@SpringBootApplication
7-
public class HibernateElementCollectionDemoApplication {
15+
public class HibernateElementCollectionDemoApplication implements CommandLineRunner {
16+
17+
@Autowired
18+
private UserRepository userRepository;
819

920
public static void main(String[] args) {
1021
SpringApplication.run(HibernateElementCollectionDemoApplication.class, args);
1122
}
23+
24+
@Override
25+
public void run(String... args) throws Exception {
26+
userRepository.deleteAllInBatch();
27+
28+
Set<String> phoneNumbers = new HashSet<>();
29+
phoneNumbers.add("+91-9999999999");
30+
phoneNumbers.add("+91-9898989898");
31+
32+
Set<Address> addresses = new HashSet<>();
33+
addresses.add(new Address("747", "Golf View Road", "Kodihalli", "Bangalore", "India", "560008"));
34+
addresses.add(new Address("Tower C", "Diamong District", "Domlur", "Bangalore", "India", "560008"));
35+
36+
User user = new User("Rajeev Kumar Singh", "rajeevs@flock.com", phoneNumbers, addresses);
37+
38+
userRepository.save(user);
39+
}
1240
}

hibernate-element-collection-demo/src/main/java/com/example/hibernateelementcollectiondemo/model/Address.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public class Address {
2525
@NotNull
2626
private String zipCode;
2727

28+
public Address() {
29+
30+
}
31+
32+
public Address(String addressLine1, String addressLine2, String city, String state, String country, String zipCode) {
33+
this.addressLine1 = addressLine1;
34+
this.addressLine2 = addressLine2;
35+
this.city = city;
36+
this.state = state;
37+
this.country = country;
38+
this.zipCode = zipCode;
39+
}
40+
2841
public String getAddressLine1() {
2942
return addressLine1;
3043
}

hibernate-element-collection-demo/src/main/java/com/example/hibernateelementcollectiondemo/model/User.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import javax.validation.Valid;
77
import javax.validation.constraints.NotNull;
88
import javax.validation.constraints.Size;
9+
import java.util.HashSet;
910
import java.util.Set;
1011

1112
/**
1213
* Created by rajeevkumarsingh on 22/11/17.
1314
*/
1415
@Entity
16+
@Table(name = "users")
1517
public class User {
1618
@Id
1719
@GeneratedValue(strategy = GenerationType.AUTO)
@@ -29,7 +31,7 @@ public class User {
2931
@ElementCollection
3032
@CollectionTable(name = "user_phone_numbers", joinColumns = @JoinColumn(name = "user_id"))
3133
@Column(name = "phone_number")
32-
private Set<String> phoneNumbers;
34+
private Set<String> phoneNumbers = new HashSet<>();
3335

3436
@Valid
3537
@ElementCollection
@@ -38,7 +40,19 @@ public class User {
3840
@AttributeOverride(name = "addressLine1", column = @Column(name = "address1")),
3941
@AttributeOverride(name = "addressLine2", column = @Column(name = "address2"))
4042
})
41-
private Set<Address> addresses;
43+
private Set<Address> addresses = new HashSet<>();
44+
45+
46+
public User() {
47+
48+
}
49+
50+
public User(String name, String email, Set<String> phoneNumbers, Set<Address> addresses) {
51+
this.name = name;
52+
this.email = email;
53+
this.phoneNumbers = phoneNumbers;
54+
this.addresses = addresses;
55+
}
4256

4357
public Long getId() {
4458
return id;

hibernate-element-collection-demo/src/main/java/com/example/hibernateelementcollectiondemo/repository/UserRepository.java

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

33
import com.example.hibernateelementcollectiondemo.model.User;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
56

67
/**
78
* Created by rajeevkumarsingh on 22/11/17.
89
*/
10+
@Repository
911
public interface UserRepository extends JpaRepository<User, Long> {
1012

1113
}
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_element_collection_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)