diff --git a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java index 478508b..d2e0759 100644 --- a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java +++ b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java @@ -26,6 +26,7 @@ import java.util.Date; import java.util.List; +import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable; import hibernateTest.entities.Event; import hibernateTest.entities.Group; import hibernateTest.entities.Request; @@ -68,38 +69,54 @@ sessionFactory.close(); } } catch (Exception e) { - + System.out.println("Exception!"); } } public void testBasicUsage() { - // create a couple of events... + /*データの保存(データベースに投入)*/ Session session = sessionFactory.openSession(); session.beginTransaction(); session.save( new Event( "Our very first event!", new Date() ) ); session.save( new Event( "A follow up event", new Date() ) ); session.save( new Group("111", "http://test1", "family", "011" ) ); session.save( new Group("222", "http://test2", "friend", "022" ) ); - session.save( new Request("111", "http://test01", "1999.01.01", "011", "ice", "1999.04.10",011)); + Group newGroup = new Group("333", "http://test3", "school", "033" ); + session.save(newGroup); //インスタンスを引数に渡すこともできます + session.save( new Request("111", "http://test01", "1999.01.01", "011", "cookie", "1999.04.10",011)); session.save( new Request("222", "http://test02", "1999.02.02", "033", "milk", "1999.04.10",011)); session.save( new Request("333", "http://test03", "1999.03.03", "033", "ice", "1999.04.10",011)); session.getTransaction().commit(); session.close(); - // read and delete a request + /*データ(レコード)の取得し削除。*/ session = sessionFactory.openSession(); session.beginTransaction(); - //http://a4dosanddos.hatenablog.com/entry/2015/03/21/135421 - //createCriteriaは非推奨。別の方法があるのか? - Criteria criteria = session.createCriteria(Request.class).add(Restrictions.eq("rId", "222")); - Request editRequest = (Request) criteria.uniqueResult(); - if(editRequest != null){ - session.delete(editRequest); + //load()メソッドを用いて取得する方法。 + Request deletedRequest = (Request) session.load(Request.class, "222"); + //データの削除 + if(deletedRequest != null){ + session.delete(deletedRequest); } session.getTransaction().commit(); session.close(); - // now lets pull events from the database and list them + /*データを取得し更新*/ + session = sessionFactory.openSession(); + session.beginTransaction(); + //createCriteriaを用いて取得する方法。ただし非推奨。 + //http://a4dosanddos.hatenablog.com/entry/2015/03/21/135421 + Criteria criteria = session.createCriteria(Request.class).add(Restrictions.eq("rId", "333")); + Request editRequest = (Request) criteria.uniqueResult(); + //データの更新 + if(editRequest != null) { + editRequest.setProduct("Chocolate ice !!"); + session.update(editRequest); + } + session.getTransaction().commit(); + session.close(); + + /*全件取得と出力*/ session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery( "from Event" ).list(); @@ -112,7 +129,7 @@ } List requestResult = session.createQuery( "from Request" ).list(); for ( Request request : (List) requestResult ) { - System.out.println( "Request (" + request.getrId() + ") : " + request.getUri() + ": " + request.getDate() + ": " + request.isDone()); + System.out.println( "Request (" + request.getrId() + ") : " + request.getUri() + ": " + request.getProduct()); } session.getTransaction().commit(); session.close();