Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com (#3711)

* Squashed commit of the following:

commit b31955df9638a6217a804e61faa230d8eacb293b
Author: Juan Moreno <earth001@gmail.com>
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 <earth001@gmail.com>
Date:   Wed Feb 21 23:26:20 2018 -0300

    Fix package names

commit b31955df9638a6217a804e61faa230d8eacb293b
Author: Juan Moreno <earth001@gmail.com>
Date:   Wed Feb 21 22:45:52 2018 -0300

    Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com
This commit is contained in:
Juan Moreno 2018-02-22 05:52:40 -03:00 committed by Predrag Maric
parent c0f85101b2
commit b89b1cab6d
4 changed files with 109 additions and 9 deletions

View File

@ -17,38 +17,64 @@
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.2.13.Final</hibernate.version>
<kotlin.version>1.2.21</kotlin.version>
<spring.version>4.3.10.RELEASE</spring.version>
<thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
<h2.version>1.4.196</h2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jre8</artifactId>
<version>1.1.4</version>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.7.RELEASE</version>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.7.RELEASE</version>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-testing</artifactId>
<version>${hibernate.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.10.RELEASE</version>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -60,10 +86,11 @@
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>1.1.4</version>
<version>${kotlin.version}</version>
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
<jvmTarget>1.8</jvmTarget>
</configuration>
@ -87,7 +114,12 @@
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>1.1.4-3</version>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>

View File

@ -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)

View File

@ -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<Class<*>> {
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)
})
}
}

View File

@ -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