#30 jpa basic: inheritance
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -18,19 +18,24 @@
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex02.domain.Member</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex02.domain.Team</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex02.domain.Locker</class>-->
|
||||
<class>com.hello.jpa.relationmapping.ex03.domain.Category</class>
|
||||
<class>com.hello.jpa.relationmapping.ex03.domain.Delivery</class>
|
||||
<class>com.hello.jpa.relationmapping.ex03.domain.Item</class>
|
||||
<class>com.hello.jpa.relationmapping.ex03.domain.Member</class>
|
||||
<class>com.hello.jpa.relationmapping.ex03.domain.Order</class>
|
||||
<class>com.hello.jpa.relationmapping.ex03.domain.OrderItem</class>
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.Category</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.Delivery</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.Item</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.Member</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.Order</class>-->
|
||||
<!-- <class>com.hello.jpa.relationmapping.ex03.domain.OrderItem</class>-->
|
||||
<class>com.hello.jpa.inheritance.ex00.Item</class>
|
||||
<class>com.hello.jpa.inheritance.ex00.Album</class>
|
||||
<class>com.hello.jpa.inheritance.ex00.Book</class>
|
||||
<class>com.hello.jpa.inheritance.ex00.Movie</class>
|
||||
|
||||
<properties>
|
||||
<!-- 필수 속성 -->
|
||||
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
|
||||
<property name="javax.persistence.jdbc.user" value="sa"/>
|
||||
<property name="javax.persistence.jdbc.password" value=""/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:~/testdb"/>
|
||||
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/test"/>
|
||||
<!-- <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:~/testdb"/>-->
|
||||
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
|
||||
|
||||
<!-- 옵션 -->
|
||||
|
||||
Reference in New Issue
Block a user