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 | 31 |
Tags
- 자바
- ciscopacket
- VLAN
- 네트워크
- Cisco
- 네트워크관리사
- cisco packet
- jsp연결
- Oracle
- 정처기필기
- 데이터베이스
- jsp
- w3school
- autoset
- javaee
- 리눅스
- 원형그래프
- 버추얼머신
- html
- php
- 이것이 자바다
- 정보처리기사
- NCS
- 오라클
- Java
- 라우터
- rinux
- ospf
- sql
- 참조타입
Archives
- Today
- Total
기록해! 정리해!
JPAJarThymeleaf-2 (양방향 연관관계 매핑) 본문
1. Board
package com.rubypaper.board;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import lombok.Data;
import lombok.ToString;
@Data
@ToString(exclude="member")
@Entity
@Table(name="board1019")
public class Board {
@Id
@GeneratedValue
private Long seq;
private String title;
private String content;
@Temporal(value=TemporalType.TIMESTAMP)
private Date createDate;
private Long cnt;
@ManyToOne
@JoinColumn(name="MEMBER_ID", nullable=false)
private Member member;
public void setMember(Member member) {
this.member = member;
member.getBoardList().add(this);
}
}
2. Member
package com.rubypaper.board;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Data;
import lombok.ToString;
@Data
@ToString(exclude="boardList")
@Entity
@Table(name="Member1019")
public class Member {
@Id
@Column(name="MEMBER_ID")
private String id;
private String password;
private String name;
private String role;
@OneToMany(mappedBy="member", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<Board> boardList = new ArrayList<Board>();
}
3. testRelationMapping
package com.rubypaper.test;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.rubypaper.board.Board;
import com.rubypaper.board.DynamicBoardRepository;
import com.rubypaper.board.DynamicMemberRepository;
import com.rubypaper.board.Member;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RelationMappingTest {
@Autowired
private DynamicBoardRepository boardRepo;
@Autowired
private DynamicMemberRepository memberRepo;
@Test
public void testRelationMapping() {
Member member1 =new Member();
member1.setId("member1");
member1.setName("소금빵");
member1.setPassword("member111");
member1.setRole("User");
// memberRepo.save(member1);
Member member2 =new Member();
member2.setId("member2");
member2.setName("앙버터");
member2.setPassword("member222");
member2.setRole("Admin");
// memberRepo.save(member2);
for(int i =1; i <=3; i++) {
Board board=new Board();
board.setMember(member1);
board.setTitle("소금빵이 등록한 게시글 " + i);
board.setContent("소금빵이 등록한 게시글 내용 " +i);
board.setCreateDate(new Date());
board.setCnt(1L);
// boardRepo.save(board);
}
memberRepo.save(member1);
for(int i =1; i <=4; i++) {
Board board=new Board();
board.setMember(member2);
board.setTitle("앙버터가 등록한 게시글 " + i);
board.setContent("앙버터가 등록한 게시글 내용 " +i);
board.setCreateDate(new Date());
board.setCnt(2L);
// boardRepo.save(board);
}
memberRepo.save(member2);
}
}
>>
1. Member 수정. (One to many)
(Eager: 즉시 / Lazy :지연)
2. Board (Many to One) 는 디폴트로 잡혀있지만 적어준다면 .
>> 똑같이 나옴
'SpringBoot' 카테고리의 다른 글
JPAJarThymeleaf-2 (양방향 연관관계 매핑) (0) | 2022.10.21 |
---|---|
JPAJarThymeleaf-2 (단방향 연관관계 매핑) (0) | 2022.10.20 |
JPAJarThymeleaf-2 (동적DSL) (0) | 2022.10.20 |
JPAJarThymeleaf-2 (Junit 연습문제) (0) | 2022.10.19 |
JPAJarThymeleaf-2 (Member) (0) | 2022.10.19 |
Comments