diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Album.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Album.java new file mode 100644 index 00000000..13ef6d6a --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Album.java @@ -0,0 +1,11 @@ +package com.hello.jpa.inheritance.ex00; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity +@DiscriminatorValue("A") +public class Album extends Item { + + private String artist; +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Book.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Book.java new file mode 100644 index 00000000..0f79dfbf --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Book.java @@ -0,0 +1,12 @@ +package com.hello.jpa.inheritance.ex00; + +import javax.persistence.DiscriminatorValue; +import javax.persistence.Entity; + +@Entity +@DiscriminatorValue("B") +public class Book extends Item { + + private String author; + private String isbn; +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Item.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Item.java new file mode 100644 index 00000000..e3968dd2 --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Item.java @@ -0,0 +1,41 @@ +package com.hello.jpa.inheritance.ex00; + +import javax.persistence.*; + +@Entity +//@Inheritance(strategy = InheritanceType.JOINED) +//@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@DiscriminatorColumn +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; + } +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/JpaMain.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/JpaMain.java new file mode 100644 index 00000000..e3d2db46 --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/JpaMain.java @@ -0,0 +1,42 @@ +package com.hello.jpa.inheritance.ex00; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.Persistence; + +public class JpaMain { + + public static void main(String[] args) { + EntityManagerFactory emf = Persistence.createEntityManagerFactory("hello"); + + EntityManager em = emf.createEntityManager(); + + EntityTransaction tx = em.getTransaction(); + tx.begin(); + + try { + Movie movie = new Movie(); + movie.setDirector("director"); + movie.setActor("actor"); + movie.setName("name"); + movie.setPrice(1000); + + em.persist(movie); + + em.flush(); + em.clear(); + + Movie findMovie = em.find(Movie.class, movie.getId()); + System.out.println("findMovie = " + findMovie); + + tx.commit(); + } catch (Exception e) { + tx.rollback(); + } finally { + em.close(); + } + + emf.close(); + } +} diff --git a/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Movie.java b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Movie.java new file mode 100644 index 00000000..05786098 --- /dev/null +++ b/hello-jpa/src/main/java/com/hello/jpa/inheritance/ex00/Movie.java @@ -0,0 +1,28 @@ +package com.hello.jpa.inheritance.ex00; + +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; + } +} diff --git a/hello-jpa/src/main/resources/META-INF/persistence.xml b/hello-jpa/src/main/resources/META-INF/persistence.xml index 2199864f..767bc14b 100644 --- a/hello-jpa/src/main/resources/META-INF/persistence.xml +++ b/hello-jpa/src/main/resources/META-INF/persistence.xml @@ -18,19 +18,24 @@ - com.hello.jpa.relationmapping.ex03.domain.Category - com.hello.jpa.relationmapping.ex03.domain.Delivery - com.hello.jpa.relationmapping.ex03.domain.Item - com.hello.jpa.relationmapping.ex03.domain.Member - com.hello.jpa.relationmapping.ex03.domain.Order - com.hello.jpa.relationmapping.ex03.domain.OrderItem + + + + + + + com.hello.jpa.inheritance.ex00.Item + com.hello.jpa.inheritance.ex00.Album + com.hello.jpa.inheritance.ex00.Book + com.hello.jpa.inheritance.ex00.Movie - + +