/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package hibernateTest.models; import java.util.Date; import java.util.List; import hibernateTest.entities.Event; import hibernateTest.entities.Group; import hibernateTest.entities.Request; import org.hibernate.Criteria; import org.hibernate.criterion.Restrictions; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import junit.framework.TestCase; /** * Illustrates use of Hibernate native APIs. * * @author Steve Ebersole */ public class NativeApiIllustrationTest { private SessionFactory sessionFactory; public void setUp() { // A SessionFactory is set up once for an application! final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure() // configures settings from hibernate.cfg.xml .build(); try { sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory(); } catch (Exception e) { // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory // so destroy it manually. StandardServiceRegistryBuilder.destroy( registry ); } } public void tearDown() { try { if ( sessionFactory != null ) { sessionFactory.close(); } } catch (Exception e) { } } 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)); 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); } session.getTransaction().commit(); session.close(); // now lets pull events from the database and list them session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery( "from Event" ).list(); for ( Event event : (List<Event>) result ) { System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); } List groupResult = session.createQuery( "from Group" ).list(); for ( Group group : (List<Group>) groupResult ) { System.out.println( "Group (" + group.getgId() + ") : " + group.getUri() + ": " + group.getName()); } List requestResult = session.createQuery( "from Request" ).list(); for ( Request request : (List<Request>) requestResult ) { System.out.println( "Request (" + request.getrId() + ") : " + request.getUri() + ": " + request.getDate() + ": " + request.isDone()); } session.getTransaction().commit(); session.close(); } }