diff --git a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java index f392f06..4914d2e 100644 --- a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java +++ b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java @@ -140,12 +140,12 @@ if(editGroup != null){ //リクエストはsession.save()で保存する必要がありません。 //後のテスト用に3つのリクエストを追加しています。 - Request aditionalrequest1 = new Request("004", "http://test04", "1999.01.01", "011", "Chocolate cookie", "1999.04.10", location); - Request aditionalrequest2 = new Request("005", "http://test05", "1999.01.01", "011", "Banana cookie", "1999.04.10", location); - Request aditionalrequest3 = new Request("006", "http://test06", "1999.01.01", "011", "Milk cookie", "1999.04.10", location); - editGroup.addRequests(aditionalrequest1); - editGroup.addRequests(aditionalrequest2); - editGroup.addRequests(aditionalrequest3); + Request additionalrequest1 = new Request("004", "http://test04", "1999.01.01", "011", "Chocolate cookie", "1999.04.10", location); + Request additionalrequest2 = new Request("005", "http://test05", "1999.01.01", "011", "Banana cookie", "1999.04.10", location); + Request additionalrequest3 = new Request("006", "http://test06", "1999.01.01", "011", "Milk cookie", "1999.04.10", location); + editGroup.addRequests(additionalrequest1); + editGroup.addRequests(additionalrequest2); + editGroup.addRequests(additionalrequest3); session.update(editGroup); } session.getTransaction().commit(); @@ -156,15 +156,32 @@ editGroup = session.load(Group.class, "222"); if(editGroup != null){ //リクエストはsession.save()で保存する必要がありません。 - Request aditionalrequest1 = new Request("007", "http://test07", "1999.01.01", "022", "Green Tee", "1999.04.10", location); - Request aditionalrequest2 = new Request("008", "http://test08", "1999.01.01", "022", "Apple Tee", "1999.04.10", location); - editGroup.addRequests(aditionalrequest1); - editGroup.addRequests(aditionalrequest2); + Request additionalrequest1 = new Request("007", "http://test07", "1999.01.01", "022", "Green Tee", "1999.04.10", location); + Request additionalrequest2 = new Request("008", "http://test08", "1999.01.01", "022", "Apple Tee", "1999.04.10", location); + editGroup.addRequests(additionalrequest1); + editGroup.addRequests(additionalrequest2); session.update(editGroup); } session.getTransaction().commit(); session.close(); + session = sessionFactory.openSession(); + session.beginTransaction(); + editGroup = session.load(Group.class, "333"); + if(editGroup != null){ + //リクエストはsession.save()で保存する必要がありません。 + Request additionalrequest1 = new Request("009", "http://test09", "1999.01.01", "033", "Hamburger", "1999.04.10", location); + Request additionalrequest2 = new Request("010", "http://test10", "1999.01.01", "033", "Potato", "1999.04.10", location); + Request additionalrequest3 = new Request("011", "http://test11", "1999.01.01", "033", "Smile", "1999.04.10", location); + editGroup.addRequests(additionalrequest1); + editGroup.addRequests(additionalrequest2); + editGroup.addRequests(additionalrequest3); + session.update(editGroup); + } + session.getTransaction().commit(); + session.close(); + + /*リレーションを持ったリクエストの削除 (RIDを指定してリクエストを取得し、その後削除)*/ session = sessionFactory.openSession(); session.beginTransaction(); @@ -176,7 +193,6 @@ session.getTransaction().commit(); session.close(); - /*全件取得と出力*/ session = sessionFactory.openSession(); session.beginTransaction(); @@ -191,11 +207,6 @@ for ( Group group : (List) groupResult ) { System.out.println( "Group (" + group.getgId() + ") : " + group.getUri() + ": " + group.getName()); } - List requestResult = session.createQuery( "from Request" ).list(); - System.out.println("リクエストテーブル"); - for ( Request request : (List) requestResult ) { - System.out.println( "Request (" + request.getrId() + ") : " + request.getUri() + ": " + request.getProduct()); - } /*リレーションを持たせた分の出力*/ //Setは要素の順番に意味がないため、出力の順番はrId順ではありません。 @@ -211,8 +222,31 @@ for ( Request request : (Set) set ) { System.out.println( "rId: " + request.getrId() + " " + request.getUri() + " " + request.getProduct()); } - session.getTransaction().commit(); session.close(); + + /*リクエストを持ったグループの削除*/ + session = sessionFactory.openSession(); + session.beginTransaction(); + //load()メソッドを用いて取得する方法。 + Group deleteGroup = (Group) session.load(Group.class, "333"); + //データの削除 + if(deleteGroup != null){ + session.delete(deleteGroup); + } + session.getTransaction().commit(); + session.close(); + + session = sessionFactory.openSession(); + session.beginTransaction(); + List requestResult = session.createQuery( "from Request" ).list(); + System.out.println("グループ(ID:333)を削除後のリクエストテーブル"); + for ( Request request : (List) requestResult ) { + System.out.println( "Request (" + request.getrId() + ") : " + request.getUri() + ": " + request.getProduct()); + } + System.out.println("ハンバーガー,ポテト,スマイルも消えています"); + session.getTransaction().commit(); + session.close(); + } }