move entities

This commit is contained in:
Krzysztof Majewski
2019-11-04 07:28:10 +01:00
parent 3453b7a18b
commit ddc86f5a47
5 changed files with 0 additions and 62 deletions
@@ -1,39 +0,0 @@
package com.baeldung.fetchMode;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.HashSet;
import java.util.Set;
@Entity
public class Customer {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "customer")
@Fetch(value = FetchMode.SELECT)
private Set<Order> orders = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
@@ -1,50 +0,0 @@
package com.baeldung.fetchMode;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Set;
@RestController
public class CustomerController {
private final CustomerRepository customerRepository;
private final OrderRepository orderRepository;
public CustomerController(CustomerRepository customerRepository, OrderRepository orderRepository) {
this.customerRepository = customerRepository;
this.orderRepository = orderRepository;
}
@GetMapping("test/{id}")
@Transactional
public Set<Order> getCustomerOrders(@PathVariable Long id) {
Customer customer = customerRepository.findById(id).get();
return customer.getOrders();
}
@GetMapping("save")
@Transactional
public Long saveNewCustomer() {
Customer customer = new Customer();
customer = customerRepository.save(customer);
Order order1 = orderRepository.save(new Order("order 1", customer));
Order order2 = orderRepository.save(new Order("order 2", customer));
Order order3 = orderRepository.save(new Order("order 3", customer));
Order order4 = orderRepository.save(new Order("order 4", customer));
Order order5 = orderRepository.save(new Order("order 5", customer));
customer.getOrders().add(order1);
customer.getOrders().add(order2);
customer.getOrders().add(order3);
customer.getOrders().add(order4);
customer.getOrders().add(order5);
Customer save = customerRepository.save(customer);
return save.getId();
}
}
@@ -1,6 +0,0 @@
package com.baeldung.fetchMode;
import org.springframework.data.jpa.repository.JpaRepository;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}
@@ -1,50 +0,0 @@
package com.baeldung.fetchMode;
import javax.persistence.*;
@Entity
public class Order {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;
public Order() {
}
public Order(String name, Customer customer) {
this.name = name;
this.customer = customer;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -1,6 +0,0 @@
package com.baeldung.fetchMode;
import org.springframework.data.jpa.repository.JpaRepository;
public interface OrderRepository extends JpaRepository<Order, Long> {
}