-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Week10][Chap2] Feat : 예제 도메인 모델과 동작확(#111)
- Loading branch information
Showing
6 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
최송희/querydsl/src/main/generated/study/querydsl/entity/QMember.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
42 changes: 42 additions & 0 deletions
42
최송희/querydsl/src/main/generated/study/querydsl/entity/QTeam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
최송희/querydsl/src/main/java/study/querydsl/entity/Member.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
최송희/querydsl/src/main/java/study/querydsl/entity/Team.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} |
47 changes: 47 additions & 0 deletions
47
최송희/querydsl/src/test/java/study/querydsl/entity/MemberTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
} |