10___III_081_ORM_ExAnnot_2010_16.ppt
- Количество слайдов: 42
ORM Приклад із використанням анотацій ORM - Example
pom. xml (1/2)
pom. xml (2/2)
Клас Author (1/2) package com. ttp. Hibernate. Example; import java. io. Serializable; import javax. persistence. *; @Entity @Table(name = "TB_AUTHOR") public class Author implements Serializable { private static final long serial. Version. UID = 1 L; @Id @Generated. Value(strategy = Generation. Type. IDENTITY) @Column(name = "AUTHOR_ID") private Long id ; private String name; @Many. To. One @Join. Column(name = "Dep") private Department department; ORM - Example
Клас Author (1/2) package com. ttp. Hibernate. Example; import java. io. Serializable; import javax. persistence. *; @Entity @Table(name = "TB_AUTHOR") public class Author implements Serializable { private static final long serial. Version. UID = 1 L; @Id @Generated. Value(strategy = Generation. Type. IDENTITY) @Column(name = "AUTHOR_ID") private Long id ; private String name; @Many. To. One @Join. Column(name = "Dep") private Department department; ORM - Example
Клас Author (2/2) public Author() {} public Author(String name, Department department) { this. name = name; this. department = department; } public Author(String name) { this. name = name; } public String to. String() { return "Author < id = " + id + ", name = " + name + ", department = " + department. get. Title() + " >"; }. . . // get-, set} ORM - Example
Клас Department (1/3) package com. ttp. Hibernate. Example; import java. io. Serializable; import java. util. Hash. Set; import java. util. Set; import javax. persistence. *; import javax. persistence. Cascade. Type; @Entity @Table(name = "TB_Department") public class Department implements Serializable { private static final long serial. Version. UID = 1 L; @Id @Generated. Value(strategy = Generation. Type. IDENTITY) @Column(name = "ID_Dep", unique = true, nullable = false) private Long id; private String title; ORM - Example
Клас Department (2/3) public Department(){} public Department( String title) { this. set. Title(title); } @One. To. Many(mapped. By = "department", orphan. Removal = true, cascade = { Cascade. Type. ALL }) private Set
Клас Department (3/3) public String to. String() { String res = new String(); res = "Department: name = " + get. Title() + ", authors : << "; for (Author next : authors) { res = res + next. get. Name() + " | "; } ; res = res + ">>. "; return res; }. . . // get-, set} ORM - Example
Клас Hibernate. Util public class Hibernate. Util { private static final Session. Factory session. Factory = build. Session. Factory(); private static Session. Factory build. Session. Factory() { try { return new Configuration(). configure(). build. Session. Factory(); } catch (Throwable ex) { System. err. println( "Initial Session. Factory creation failed. " + ex); throw new Exception. Initializer. Error(ex); } } public static Session. Factory get. Session. Factory() { return session. Factory; } public static void shutdown() { // Close caches and connection pools get. Session. Factory(). close(); } ORM - Example }
Клас Test. NG 1 , метод my_print package com. ttp. Hibernate. Example; import org. testng. annotations. Test; import org. testng. annotations. Before. Class; import org. testng. annotations. Before. Method; import java. util. Hash. Set; import java. util. List; import org. hibernate. Query; import org. hibernate. Session; import org. testng. annotations. After. Class; import org. testng. annotations. After. Method; public class Test. NG 1 { Session session; void my_print(Integer j){ System. out. println("================="); System. out. println("====== " + j. to. String() + " ===="); System. out. println("================="); Query q = session. create. Query("From Author"); List list = q. list(); for (int i = 0; i < list. size(); ) { Author aut = (Author) list. get(i); System. out. println(aut. to. String()); i = i + 1; } ORM - Example }
Клас Test. NG 1 , основні методи (1/2) @Before. Class public void before. Class() { session = Hibernate. Util. get. Session. Factory(). open. Session(); } @After. Class public void after. Class() { session. close(); } @Before. Method public void before. Method() { session. begin. Transaction(); } @After. Method public void after. Method () { session. get. Transaction(). commit(); } ORM - Example
Клас Test. NG 1 , основні методи (2/2) @Test public void test 1() {. . . session. save(dep. TTP); // cascade session. save(dep. MI); // cascade public class Department … my_print(1); @One. To. Many(mapped. By = "department", } orphan. Removal = true, @Test(depends. On. Methods = { "test 1"}) cascade = { Cascade. Type. ALL }) public void test 2() { private Set
Результати тестування ORM - Example
Клас Test. NG 1 , метод test 1 @Test public void test 1() { Department dep. TTP = new Department("TTP"); Department dep. MI = new Department("MI"); dep. TTP. set. Authors(new Hash. Set
Клас Test. NG 1 , метод test 2 @Test(depends. On. Methods = { "test 1"}) public void test 2() { Query q = session. create. Query( "From Department a where a. title = : dep. Title"); q. set. Parameter("dep. Title", "MI"); Department dep. MI = (Department)q. list(). get(0); session. delete(dep. MI); my_print(2); } public class Department. . . @One. To. Many(mapped. By = "department", orphan. Removal = true, cascade = { Cascade. Type. ALL }) ORM - Example private Set
Клас Test. NG 1 , метод test 3 @Test(depends. On. Methods = { "test 1", "test 2" }) public void test 3() { Query q 3 = session. create. Query( "From Author a where a. name = : author. Name"); q 3. set. Parameter("author. Name", "Aspirant. TTP"); Author asp = (Author)q 3. list(). get(0); Query q 4 = session. create. Query( "From Department d where d. title = : dep. Title"); q 4. set. Parameter("dep. Title", "TTP"); Department dep_TTP = (Department)q 4. list(). get(0); dep_TTP. get. Authors(). remove(asp); session. save. Or. Update(dep_TTP ); // delete-orphan my_print(3); } public class Department. . . @One. To. Many(mapped. By = "department", orphan. Removal = true, cascade = { Cascade. Type. ALL }) ORM - Example private Set
Файл hibernate. cfg. xml (1/2)
• Одна таблиця на ієрархію класів (table-per-class-hierarchy) — “швейцарський сир”; Повний приклад на використання анотацій ORM - Example
Повний приклад на використання анотацій ORM - Example
Повний приклад. До генерації таблиць ORM - Example
Повний приклад. Клас Article (1/2) @Entity @Table(name = "TArticle") public class Article implements Serializable { @Id @Generated. Value(strategy = Generation. Type. IDENTITY) private Long id; @Many. To. Many (mapped. By = "articles") private Set
Повний приклад. Клас Article (2/2) public String to. String() { String res = new String(); res = "Article, назва статті : " + get. Title()+ ". Автори : ( "; for (Author next : authors) { res = res + next. get. Name(); }; res = res + "). "; return res; }. . . // get-, set} ORM - Example
Повний приклад. Клас Author (1/2) @Entity @Inheritance(strategy = Inheritance. Type. SINGLE_TABLE) @Discriminator. Column(name = "author_type", discriminator. Type = Discriminator. Type. STRING) @Discriminator. Value("Author. Discr") @Table(name = "TAuthor") public class Author implements Serializable { @Id @Generated. Value(strategy = Generation. Type. IDENTITY) private Long id; @Many. To. Many(cascade = { Cascade. Type. ALL }) @Join. Table(name = "T_AUTHOR_ARTICLE", join. Columns = @Join. Column(name = "AUTHOR_ID"), inverse. Join. Columns = @Join. Column(name = "ARTICLE_ID") ) protected Set
"; }. . . // get-, set- } ORM - Example
Повний приклад. Клас Department (1/2) @Entity @Table(name = "TDepartment") public class Department implements Serializable { @Id @Generated. Value(strategy = Generation. Type. IDENTITY) @Column(name = "DEP_ID") private Long id; @One. To. Many(cascade = { Cascade. Type. ALL }) @Join. Column(name = "DEP") private Set
Повний приклад. Клас Department (2/2) public Department() { } public Department(String title) { this. set. Title(title); } @Override public String to. String() { return "Department
Повний приклад. Клас Prof @Entity @Discriminator. Value("Prof. Discr") public class Prof extends Author implements Serializable { @One. To. Many(cascade = { Cascade. Type. ALL }, mapped. By = "prof") private Set
Повний приклад. Клас Stud @Entity @Discriminator. Value("Stud. Discr") public class Stud extends Author implements Serializable { @One. To. One @Join. Column(name = "TEMA_Dip") private Tema. Dipl tema; private int kurs; public Stud() {} public Stud(String name, Department department, int kurs) { this. name = name; this. kurs = kurs; this. department = department; } public String to. String() { return ("Stud " + super. to. String() + ". Курс : " + kurs + ", тема ДР: " + tema. to. String()); }. . . // get-, set. ORM - Example }
Повний приклад. Клас Tema. Dipl (1/2) @Entity @Table(name="TTema. Dipl") public class Tema. Dipl implements Serializable{ @Id @Generated. Value(strategy=Generation. Type. IDENTITY) @Column(name="Tema_ID") private Long id; @One. To. One (mapped. By="tema") private Stud stud; @Many. To. One (cascade={Cascade. Type. ALL}) @Join. Column(name = "PROF") private Prof prof; private String title; ORM - Example
Повний приклад. Клас Tema. Dipl (2/2) public Tema. Dipl(){} public Tema. Dipl(String title, Prof prof, Stud stud){ this. title = title; this. prof = prof; this. stud = stud; } public String to. String(){ return( "Tema. Dipl. Назва теми : " + get. Title() + ", керівник "+ prof. get. Name()+ ", виконавець "+stud. get. Name()+ ". " ); }. . . // get-, set} ORM - Example
Повний приклад. Клас Hibernate. Util public class Hibernate. Util { private static final Session. Factory session. Factory = build. Session. Factory(); private static Session. Factory build. Session. Factory() { try { return new Configuration(). configure(). build. Session. Factory(); } catch (Throwable ex) { System. err. println( "Initial Session. Factory creation failed. " + ex); throw new Exception. Initializer. Error(ex); } } public static Session. Factory get. Session. Factory() { return session. Factory; } public static void shutdown() { // Close caches and connection pools get. Session. Factory(). close(); } ORM - Example }
Повний приклад. Клас App. Test (1/7) public class App. Test { @Test public void test 1() { Session session = Hibernate. Util. get. Session. Factory(). open. Session(); session. begin. Transaction(); Department dep. TTP = new Department("TTP"); Department dep. MI = new Department("MI"); dep. TTP. set. Authors(new Hash. Set
Повний приклад. Клас App. Test (2/7) dep. TTP. get. Authors(). add(dotsent. TTP); dep. TTP. get. Authors(). add(prof. TTP); dep. TTP. get. Authors(). add(stud 2 TTP); dep. MI. get. Authors(). add(dotsent. MI); dep. MI. get. Authors(). add(prof. MI); dep. MI. get. Authors(). add(stud 2 MI); Tema. Dipl dip 1 dip 2 dip 3 dip 4 = = new new Tema. Dipl("Dip 1", Tema. Dipl("Dip 2", Tema. Dipl("Dip 3", Tema. Dipl("Dip 4", dotsent. TTP, stud. TTP); prof. TTP, stud 2 TTP); prof. MI, stud. MI); prof. MI, stud 2 MI); dotsent. TTP. set. Tems(new Hash. Set
Повний приклад. Клас App. Test (3/7) dotsent. MI. set. Tems(new Hash. Set
Повний приклад. Клас App. Test (4/7) Set
Повний приклад. Клас App. Test (5/7) Set
Повний приклад. Клас App. Test (6/7) System. out. println("--------2 ---------"); Query q 2 = session. create. Query("From Author"); List list 2 = q 2. list(); for (int i = 0; i < list 2. size(); ) { Author st = (Author) list 2. get(i); System. out. println(st. get. Name() + " , " + st. articles. to. String()); i = i + 1; }; ORM - Example
Повний приклад. Клас App. Test (7/7) System. out. println("--------3 ---------"); Query q 3 = session. create. Query("From Department"); List list 3 = q 3. list(); for (int i = 0; i < list 3. size(); ) { Department dep = (Department) list 3. get(i); System. out. println(dep. get. Title() + " , " + dep. get. Authors(). to. String()); i = i + 1; }; session. get. Transaction(). commit(); session. close(); } } ORM - Example
Повний приклад. Файл hibernate. cfg. xml (1/2) xml version="1. 0" encoding="utf-8"? >
Повний приклад. Файл hibernate. cfg. xml (2/2)