WIP:site
[hibernate4-maven-plugin] / src / it / tutorials-5.0.12 / basic / src / test / java / org / hibernate / tutorial / hbm / NativeApiIllustrationTest.java
1 /*
2  * Hibernate, Relational Persistence for Idiomatic Java
3  *
4  * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
5  * indicated by the @author tags or express copyright attribution
6  * statements applied by the authors.  All third-party contributions are
7  * distributed under license by Red Hat Inc.
8  *
9  * This copyrighted material is made available to anyone wishing to use, modify,
10  * copy, or redistribute it subject to the terms and conditions of the GNU
11  * Lesser General Public License, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16  * for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this distribution; if not, write to:
20  * Free Software Foundation, Inc.
21  * 51 Franklin Street, Fifth Floor
22  * Boston, MA  02110-1301  USA
23  */
24 package org.hibernate.tutorial.hbm;
25
26 import java.util.Date;
27 import java.util.List;
28
29 import org.hibernate.Session;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.boot.MetadataSources;
32 import org.hibernate.boot.registry.StandardServiceRegistry;
33 import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Illustrates use of Hibernate native APIs.
39  *
40  * @author Steve Ebersole
41  */
42 public class NativeApiIllustrationTest extends TestCase {
43         private SessionFactory sessionFactory;
44
45         @Override
46         protected void setUp() throws Exception {
47                 // A SessionFactory is set up once for an application!
48                 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
49                                 .configure() // configures settings from hibernate.cfg.xml
50                                 .build();
51                 try {
52                         sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
53                 }
54                 catch (Exception e) {
55                         // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
56                         // so destroy it manually.
57                         StandardServiceRegistryBuilder.destroy( registry );
58                 }
59         }
60
61         @Override
62         protected void tearDown() throws Exception {
63                 if ( sessionFactory != null ) {
64                         sessionFactory.close();
65                 }
66         }
67
68         @SuppressWarnings("unchecked")
69         public void testBasicUsage() {
70                 // create a couple of events...
71                 Session session = sessionFactory.openSession();
72                 session.beginTransaction();
73                 session.save( new Event( "Our very first event!", new Date() ) );
74                 session.save( new Event( "A follow up event", new Date() ) );
75                 session.getTransaction().commit();
76                 session.close();
77
78                 // now lets pull events from the database and list them
79                 session = sessionFactory.openSession();
80         session.beginTransaction();
81         List result = session.createQuery( "from Event" ).list();
82                 for ( Event event : (List<Event>) result ) {
83                         System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
84                 }
85         session.getTransaction().commit();
86         session.close();
87         }
88 }