Skip to content

Commit b89b1ca

Browse files
earth001pedja4
authored andcommitted
Sample code for BAEL-1159 - Working with Kotlin and JPA - earth001@gmail.com (eugenp#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
1 parent c0f8510 commit b89b1ca

4 files changed

Lines changed: 109 additions & 9 deletions

File tree

‎spring-mvc-kotlin/pom.xml‎

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,64 @@
1717

1818
<packaging>war</packaging>
1919

20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<hibernate.version>5.2.13.Final</hibernate.version>
23+
<kotlin.version>1.2.21</kotlin.version>
24+
<spring.version>4.3.10.RELEASE</spring.version>
25+
<thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
26+
<h2.version>1.4.196</h2.version>
27+
</properties>
28+
2029
<dependencies>
2130
<dependency>
2231
<groupId>org.jetbrains.kotlin</groupId>
23-
<artifactId>kotlin-stdlib-jre8</artifactId>
24-
<version>1.1.4</version>
32+
<artifactId>kotlin-stdlib-jdk8</artifactId>
33+
<version>${kotlin.version}</version>
2534
</dependency>
2635

2736
<dependency>
2837
<groupId>org.springframework</groupId>
2938
<artifactId>spring-web</artifactId>
30-
<version>4.3.10.RELEASE</version>
39+
<version>${spring.version}</version>
3140
</dependency>
3241
<dependency>
3342
<groupId>org.springframework</groupId>
3443
<artifactId>spring-webmvc</artifactId>
35-
<version>4.3.10.RELEASE</version>
44+
<version>${spring.version}</version>
3645
</dependency>
3746
<dependency>
3847
<groupId>org.thymeleaf</groupId>
3948
<artifactId>thymeleaf</artifactId>
40-
<version>3.0.7.RELEASE</version>
49+
<version>${thymeleaf.version}</version>
4150
</dependency>
4251

4352
<dependency>
4453
<groupId>org.thymeleaf</groupId>
4554
<artifactId>thymeleaf-spring4</artifactId>
46-
<version>3.0.7.RELEASE</version>
55+
<version>${thymeleaf.version}</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.hibernate</groupId>
59+
<artifactId>hibernate-core</artifactId>
60+
<version>${hibernate.version}</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.hibernate</groupId>
64+
<artifactId>hibernate-testing</artifactId>
65+
<version>${hibernate.version}</version>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.h2database</groupId>
70+
<artifactId>h2</artifactId>
71+
<version>${h2.version}</version>
72+
<scope>test</scope>
4773
</dependency>
4874
<dependency>
4975
<groupId>org.springframework</groupId>
5076
<artifactId>spring-test</artifactId>
51-
<version>4.3.10.RELEASE</version>
77+
<version>${spring.version}</version>
5278
<scope>test</scope>
5379
</dependency>
5480
</dependencies>
@@ -60,10 +86,11 @@
6086
<plugin>
6187
<artifactId>kotlin-maven-plugin</artifactId>
6288
<groupId>org.jetbrains.kotlin</groupId>
63-
<version>1.1.4</version>
89+
<version>${kotlin.version}</version>
6490
<configuration>
6591
<compilerPlugins>
6692
<plugin>spring</plugin>
93+
<plugin>jpa</plugin>
6794
</compilerPlugins>
6895
<jvmTarget>1.8</jvmTarget>
6996
</configuration>
@@ -87,7 +114,12 @@
87114
<dependency>
88115
<groupId>org.jetbrains.kotlin</groupId>
89116
<artifactId>kotlin-maven-allopen</artifactId>
90-
<version>1.1.4-3</version>
117+
<version>${kotlin.version}</version>
118+
</dependency>
119+
<dependency>
120+
<groupId>org.jetbrains.kotlin</groupId>
121+
<artifactId>kotlin-maven-noarg</artifactId>
122+
<version>${kotlin.version}</version>
91123
</dependency>
92124
</dependencies>
93125
</plugin>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.jpa
2+
3+
import javax.persistence.Entity
4+
import javax.persistence.GeneratedValue
5+
import javax.persistence.GenerationType
6+
import javax.persistence.Id
7+
import javax.persistence.Table
8+
9+
@Entity
10+
@Table(name = "person")
11+
data class Person(
12+
@Id
13+
@GeneratedValue(strategy = GenerationType.AUTO)
14+
val id: Int,
15+
val name: String)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.kotlin.jpa
2+
3+
import com.baeldung.jpa.Person
4+
import org.hibernate.cfg.Configuration
5+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase
6+
import org.hibernate.testing.transaction.TransactionUtil.doInHibernate
7+
import org.junit.Assert.assertTrue
8+
import org.junit.Test
9+
import java.io.IOException
10+
import java.util.*
11+
12+
13+
class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() {
14+
15+
private val properties: Properties
16+
@Throws(IOException::class)
17+
get() {
18+
val properties = Properties()
19+
properties.load(javaClass.classLoader.getResourceAsStream("hibernate.properties"))
20+
return properties
21+
}
22+
23+
override fun getAnnotatedClasses(): Array<Class<*>> {
24+
return arrayOf(Person::class.java)
25+
}
26+
27+
override fun configure(configuration: Configuration) {
28+
super.configure(configuration)
29+
configuration.properties = properties
30+
}
31+
32+
@Test
33+
fun givenPerson_whenSaved_thenFound() {
34+
val personToSave = Person(0, "John")
35+
doInHibernate(({ this.sessionFactory() }), { session ->
36+
session.persist(personToSave)
37+
assertTrue(session.contains(personToSave))
38+
})
39+
doInHibernate(({ this.sessionFactory() }), { session ->
40+
val personFound = session.find(Person::class.java, personToSave.id)
41+
assertTrue(personToSave == personFound)
42+
})
43+
}
44+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
hibernate.connection.driver_class=org.h2.Driver
2+
hibernate.connection.url=jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1
3+
hibernate.connection.username=sa
4+
hibernate.connection.autocommit=true
5+
jdbc.password=
6+
7+
hibernate.dialect=org.hibernate.dialect.H2Dialect
8+
hibernate.show_sql=true
9+
hibernate.hbm2ddl.auto=create-drop

0 commit comments

Comments
 (0)