Skip to content

Commit

Permalink
Merge pull request #37 from Tave12st-Backend-Study/minyoungsong
Browse files Browse the repository at this point in the history
[Week3][Chapter6~7] Minyoung Song
  • Loading branch information
ankisile authored Sep 30, 2023
2 parents 171bd83 + b7c8b5d commit ee953f9
Show file tree
Hide file tree
Showing 23 changed files with 438 additions and 47 deletions.
11 changes: 11 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Album.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hellojpa;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("A")
public class Album extends Item{

private String artist;
}
44 changes: 44 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package hellojpa;

import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@MappedSuperclass
public abstract class BaseEntity {
private String createdBy;
private LocalDateTime createdDate;
private String lastModifiedBy;
private LocalDateTime lastModifiedDate;

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

public LocalDateTime getCreatedDate() {
return createdDate;
}

public void setCreatedDate(LocalDateTime createdDate) {
this.createdDate = createdDate;
}

public String getLastModifiedBy() {
return lastModifiedBy;
}

public void setLastModifiedBy(String lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}

public LocalDateTime getLastModifiedDate() {
return lastModifiedDate;
}

public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}
}
11 changes: 11 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hellojpa;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("B")
public class Book extends Item {
private String author;
private String isbn;
}
39 changes: 39 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package hellojpa;

import javax.persistence.*;

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Item {

@Id
@GeneratedValue
private Long id;

private String name;
private int price;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}
}
25 changes: 5 additions & 20 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/JpaMain.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package hellojpa;

import org.h2.mvstore.tx.Transaction;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;
import java.time.LocalDateTime;

public class JpaMain {
public static void main(String[] args) {
Expand All @@ -18,25 +16,12 @@ public static void main(String[] args) {
tx.begin();

try {
Team team = new Team();
team.setName("TeamA");
em.persist(team);

Member member = new Member();
member.setUsername("Member1");
member.setTeam(team);
em.persist(member);

em.flush();
em.clear();

Member findMember = em.find(Member.class, member.getId());
List<Member> members = findMember.getTeam().getMembers();

for(Member m : members){
System.out.println("m = "+m.getUsername());
}
member.setUsername("user1");
member.setCreatedBy("kim");
member.setCreatedDate(LocalDateTime.now());

em.persist(member);

tx.commit();
}
Expand Down
19 changes: 19 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Locker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package hellojpa;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Locker {

@Id
@GeneratedValue
private Long id;

private String name;

@OneToOne(mappedBy = "locker")
private Member member;
}
23 changes: 12 additions & 11 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Member.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package hellojpa;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Member {
public class Member extends BaseEntity{
@Id
@GeneratedValue
@Column(name = "MEMBER_ID")
Expand All @@ -12,13 +15,17 @@ public class Member {
@Column(name = "USERNAME")
private String username;

// @Column(name = "TEAM_ID")
// private Long teamId;

@ManyToOne
@JoinColumn(name = "TEAM_ID")
@JoinColumn(name = "TEAM_ID", insertable = false, updatable = false)
private Team team;

@OneToOne
@JoinColumn(name = "LOCKER_ID")
private Locker locker;

@OneToMany(mappedBy = "member")
private List<MemberProduct> memberProducts = new ArrayList<>();

public Long getId() {
return id;
}
Expand All @@ -35,11 +42,5 @@ public void setUsername(String username) {
this.username = username;
}

public Team getTeam() {
return team;
}

public void setTeam(Team team) {
this.team = team;
}
}
23 changes: 23 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/MemberProduct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hellojpa;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
public class MemberProduct {
@Id
@GeneratedValue
private Long id;

@ManyToOne
@JoinColumn(name = "MEMBER_ID")
private Member member;

@ManyToOne
@JoinColumn(name = "PRODCUT_ID")
private Product product;

private int count;
private int price;
private LocalDateTime orderDateTime;
}
27 changes: 27 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package hellojpa;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue("M")
public class Movie extends Item{
private String director;
private String actor;

public String getDirector() {
return director;
}

public void setDirector(String director) {
this.director = director;
}

public String getActor() {
return actor;
}

public void setActor(String actor) {
this.actor = actor;
}
}
33 changes: 33 additions & 0 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package hellojpa;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Product {
@Id
@GeneratedValue
private Long id;

private String name;

@OneToMany(mappedBy="product")
private List<MemberProduct> memberProducts = new ArrayList<>();

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
21 changes: 16 additions & 5 deletions 송민영/ex1-hello-jpa/src/main/java/hellojpa/Team.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package hellojpa;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Team {
public class Team extends BaseEntity{
@Id
@GeneratedValue
@Column(name = "TEAM_ID")
private Long id;
private String name;

@OneToMany
@JoinColumn(name = "TEAM_ID")
private List<Member> members = new ArrayList<>();

public Long getId() {
return id;
}
Expand All @@ -28,4 +31,12 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

public List<Member> getMembers() {
return members;
}

public void setMembers(List<Member> members) {
this.members = members;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jpabook;
package jpabook.jpashop;

import jpabook.jpashop.domain.Book;
import jpabook.jpashop.domain.Member;
import jpabook.jpashop.domain.Order;
import jpabook.jpashop.domain.OrderItem;
Expand All @@ -20,8 +21,11 @@ public static void main(String[] args) {
tx.begin();

try {
Order order = new Order();
order.addOrderItem(new OrderItem());
Book book = new Book();
book.setName("JPA");
book.setAuthor("j");

em.persist(book);
tx.commit();
}
catch (Exception e){
Expand Down
25 changes: 25 additions & 0 deletions 송민영/jpashop/src/main/java/jpabook/jpashop/domain/Album.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package jpabook.jpashop.domain;

import javax.persistence.Entity;

@Entity
public class Album extends Item{
private String artist;
private String etc;

public String getArtist() {
return artist;
}

public void setArtist(String artist) {
this.artist = artist;
}

public String getEtc() {
return etc;
}

public void setEtc(String etc) {
this.etc = etc;
}
}
Loading

0 comments on commit ee953f9

Please sign in to comment.