diff --git "a/\354\265\234\354\206\241\355\235\254/querydsl/build.gradle" "b/\354\265\234\354\206\241\355\235\254/querydsl/build.gradle" index a545db44..e25a6386 100644 --- "a/\354\265\234\354\206\241\355\235\254/querydsl/build.gradle" +++ "b/\354\265\234\354\206\241\355\235\254/querydsl/build.gradle" @@ -31,7 +31,7 @@ dependencies { annotationProcessor "jakarta.annotation:jakarta.annotation-api" annotationProcessor "jakarta.persistence:jakarta.persistence-api" - implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.8' + implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' diff --git "a/\354\265\234\354\206\241\355\235\254/querydsl/src/main/generated/study/querydsl/entity/QMember.java" "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/generated/study/querydsl/entity/QMember.java" new file mode 100644 index 00000000..8161353e --- /dev/null +++ "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/generated/study/querydsl/entity/QMember.java" @@ -0,0 +1,55 @@ +package study.querydsl.entity; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.PathInits; + + +/** + * QMember is a Querydsl query type for Member + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QMember extends EntityPathBase { + + private static final long serialVersionUID = -769675599L; + + private static final PathInits INITS = PathInits.DIRECT2; + + public static final QMember member = new QMember("member1"); + + public final NumberPath age = createNumber("age", Integer.class); + + public final NumberPath id = createNumber("id", Long.class); + + public final QTeam team; + + public final StringPath username = createString("username"); + + public QMember(String variable) { + this(Member.class, forVariable(variable), INITS); + } + + public QMember(Path path) { + this(path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS)); + } + + public QMember(PathMetadata metadata) { + this(metadata, PathInits.getFor(metadata, INITS)); + } + + public QMember(PathMetadata metadata, PathInits inits) { + this(Member.class, metadata, inits); + } + + public QMember(Class type, PathMetadata metadata, PathInits inits) { + super(type, metadata, inits); + this.team = inits.isInitialized("team") ? new QTeam(forProperty("team")) : null; + } + +} + diff --git "a/\354\265\234\354\206\241\355\235\254/querydsl/src/main/generated/study/querydsl/entity/QTeam.java" "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/generated/study/querydsl/entity/QTeam.java" new file mode 100644 index 00000000..f2ee8e22 --- /dev/null +++ "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/generated/study/querydsl/entity/QTeam.java" @@ -0,0 +1,42 @@ +package study.querydsl.entity; + +import static com.querydsl.core.types.PathMetadataFactory.*; + +import com.querydsl.core.types.dsl.*; + +import com.querydsl.core.types.PathMetadata; +import javax.annotation.processing.Generated; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.PathInits; + + +/** + * QTeam is a Querydsl query type for Team + */ +@Generated("com.querydsl.codegen.DefaultEntitySerializer") +public class QTeam extends EntityPathBase { + + private static final long serialVersionUID = -353664972L; + + public static final QTeam team = new QTeam("team"); + + public final NumberPath id = createNumber("id", Long.class); + + public final ListPath members = this.createList("members", Member.class, QMember.class, PathInits.DIRECT2); + + public final StringPath name = createString("name"); + + public QTeam(String variable) { + super(Team.class, forVariable(variable)); + } + + public QTeam(Path path) { + super(path.getType(), path.getMetadata()); + } + + public QTeam(PathMetadata metadata) { + super(Team.class, metadata); + } + +} + diff --git "a/\354\265\234\354\206\241\355\235\254/querydsl/src/main/java/study/querydsl/entity/Member.java" "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/java/study/querydsl/entity/Member.java" new file mode 100644 index 00000000..710f94f0 --- /dev/null +++ "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/java/study/querydsl/entity/Member.java" @@ -0,0 +1,42 @@ +package study.querydsl.entity; + +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter @Setter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@ToString(of = {"id", "username", "age"}) +public class Member { + @Id @GeneratedValue + @Column(name = "member_id") + private Long id; + private String username; + private int age; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "team_id") //외래키 컬럼 이름 + private Team team; + + public Member(String username, int age, Team team) { + this.username = username; + this.age = age; + this.team = team; + if(team!=null){ + changeTeam(team); + } + } + + public void changeTeam(Team team){ + this.team = team; + team.getMembers().add(this); + } + + public Member(String username, int age) { + this(username, age, null); + } + + public Member(String username) { + this(username, 0); + } +} diff --git "a/\354\265\234\354\206\241\355\235\254/querydsl/src/main/java/study/querydsl/entity/Team.java" "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/java/study/querydsl/entity/Team.java" new file mode 100644 index 00000000..8acceb92 --- /dev/null +++ "b/\354\265\234\354\206\241\355\235\254/querydsl/src/main/java/study/querydsl/entity/Team.java" @@ -0,0 +1,28 @@ +package study.querydsl.entity; + +import jakarta.persistence.*; +import lombok.*; + +import java.util.ArrayList; +import java.util.List; + +@Entity +@Getter @Setter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@ToString(of = {"id", "name"}) +public class Team { + @Id @GeneratedValue + @Column(name = "team_id") + private Long id; + private String name; + + @OneToMany(mappedBy = "team") //연관관계의 거울 + private List members = new ArrayList<>(); + + public Team(String name) { + this.name = name; + } + + + +} diff --git "a/\354\265\234\354\206\241\355\235\254/querydsl/src/test/java/study/querydsl/entity/MemberTest.java" "b/\354\265\234\354\206\241\355\235\254/querydsl/src/test/java/study/querydsl/entity/MemberTest.java" new file mode 100644 index 00000000..f634ea1a --- /dev/null +++ "b/\354\265\234\354\206\241\355\235\254/querydsl/src/test/java/study/querydsl/entity/MemberTest.java" @@ -0,0 +1,47 @@ +package study.querydsl.entity; + +import jakarta.persistence.EntityManager; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Commit; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +@SpringBootTest +@Transactional +class MemberTest { + @Autowired + EntityManager em; + + @Test + public void testEntity(){ + Team teamA = new Team("teamA"); + Team teamB = new Team("teamB"); + em.persist(teamA); + em.persist(teamB); + + Member member1 = new Member("member1", 10, teamA); + Member member2 = new Member("member2", 20, teamA); + Member member3 = new Member("member3", 30, teamB); + Member member4 = new Member("member4", 40, teamB); + em.persist(member1); + em.persist(member2); + em.persist(member3); + em.persist(member4); + + //초기화 + em.flush(); + em.clear(); + + List members = em.createQuery("select m from Member m", Member.class) + .getResultList(); + for (Member member : members) { + System.out.println("member = "+member); + System.out.println("-> member.team = "+member.getTeam()); + } + + } +} \ No newline at end of file