Скачать презентацию ORM Приклад із використанням анотацій ORM — Example Скачать презентацию ORM Приклад із використанням анотацій ORM — Example

10___III_081_ORM_ExAnnot_2010_16.ppt

  • Количество слайдов: 42

ORM Приклад із використанням анотацій ORM - Example ORM Приклад із використанням анотацій ORM - Example

pom. xml (1/2) <project xmlns= pom. xml (1/2) 4. 0. 0 com. ttp Hibernate. Example jar 1. 0. 0 Hibernate. Example http: //maven. apache. org ORM - Example

pom. xml (2/2) <dependencies> <dependency> <group. Id>org. hibernate</group. Id> <artifact. Id>hibernate-core</artifact. Id> <version>5. 1. pom. xml (2/2) org. hibernate hibernate-core 5. 1. 0. Final mysql mysql-connector-java 5. 1. 10 org. testng testng 6. 8. 7 test ORM - Example

Клас Author (1/2) package com. ttp. Hibernate. Example; import java. io. Serializable; import javax. Клас 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. Клас 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 Клас 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. Клас 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); } Клас 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 authors = new Hash. Set(); public void add. Author(Author author) { author. set. Department(this); this. authors. add(author); } Author aspirant = new Author("Aspirant. TTP"); ORM - Example dep. TTP. add. Author(aspirant);

