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

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