Skip to content

Commit

Permalink
[Week10][Chap2] Feat : 예제 도메인 모델과 동작확(#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
songhee1 committed Nov 24, 2023
1 parent 51f0d3a commit dace9d1
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 1 deletion.
2 changes: 1 addition & 1 deletion 최송희/querydsl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Member> {

private static final long serialVersionUID = -769675599L;

private static final PathInits INITS = PathInits.DIRECT2;

public static final QMember member = new QMember("member1");

public final NumberPath<Integer> age = createNumber("age", Integer.class);

public final NumberPath<Long> 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<? extends Member> 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<? extends Member> type, PathMetadata metadata, PathInits inits) {
super(type, metadata, inits);
this.team = inits.isInitialized("team") ? new QTeam(forProperty("team")) : null;
}

}

Original file line number Diff line number Diff line change
@@ -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<Team> {

private static final long serialVersionUID = -353664972L;

public static final QTeam team = new QTeam("team");

public final NumberPath<Long> id = createNumber("id", Long.class);

public final ListPath<Member, QMember> members = this.<Member, QMember>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<? extends Team> path) {
super(path.getType(), path.getMetadata());
}

public QTeam(PathMetadata metadata) {
super(Team.class, metadata);
}

}

42 changes: 42 additions & 0 deletions 최송희/querydsl/src/main/java/study/querydsl/entity/Member.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 28 additions & 0 deletions 최송희/querydsl/src/main/java/study/querydsl/entity/Team.java
Original file line number Diff line number Diff line change
@@ -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<Member> members = new ArrayList<>();

public Team(String name) {
this.name = name;
}



}
Original file line number Diff line number Diff line change
@@ -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<Member> 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());
}

}
}

0 comments on commit dace9d1

Please sign in to comment.