Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- spring batch 코드
- Spring Data JPA
- spring-mvc
- JPA mapping
- spring bean
- spring boot tomcat
- spring boot
- spring-webmvc #코드읽기
- JPA
- docker
- spring-webmvc
- 톰캣
- spring camp
- 세미나
- SuperTypeToken
- JUnit
- IntelliJ
- Spring Data REST
- tomcat
- Spring Batch
- ApplicationPidFileWriter
- Spring
- Data REST
- REST API
- static inner class
- spring jpa
- ORM
- spring pid
- batch
- 코드 리뷰
Archives
- Today
- Total
woniper
[JPA] @ManyToMany, N:M 관계 매핑 본문
- JPA란 무엇인가?
- JavaSE 환경에서 JPA 설정 및 CRUD
- JavaEE 환경(Spring)에서 JPA 설정 및 CRUD
- @OneToOne, 1:1 관계 매핑
- @OneToMany / @ManyToOne, 1:N / N:1 관계 매핑
- @ManyToMany, N:M 관계 매핑
- Entity 객체 생명주기(Lifecycle)와 Persistence Context
Table 관계도
Entity
@Entity(name = "tbl_product") public class Product { @Id @GeneratedValue private int productId; private String name; private int price; @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinTable(name = "tbl_product_category", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "category_id")) private List<Category> categories; public Product() { } public Product(String name, int price) { this.name = name; this.price = price; } // getter and setter public boolean addCategory(Category category) { if(categories == null) categories = new ArrayList<>(); return categories.add(category); } @Override public String toString() { return "Product{" + "productId=" + productId + ", name='" + name + '\'' + ", price=" + price + ", categories=" + categories + '}'; } } @Entity(name = "tbl_category") public class Category { @Id @GeneratedValue private int categoryId; private String name; @ManyToMany(fetch = FetchType.LAZY, mappedBy = "categories") private List<Product> products; public Category() { } public Category(String name, Listproducts) { this.name = name; this.products = products; } // getter and setter public boolean addProduct(Product product) { if(products == null) products = new ArrayList<>(); return products.add(product); } @Override public String toString() { return "Category{" + "name='" + name + '\'' + ", categoryId=" + categoryId + '}'; } }
TEST
public class ManyToManyTest { @Autowired private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; @Test public void manyToManyTest() { Category category = new Category(); category.setName("IT"); entityManager.persist(category); Product product = new Product(); product.setName("MacBook"); product.setPrice(1000); product.addCategory(category); category.addProduct(product); entityManager.persist(product); Assert.assertEquals(product.getProductId(), category.getProducts().get(0).getProductId()); } @Before public void setUp() throws Exception { entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); } @After public void after() { entityManager.getTransaction().commit(); entityManager.close(); } }
'Framework' 카테고리의 다른 글
[junit] junit 사용하기 (1) | 2015.05.21 |
---|---|
[JPA] Entity 객체 생명주기(Lifecycle)와 Persistence Context (4) | 2015.05.12 |
[JPA] @OneToMany / @ManyToOne, 1:N / N:1 관계 매핑 (0) | 2015.05.12 |
[JPA] @OneToOne, 1:1 관계 매핑 (0) | 2015.05.12 |
[JPA] JavaEE 환경(Spring)에서 JPA 설정 및 CRUD (0) | 2015.05.02 |
Comments