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
- jsp
- html
- autoset
- php
- Java
- 네트워크
- 리눅스
- 라우터
- NCS
- rinux
- VLAN
- 원형그래프
- Oracle
- javaee
- 이것이 자바다
- sql
- Cisco
- ciscopacket
- 참조타입
- 버추얼머신
- 오라클
- 네트워크관리사
- 정처기필기
- 자바
- 정보처리기사
- jsp연결
- 데이터베이스
- ospf
- cisco packet
- w3school
Archives
- Today
- Total
기록해! 정리해!
JPAJarThymeleaf-2 (양방향 연관관계 매핑) 본문
1. RelationMappingTest
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 test() {
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);
Member member3 = new Member();
member3.setId("member3");
member3.setName("영심이");
member3.setPassword("member333");
member3.setRole("User");
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);
}
for ( int i=1; i <=3; 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(member1);
memberRepo.save(member2);
memberRepo.save(member3);
}
}
2. Board
package com.rubypaper.board;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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="board1020")
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 (fetch=FetchType.EAGER)
@JoinColumn(name="MEMBER_ID", nullable=false)
private Member member;
public void setMember(Member member) {
this.member = member;
member.getBoardList().add(this);
}
}
3. Member
package com.rubypaper.board;
import java.util.ArrayList;
import java.util.Date;
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.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString(exclude="boardList")
@Entity
@Table(name="Member1020")
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.LAZY, cascade=CascadeType.ALL)
private List<Board> boardList = new ArrayList<Board>();
}
4. RelationMappingTest3
package com.rubypaper.test;
import java.util.List;
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 RelationMappingTest3 {
@Autowired
private DynamicBoardRepository boardRepo;
@Autowired
private DynamicMemberRepository memberRepo;
@Test
public void testRelationMapping() {
List<Member> memberList =(List<Member>) memberRepo.findAll();
for(Member m :memberList) {
System.out.println(m.getBoardList().toString());
}
Member member=memberRepo.findById("member2").get();
System.out.println(member.getName());
List<Board> boardList=member.getBoardList();
for(Board b:boardList) {
System.out.println(b.toString());
}
}
}
'SpringBoot' 카테고리의 다른 글
JPAJarThymeleaf-2 (양방향 연관관계 매핑) (0) | 2022.10.20 |
---|---|
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