da-nyee
@danyee
Reviews Written
1
Average Rating
5.0
Posts
Q&A
type(i) = Book ์ ์ฌ์ฉ
์ด์ ๋ํ ๊ฒฐ๊ณผ๊ฐ ๊ถ๊ธํด์ '์์๊ด๊ณ ๋งคํ' ์์ ์งํํ๋ ์์ ๋ฅผ ํ ๋๋ก ์ง์ ์ค์ต์ ์งํํด๋ดค์ต๋๋ค. ๋ค์์ Main ํจ์ ์ฝ๋์ ๋๋ค. try { Book book = new Book(); book.setName("bookA"); book.setAuthor("babo"); em.persist(book); em.flush(); em.clear(); List result = em.createQuery("SELECT i FROM Item i WHERE TYPE(i) = Book", Item.class) .getResultList(); for (Item item : result) { Book book1 = (Book) item; System.out.println("book1.getName() = " + book1.getName()); System.out.println("book1.getAuthor() = " + book1.getAuthor()); } tx.commit(); } catch (Exception e) { tx.rollback(); e.printStackTrace(); } finally { em.close(); emf.close(); } } ๊ทธ๋ฐ ๋ค์ ์์ ์ ๋ต์ JOIN, SINGLE_TALBE, TABLE_PER_CLASS์ผ๋ก ๋ฐ๊ฟ๊ฐ๋ฉฐ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ์ดํด๋ดค์ต๋๋ค. (์ฐธ๊ณ ๋ก @DiscriminatorColumn ๊ฐ์ ๊ธฐ๋ณธ๊ฐ์ธ DTYPE, ๋๋จธ์ง ํ ์ด๋ธ์ @DiscriminatorValue ๊ฐ์ ์ ๊ธ์์ ๋๋ฌธ์๋ก ์ค์ ํ์์ต๋๋ค.) 1. JOINED LEFT JOIN์ ํ๋ฉด์ ITEM์ DTYPE์ด 'B'์ธ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฐ์ ์ฐพ๋ SELECT ์ฟผ๋ฆฌ๊ฐ ์คํ๋๋ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค. Hibernate: /* SELECT i FROM Item i WHERE TYPE(i) = Book */ select item0_.id as id2_5_, item0_.name as name3_5_, item0_.price as price4_5_, item0_1_.artist as artist1_1_, item0_2_.author as author1_2_, item0_2_.isbn as isbn2_2_, item0_3_.actor as actor1_9_, item0_3_.director as director2_9_, item0_.DTYPE as dtype1_5_ from Item item0_ left outer join Album item0_1_ on item0_.id=item0_1_.id left outer join Book item0_2_ on item0_.id=item0_2_.id left outer join Movie item0_3_ on item0_.id=item0_3_.id where item0_.DTYPE='B' book1.getName() = bookA book1.getAuthor() = babo 2. SINGLE_TABLE SINGLE_TABLE ์ ๋ต์ ITEM ํ ์ด๋ธ์ ๋ชจ๋ ๊ฐ์ด ์ ์ฅ๋๋ฏ๋ก ์กฐ์ธ์ ๊ณผ์ ์์ด ITEM์ DTYPE์ด 'B'์ธ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฐ์ ์ฐพ๋ SELECT ์ฟผ๋ฆฌ๊ฐ ๋ฐ์ํ๋ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค. Hibernate: /* SELECT i FROM Item i WHERE TYPE(i) = Book */ select item0_.id as id2_3_, item0_.name as name3_3_, item0_.price as price4_3_, item0_.artist as artist5_3_, item0_.author as author6_3_, item0_.isbn as isbn7_3_, item0_.actor as actor8_3_, item0_.director as director9_3_, item0_.DTYPE as dtype1_3_ from Item item0_ where item0_.DTYPE='B' book1.getName() = bookA book1.getAuthor() = babo 3. TABLE_PER_CLASS UNION์ ํตํ ์ด๋ง๋ฌด์ํ SELECT ์ฟผ๋ฆฌ๊ฐ ๋ฐ์ํ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค. ๊ทธ๋ฌ๋ ์์ ๋ ์ ๋ต๊ณผ๋ ๋ค๋ฅด๊ฒ DTYPE์ ํตํด ๋ง์กฑํ๋ ์กฐ๊ฑด์ ์ฐพ๋ ๊ฒ์ด ์๋๋ผ 'clazz_' ๋ผ๋ ์์ฑ์ ํตํด ๋ง์กฑํ๋ ์กฐ๊ฑด์ ์ฐพ๋ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค. ์ด clazz_๋ TABLE_PER_CLASS ์ ๋ต์ ์ฌ์ฉํ ๋๋ง ์์ฑ๋๋ ๊ฒ ๊ฐ์๊ณ , FROM ์ ์ ์๋ธ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ฉด Album, Book, Movie ํ ์ด๋ธ์ ๊ฐ๊ฐ clazz_์ ๊ฐ์ด 1, 2, 3์ผ๋ก ์ค์ ๋๋ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค. Hibernate: /* SELECT i FROM Item i WHERE TYPE(i) = Book */ select item0_.id as id1_5_, item0_.name as name2_5_, item0_.price as price3_5_, item0_.artist as artist1_1_, item0_.author as author1_2_, item0_.isbn as isbn2_2_, item0_.actor as actor1_9_, item0_.director as director2_9_, item0_.clazz_ as clazz_ from ( select id, name, price, artist, null as author, null as isbn, null as actor, null as director, 1 as clazz_ from Album union all select id, name, price, null as artist, author, isbn, null as actor, null as director, 2 as clazz_ from Book union all select id, name, price, null as artist, null as author, null as isbn, actor, director, 3 as clazz_ from Movie ) item0_ where item0_.clazz_=2 book1.getName() = bookA book1.getAuthor() = babo ๊ฒฐ๋ก JOINED, SINGLE_TABLE, TABLE_PER_CLASS ์ ๋ต ๋ชจ๋ ์์๊ด๊ณ์์ TYPE ์ฐ์ฐ์ ํตํด ํน์ ์์ ํด๋์ค๋ฅผ ์กฐํํ ์ ์์์ต๋๋ค. ๊ทธ๋ฌ๋ JOINED, SINGLE_TABLE ์ ๋ต์ DTYPE์ ์ฌ์ฉํ๊ณ , TABLE_PER_CLASS๋ clazz_ ๋ผ๋ ์์ฑ(?)์ ์ฌ์ฉํฉ๋๋ค. ์ค์ต ๋ด์ฉ์ด ๋ง์ด ๋ถ์กฑํ๊ณ ์ด์คํ๊ธฐ ๋๋ฌธ์ ์๋ชป๋ ์ ์ด๋ ๋ถ์กฑํ ์ ์์ผ๋ฉด ๋ง์ด ์กฐ์ธ ๋จ๊ฒจ์ฃผ์ธ์ ๐
- 0
- 2
- 333
Q&A
H2 ์คํ ์๋ฌ
์๋ ํ์ธ์, ๊ฐ์ฌ๋. ๊ด๋ฆฌ์ ๊ถํ์ผ๋ก ์คํํด๋ณด๋ ๋ค๋ฅธ ์๋ฌ ๋ฉ์ธ์ง๊ฐ ๋ํ๋์ ๊ตฌ๊ธ๋ง์ ๊ณ์ ํด๋ณธ ๊ฒฐ๊ณผ, java ๋ฒ์ ๊ณผ javac ๋ฒ์ ์ด ์์ดํด์ ๋ฐ์ํ๋ ๋ฌธ์ ์์ต๋๋ค..ใ ใ ใ ๋ฐฉ๊ธ ์ ํด๊ฒฐํ์ต๋๋ค! ๋์ ์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ใ ใ ๋จ์ ๊ฐ์๋ ์ด์ฌํ ์๊ฐํ๊ฒ ์ต๋๋ค !
- 0
- 3
- 448




