From b89b1cab6d0d57a1eebf82c9d8a797180b7a64aa Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Thu, 22 Feb 2018 05:52:40 -0300 Subject: [PATCH] Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com (#3711) * Squashed commit of the following: commit b31955df9638a6217a804e61faa230d8eacb293b Author: Juan Moreno Date: Wed Feb 21 22:45:52 2018 -0300 Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com * Squashed commit of the following: commit 4e6e27c914a401ee6bc599c7ffe913384137646a Author: Juan Moreno Date: Wed Feb 21 23:26:20 2018 -0300 Fix package names commit b31955df9638a6217a804e61faa230d8eacb293b Author: Juan Moreno Date: Wed Feb 21 22:45:52 2018 -0300 Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com --- spring-mvc-kotlin/pom.xml | 50 +++++++++++++++---- .../kotlin/com/baeldung/kotlin/jpa/Person.kt | 15 ++++++ .../jpa/HibernateKotlinIntegrationTest.kt | 44 ++++++++++++++++ .../src/test/resources/hibernate.properties | 9 ++++ 4 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt create mode 100644 spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt create mode 100644 spring-mvc-kotlin/src/test/resources/hibernate.properties diff --git a/spring-mvc-kotlin/pom.xml b/spring-mvc-kotlin/pom.xml index 2d1dac5e29..28a5681c03 100644 --- a/spring-mvc-kotlin/pom.xml +++ b/spring-mvc-kotlin/pom.xml @@ -17,38 +17,64 @@ war + + UTF-8 + 5.2.13.Final + 1.2.21 + 4.3.10.RELEASE + 3.0.7.RELEASE + 1.4.196 + + org.jetbrains.kotlin - kotlin-stdlib-jre8 - 1.1.4 + kotlin-stdlib-jdk8 + ${kotlin.version} org.springframework spring-web - 4.3.10.RELEASE + ${spring.version} org.springframework spring-webmvc - 4.3.10.RELEASE + ${spring.version} org.thymeleaf thymeleaf - 3.0.7.RELEASE + ${thymeleaf.version} org.thymeleaf thymeleaf-spring4 - 3.0.7.RELEASE + ${thymeleaf.version} + + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.hibernate + hibernate-testing + ${hibernate.version} + test + + + com.h2database + h2 + ${h2.version} + test org.springframework spring-test - 4.3.10.RELEASE + ${spring.version} test @@ -60,10 +86,11 @@ kotlin-maven-plugin org.jetbrains.kotlin - 1.1.4 + ${kotlin.version} spring + jpa 1.8 @@ -87,7 +114,12 @@ org.jetbrains.kotlin kotlin-maven-allopen - 1.1.4-3 + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + ${kotlin.version} diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt new file mode 100644 index 0000000000..801bd1b0a5 --- /dev/null +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt @@ -0,0 +1,15 @@ +package com.baeldung.jpa + +import javax.persistence.Entity +import javax.persistence.GeneratedValue +import javax.persistence.GenerationType +import javax.persistence.Id +import javax.persistence.Table + +@Entity +@Table(name = "person") +data class Person( + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + val id: Int, + val name: String) \ No newline at end of file diff --git a/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt new file mode 100644 index 0000000000..8f54373406 --- /dev/null +++ b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt @@ -0,0 +1,44 @@ +package com.baeldung.kotlin.jpa + +import com.baeldung.jpa.Person +import org.hibernate.cfg.Configuration +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase +import org.hibernate.testing.transaction.TransactionUtil.doInHibernate +import org.junit.Assert.assertTrue +import org.junit.Test +import java.io.IOException +import java.util.* + + +class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() { + + private val properties: Properties + @Throws(IOException::class) + get() { + val properties = Properties() + properties.load(javaClass.classLoader.getResourceAsStream("hibernate.properties")) + return properties + } + + override fun getAnnotatedClasses(): Array> { + return arrayOf(Person::class.java) + } + + override fun configure(configuration: Configuration) { + super.configure(configuration) + configuration.properties = properties + } + + @Test + fun givenPerson_whenSaved_thenFound() { + val personToSave = Person(0, "John") + doInHibernate(({ this.sessionFactory() }), { session -> + session.persist(personToSave) + assertTrue(session.contains(personToSave)) + }) + doInHibernate(({ this.sessionFactory() }), { session -> + val personFound = session.find(Person::class.java, personToSave.id) + assertTrue(personToSave == personFound) + }) + } +} \ No newline at end of file diff --git a/spring-mvc-kotlin/src/test/resources/hibernate.properties b/spring-mvc-kotlin/src/test/resources/hibernate.properties new file mode 100644 index 0000000000..7b8764637b --- /dev/null +++ b/spring-mvc-kotlin/src/test/resources/hibernate.properties @@ -0,0 +1,9 @@ +hibernate.connection.driver_class=org.h2.Driver +hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1 +hibernate.connection.username=sa +hibernate.connection.autocommit=true +jdbc.password= + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop \ No newline at end of file