diff --git a/build.gradle b/build.gradle index 908c54f..23173ae 100644 --- a/build.gradle +++ b/build.gradle @@ -20,10 +20,10 @@ implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' - implementation 'org.hibernate:hibernate-core:5.4.2.Final' - runtimeOnly 'com.h2database:h2' runtimeOnly 'mysql:mysql-connector-java' providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation 'org.springframework.boot:spring-boot-starter-test' + compile("com.h2database:h2:1.4.196") compile("com.fasterxml.jackson.core:jackson-databind") + compile("org.hibernate:hibernate-core:5.4.2.Final") } diff --git a/src/test/java/hibernateTest/entities/Event.java b/src/test/java/hibernateTest/entities/Event.java new file mode 100644 index 0000000..2a6df8a --- /dev/null +++ b/src/test/java/hibernateTest/entities/Event.java @@ -0,0 +1,67 @@ +/* + * 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.entities; + +import java.util.Date; + +public class Event { + private Long id; + + private String title; + private Date date; + + public Event() { + // this form used by Hibernate + } + + public Event(String title, Date date) { + // for application use, to create new events + this.title = title; + this.date = date; + } + + public Long getId() { + return id; + } + + private void setId(Long id) { + this.id = id; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} \ No newline at end of file diff --git a/src/test/java/hibernateTest/models/Main.java b/src/test/java/hibernateTest/models/Main.java new file mode 100644 index 0000000..e081e4c --- /dev/null +++ b/src/test/java/hibernateTest/models/Main.java @@ -0,0 +1,12 @@ +package hibernateTest.models; + +import org.hibernate.SessionFactory; + +public class Main { + public static void main(String args[]){ + NativeApiIllustrationTest test = new NativeApiIllustrationTest(); + test.setUp(); + test.testBasicUsage(); + test.tearDown(); + } +} diff --git a/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java new file mode 100644 index 0000000..76a87a1 --- /dev/null +++ b/src/test/java/hibernateTest/models/NativeApiIllustrationTest.java @@ -0,0 +1,90 @@ +/* + * 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 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.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) result ) { + System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() ); + } + session.getTransaction().commit(); + session.close(); + } +} diff --git a/src/test/resources/hibernate.cfg.xml b/src/test/resources/hibernate.cfg.xml new file mode 100644 index 0000000..68e7f3f --- /dev/null +++ b/src/test/resources/hibernate.cfg.xml @@ -0,0 +1,41 @@ + + + + + + + + + + org.h2.Driver + jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE + sa + + + + 1 + + + org.hibernate.dialect.H2Dialect + + + org.hibernate.cache.internal.NoCacheProvider + + + true + + + create + + + + + + \ No newline at end of file