Клас Department (3/3) public String to. String() { String res = new String(); res Клас 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. Клас 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. Клас 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() Клас 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() {. Клас 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 authors =. . . new Hash. Set(); session. delete(dep. MI); // cascade my_print(2); } @Test(depends. On. Methods = { "test 1", "test 2" }) public void test 3() {. . . session. save. Or. Update(dep_TTP); // delete-orphan ORM - Example my_print(3); }

Результати тестування ORM - Example Результати тестування ORM - Example

Клас Test. NG 1 , метод test 1 @Test public void test 1() { Клас 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()); dep. MI. set. Authors(new Hash. Set()); Author dotsent. TTP = new Author("Dotsent. TTP", dep. TTP); dep. TTP. get. Authors(). add(dotsent. TTP); // etc"Dotsent. MI", "Prof. TTP", "Prof. MI", //"Stud. TTP", "Stud 2 TTP", "Stud. MI", "Stud 2 MI” Author aspirant = new Author("Aspirant. TTP"); dep. TTP. add. Author(aspirant); session. save(dep. TTP); session. save(dep. MI); my_print(1); } public class Department. . . @One. To. Many(mapped. By = "department", orphan. Removal = true, cascade = { Cascade. Type. ALL }) ORM - Example private Set authors = new Hash. Set();

Клас Test. NG 1 , метод test 2 @Test(depends. On. Methods = { Клас 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 authors = new Hash. Set();

Клас Test. NG 1 , метод test 3 @Test(depends. On. Methods = { Клас 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 authors = new Hash. Set();

Файл hibernate. cfg. xml (1/2) jdbc: mysql: //localhost: 3306/cybfak root . . . com. mysql. jdbc. Driver org. hibernate. dialect. My. SQLDialect true true create-drop ORM - Example

Файл hibernate. cfg. xml (1/2) <!-- JDBC connection pool (use the built-in) --> <property Файл hibernate. cfg. xml (1/2) 1 thread ORM - Example

 • Одна таблиця на ієрархію класів (table-per-class-hierarchy) — “швейцарський сир”; Повний приклад на • Одна таблиця на ієрархію класів (table-per-class-hierarchy) — “швейцарський сир”; Повний приклад на використання анотацій ORM - Example

Повний приклад на використання анотацій ORM - Example Повний приклад на використання анотацій ORM - Example

Повний приклад. До генерації таблиць ORM - Example Повний приклад. До генерації таблиць ORM - Example

Повний приклад. Клас Article (1/2) @Entity @Table(name = Повний приклад. Клас 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 authors = new Hash. Set(); private String title; public Article(String title, Set authors) { this. title = title; this. authors = authors; } public Article(String title) { this. title = title; } public Article() { } ORM - Example

Повний приклад. Клас Article (2/2) public String to. String() { String res = new Повний приклад. Клас 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 (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

articles = new Hash. Set
(); protected String name; ORM - Example

Повний приклад. Клас Author (2/2) @Many. To. One @Join. Column(name = "; }. . . // get-, set- } ORM - Example

Повний приклад. Клас Department (1/2) @Entity @Table(name = Повний приклад. Клас 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 authors = new Hash. Set(); private String title; ORM - Example

Повний приклад. Клас Department (2/2) public Department() { } public Department(String title) { this. Повний приклад. Клас Department (2/2) public Department() { } public Department(String title) { this. set. Title(title); } @Override public String to. String() { return "Department "; }. . . // get-, set} ORM - Example

Повний приклад. Клас Prof @Entity @Discriminator. Value( Повний приклад. Клас 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 tems = new Hash. Set(); private String posada; public Prof(String name, Department department, String posada) { this. name = name; this. posada = posada; this. department = department; } public Prof() { } public String to. String() { return ("Prof " + super. to. String() + ". Посада : " + posada + ", сп. тем : {" + tems. to. String() + "}"); }. . . // get-, set. ORM - Example }

Повний приклад. Клас Stud @Entity @Discriminator. Value( Повний приклад. Клас 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= Повний приклад. Клас 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 Повний приклад. Клас 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. Повний приклад. Клас 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 Повний приклад. Клас 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()); dep. MI. set. Authors(new Hash. Set()); Prof dotsent. TTP = new Prof("Dotsent. TTP", dep. TTP, "доцент"); dotsent. MI = new Prof("Dotsent. MI", dep. MI, "доцент"); prof. TTP = new Prof("Prof. TTP", dep. TTP, "професор"); prof. MI = new Prof("Prof. MI", dep. TTP, "професор"); Stud stud. TTP = new Stud("Stud. TTP", dep. TTP, 4); stud 2 TTP = new Stud("Stud 2 TTP", dep. TTP, 4); stud. MI = new Stud("Stud. MI", dep. MI, 4); ORM - Example stud 2 MI = new Stud("Stud 2 MI", dep. MI, 4);

Повний приклад. Клас App. Test (2/7) dep. TTP. get. Authors(). add(dotsent. TTP); dep. TTP. Повний приклад. Клас 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()); dotsent. TTP. set. Articles(new Hash. Set

()); dotsent. TTP. get. Tems(). add(dip 1); prof. TTP. set. Tems(new Hash. Set()); prof. TTP. set. Articles(new Hash. Set
()); ORM prof. TTP. get. Tems(). add(dip 2); - Example

Повний приклад. Клас App. Test (3/7) dotsent. MI. set. Tems(new Hash. Set<Tema. Dipl>()); dotsent. Повний приклад. Клас App. Test (3/7) dotsent. MI. set. Tems(new Hash. Set()); dotsent. MI. set. Articles(new Hash. Set

()); prof. MI. set. Tems(new Hash. Set()); prof. MI. set. Articles(new Hash. Set
()); prof. MI. get. Tems(). add(dip 3); prof. MI. get. Tems(). add(dip 4); dotsent. MI. set. Articles(new Hash. Set
()); stud. TTP. set. Tema(dip 1); stud 2 TTP. set. Tema(dip 2); stud 2 TTP. set. Articles(new Hash. Set
()); stud. MI. set. Tema(dip 3); stud. MI. set. Articles(new Hash. Set
()); stud 2 MI. set. Tema(dip 4); stud 2 MI. set. Articles(new Hash. Set
()); Set set 1 = new Hash. Set(); set 1. add(dotsent. TTP); set 1. add(prof. TTP); set 1. add(stud. TTP); Article art 1 = new Article("art 1", set 1); dotsent. TTP. get. Articles(). add(art 1); prof. TTP. get. Articles(). add(art 1); ORM - Example stud. TTP. get. Articles(). add(art 1);

Повний приклад. Клас App. Test (4/7) Set<Author> set 2 = new Hash. Set(); set Повний приклад. Клас App. Test (4/7) Set set 2 = new Hash. Set(); set 2. add(dotsent. TTP); set 2. add(prof. TTP); set 2. add(stud 2 TTP); Article art 2 = new Article("art 2", set 2); dotsent. TTP. get. Articles(). add(art 2); prof. TTP. get. Articles(). add(art 2); stud 2 TTP. get. Articles(). add(art 2); Set set 3 = new Hash. Set(); set 3. add(prof. TTP); set 3. add(stud 2 TTP); Article art 3 = new Article("art 3", set 3); Article art 4 = new Article("art 4", set 3); prof. TTP. get. Articles(). add(art 3); stud 2 TTP. get. Articles(). add(art 3); prof. TTP. get. Articles(). add(art 4); stud 2 TTP. get. Articles(). add(art 4); ORM - Example

Повний приклад. Клас App. Test (5/7) Set<Author> set 4 = new Hash. Set(); set Повний приклад. Клас App. Test (5/7) Set set 4 = new Hash. Set(); set 4. add(prof. MI); set 4. add(prof. TTP); Article art 5 = new Article("art 5", set 4); Article art 6 = new Article("art 6", set 4); prof. TTP. get. Articles(). add(art 5); prof. MI. get. Articles(). add(art 6); session. save(dep. TTP); session. save(dep. MI); System. out. println("--------1 ---------"); Query q 1 = session. create. Query("From Stud"); List list = q 1. list(); for (int i = 0; i < list. size(); ) { Stud st = (Stud) list. get(i); System. out. println(st. get. Name() + " , " + st. articles. to. String()); i = i + 1; ORM - Example };

Повний приклад. Клас App. Test (6/7) System. out. println(

Повний приклад. Клас App. Test (7/7) System. out. println(

Повний приклад. Файл hibernate. cfg. xml (1/2) jdbc: mysql: //localhost: 3306/cyb_db root . . . com. mysql. jdbc. Driver org. hibernate. dialect. My. SQLDialect false true create-drop ORM - Example

Повний приклад. Файл hibernate. cfg. xml (2/2) <!-- JDBC connection pool (use the built-in) Повний приклад. Файл hibernate. cfg. xml (2/2) 1 thread ORM - Example