Skip to content

Commit a64bae6

Browse files
author
Krzysztof Majewski
committed
BAEL-2513
1 parent 749eff7 commit a64bae6

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.fetchMode;
2+
3+
import org.hibernate.annotations.Fetch;
4+
import org.hibernate.annotations.FetchMode;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
import javax.persistence.OneToMany;
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
@Entity
14+
public class Customer {
15+
16+
@Id
17+
@GeneratedValue
18+
private Long id;
19+
20+
@OneToMany(mappedBy = "customer")
21+
@Fetch(value = FetchMode.SELECT)
22+
private Set<Order> orders = new HashSet<>();
23+
24+
public Long getId() {
25+
return id;
26+
}
27+
28+
public void setId(Long id) {
29+
this.id = id;
30+
}
31+
32+
public Set<Order> getOrders() {
33+
return orders;
34+
}
35+
36+
public void setOrders(Set<Order> orders) {
37+
this.orders = orders;
38+
}
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.fetchMode;
2+
3+
import org.springframework.transaction.annotation.Transactional;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import java.util.Set;
9+
10+
@RestController
11+
public class CustomerController {
12+
13+
private final CustomerRepository customerRepository;
14+
private final OrderRepository orderRepository;
15+
16+
public CustomerController(CustomerRepository customerRepository, OrderRepository orderRepository) {
17+
this.customerRepository = customerRepository;
18+
this.orderRepository = orderRepository;
19+
}
20+
21+
@GetMapping("test/{id}")
22+
@Transactional
23+
public Set<Order> getCustomerOrders(@PathVariable Long id) {
24+
Customer customer = customerRepository.findById(id).get();
25+
return customer.getOrders();
26+
}
27+
28+
@GetMapping("save")
29+
@Transactional
30+
public Long saveNewCustomer() {
31+
Customer customer = new Customer();
32+
customer = customerRepository.save(customer);
33+
34+
Order order1 = orderRepository.save(new Order("order 1", customer));
35+
Order order2 = orderRepository.save(new Order("order 2", customer));
36+
Order order3 = orderRepository.save(new Order("order 3", customer));
37+
Order order4 = orderRepository.save(new Order("order 4", customer));
38+
Order order5 = orderRepository.save(new Order("order 5", customer));
39+
40+
customer.getOrders().add(order1);
41+
customer.getOrders().add(order2);
42+
customer.getOrders().add(order3);
43+
customer.getOrders().add(order4);
44+
customer.getOrders().add(order5);
45+
46+
Customer save = customerRepository.save(customer);
47+
return save.getId();
48+
}
49+
50+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.fetchMode;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface CustomerRepository extends JpaRepository<Customer, Long> {
6+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.fetchMode;
2+
3+
import javax.persistence.*;
4+
5+
@Entity
6+
public class Order {
7+
8+
@Id
9+
@GeneratedValue
10+
private Long id;
11+
12+
private String name;
13+
14+
@ManyToOne
15+
@JoinColumn(name = "customer_id")
16+
private Customer customer;
17+
18+
public Order() {
19+
20+
}
21+
22+
public Order(String name, Customer customer) {
23+
this.name = name;
24+
this.customer = customer;
25+
}
26+
27+
public Long getId() {
28+
return id;
29+
}
30+
31+
public void setId(Long id) {
32+
this.id = id;
33+
}
34+
35+
public Customer getCustomer() {
36+
return customer;
37+
}
38+
39+
public void setCustomer(Customer customer) {
40+
this.customer = customer;
41+
}
42+
43+
public String getName() {
44+
return name;
45+
}
46+
47+
public void setName(String name) {
48+
this.name = name;
49+
}
50+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.fetchMode;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface OrderRepository extends JpaRepository<Order, Long> {
6+
}

0 commit comments

Comments
 (0)