Skip to content

Commit 6ae750c

Browse files
Add files via upload
1 parent c143a28 commit 6ae750c

File tree

8 files changed

+299
-0
lines changed

8 files changed

+299
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.1.RELEASE</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>com.example</groupId>
13+
<artifactId>springboot-jpa-hibernate-onetomany-mapping</artifactId>
14+
<version>0.0.1-SNAPSHOT</version>
15+
<name>springboot-jpa-hibernate-onetomany-mapping</name>
16+
<description>Demo project for Spring Boot</description>
17+
18+
<properties>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-data-jpa</artifactId>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.h2database</groupId>
34+
<artifactId>h2</artifactId>
35+
<scope>runtime</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.example.demo;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.CommandLineRunner;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
11+
import com.example.demo.model.Employee;
12+
import com.example.demo.model.EmployeeContact;
13+
import com.example.demo.repository.EmployeeRepository;
14+
15+
@SpringBootApplication
16+
public class SpringbootJpaHibernateOnetoonemappingApplication implements CommandLineRunner {
17+
@Autowired
18+
EmployeeRepository employeeRepository;
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(SpringbootJpaHibernateOnetoonemappingApplication.class, args);
22+
}
23+
24+
@Override
25+
public void run(String... args) throws Exception {
26+
Employee employee = new Employee();
27+
employee.setName("sibin");
28+
employee.setSalary(12500);
29+
EmployeeContact contact1 = new EmployeeContact();
30+
contact1.setPhoneNo(111111111111l);
31+
EmployeeContact contact2 = new EmployeeContact();
32+
contact2.setPhoneNo(22222222222222l);
33+
Set<EmployeeContact> contacts = new HashSet<>();
34+
contacts.add(contact1);
35+
contacts.add(contact2);
36+
employee.setEmployeeContacts(contacts);
37+
employeeRepository.save(employee);
38+
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.demo.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestBody;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import com.example.demo.model.Employee;
15+
import com.example.demo.repository.EmployeeRepository;
16+
17+
@RestController
18+
@RequestMapping("employee")
19+
public class HomeController {
20+
@Autowired
21+
EmployeeRepository employeeRepository;
22+
23+
@GetMapping
24+
public List<Employee> findAll() {
25+
26+
return employeeRepository.findAll();
27+
28+
}
29+
30+
@PostMapping
31+
public Employee saveEmployee(@RequestBody Employee employee) {
32+
return employeeRepository.save(employee);
33+
34+
}
35+
36+
@GetMapping("/{employeeName}")
37+
public List<Employee> findByName(@PathVariable("employeeName") String employeeName) {
38+
return employeeRepository.findByName(employeeName);
39+
40+
}
41+
42+
@DeleteMapping("/{id}")
43+
public String deleteEmployee(@PathVariable("id") Integer id) {
44+
try {
45+
employeeRepository.deleteById(id);
46+
return "Deleted successfully";
47+
} catch (Exception e) {
48+
return "Failed to delete";
49+
}
50+
}
51+
52+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.example.demo.model;
2+
3+
import java.util.Set;
4+
5+
import javax.persistence.CascadeType;
6+
import javax.persistence.Column;
7+
import javax.persistence.Entity;
8+
import javax.persistence.FetchType;
9+
import javax.persistence.GeneratedValue;
10+
import javax.persistence.GenerationType;
11+
import javax.persistence.Id;
12+
import javax.persistence.JoinColumn;
13+
import javax.persistence.OneToMany;
14+
import javax.persistence.Table;
15+
16+
@Entity
17+
@Table(name = "employee")
18+
public class Employee {
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.AUTO)
21+
@Column(name = "id", unique = true, nullable = false)
22+
private Integer id;
23+
@Column(name = "name")
24+
private String name;
25+
private Integer salary;
26+
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
27+
@JoinColumn(name = "empId")
28+
private Set<EmployeeContact> employeeContacts;
29+
30+
public Set<EmployeeContact> getEmployeeContacts() {
31+
return employeeContacts;
32+
}
33+
34+
public void setEmployeeContacts(Set<EmployeeContact> employeeContacts) {
35+
this.employeeContacts = employeeContacts;
36+
}
37+
38+
public Employee() {
39+
}
40+
41+
public Integer getId() {
42+
return id;
43+
}
44+
45+
public Employee setId(Integer id) {
46+
this.id = id;
47+
return this;
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public Employee setName(String name) {
55+
this.name = name;
56+
return this;
57+
}
58+
59+
public Integer getSalary() {
60+
return salary;
61+
}
62+
63+
public Employee setSalary(Integer salary) {
64+
this.salary = salary;
65+
return this;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + ", employeeContacts=" + employeeContacts
71+
+ "]";
72+
}
73+
74+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.demo.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.Table;
9+
10+
@Entity
11+
@Table(name = "employee_contact")
12+
public class EmployeeContact {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
@Column(name = "id")
17+
private Integer id;
18+
private Long phoneNo;
19+
20+
public EmployeeContact() {
21+
}
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public EmployeeContact setId(Integer id) {
28+
this.id = id;
29+
return this;
30+
}
31+
32+
public Long getPhoneNo() {
33+
return phoneNo;
34+
}
35+
36+
public void setPhoneNo(Long phoneNo) {
37+
this.phoneNo = phoneNo;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return "EmployeeContact [id=" + id + ", phoneNo=" + phoneNo + "]";
43+
}
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.demo.repository;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
import com.example.demo.model.Employee;
9+
10+
@Repository
11+
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
12+
List<Employee> findByName(String name);
13+
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring.datasource.url=jdbc:h2:mem:knfdb
2+
spring.datasource.driverClassName=org.h2.Driver
3+
spring.datasource.username=sa
4+
spring.datasource.password=password
5+
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
6+
logging.level.org.hibernate.SQL=DEBUG
7+
logging.level.org.hibernate.type=TRACE
8+
spring.h2.console.enabled=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* package com.example.demo;
3+
*
4+
* import org.junit.jupiter.api.Test; import
5+
* org.springframework.boot.test.context.SpringBootTest;
6+
*
7+
* @SpringBootTest class SpringbootJpaHibernateOnetoonemappingApplicationTests {
8+
*
9+
* @Test void contextLoads() { }
10+
*
11+
* }
12+
*/

0 commit comments

Comments
 (0)