Skip to content

Commit 86a0333

Browse files
earth001pedja4
authored andcommitted
BAEL-1159 - Simplified example (eugenp#3853)
* 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 * Added sample with null fields * Removed unused dependency * BAEL-1159 - Simplified example
1 parent 8cee378 commit 86a0333

4 files changed

Lines changed: 22 additions & 15 deletions

File tree

‎spring-mvc-kotlin/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<properties>
2121
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22-
<hibernate.version>5.2.13.Final</hibernate.version>
22+
<hibernate.version>5.2.15.Final</hibernate.version>
2323
<kotlin.version>1.2.30</kotlin.version>
2424
<spring.version>4.3.10.RELEASE</spring.version>
2525
<thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
package com.baeldung.jpa
1+
package com.baeldung.kotlin.jpa
22

33
import javax.persistence.CascadeType
44
import javax.persistence.Column
55
import javax.persistence.Entity
6-
import javax.persistence.FetchType
76
import javax.persistence.GeneratedValue
87
import javax.persistence.GenerationType
98
import javax.persistence.Id
109
import javax.persistence.OneToMany
11-
import javax.persistence.Table
1210

1311
@Entity
14-
@Table(name = "person")
15-
data class Person(
12+
data class Person @JvmOverloads constructor(
1613
@Id
1714
@GeneratedValue(strategy = GenerationType.IDENTITY)
1815
val id: Int,
1916
@Column(nullable = false)
2017
val name: String,
2118
@Column(nullable = true)
22-
val email: String?,
23-
@Column(nullable = true)
24-
@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.ALL]) val phoneNumbers: List<PhoneNumber>?)
19+
val email: String? = null,
20+
@OneToMany(cascade = [CascadeType.ALL])
21+
val phoneNumbers: List<PhoneNumber>? = null)

‎spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package com.baeldung.jpa
1+
package com.baeldung.kotlin.jpa
22

33
import javax.persistence.Column
44
import javax.persistence.Entity
55
import javax.persistence.GeneratedValue
66
import javax.persistence.GenerationType
77
import javax.persistence.Id
8-
import javax.persistence.Table
98

109
@Entity
11-
@Table(name = "phone_number")
1210
data class PhoneNumber(
1311
@Id
1412
@GeneratedValue(strategy = GenerationType.IDENTITY)

‎spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.baeldung.kotlin.jpa
22

3-
import com.baeldung.jpa.Person
4-
import com.baeldung.jpa.PhoneNumber
53
import org.hibernate.cfg.Configuration
64
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase
75
import org.hibernate.testing.transaction.TransactionUtil.doInHibernate
@@ -31,12 +29,25 @@ class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() {
3129
}
3230

3331
@Test
34-
fun givenPerson_whenSaved_thenFound() {
32+
fun givenPersonWithFullData_whenSaved_thenFound() {
3533
doInHibernate(({ this.sessionFactory() }), { session ->
3634
val personToSave = Person(0, "John", "jhon@test.com", Arrays.asList(PhoneNumber(0, "202-555-0171"), PhoneNumber(0, "202-555-0102")))
3735
session.persist(personToSave)
3836
val personFound = session.find(Person::class.java, personToSave.id)
3937
session.refresh(personFound)
38+
39+
assertTrue(personToSave == personFound)
40+
})
41+
}
42+
43+
@Test
44+
fun givenPerson_whenSaved_thenFound() {
45+
doInHibernate(({ this.sessionFactory() }), { session ->
46+
val personToSave = Person(0, "John")
47+
session.persist(personToSave)
48+
val personFound = session.find(Person::class.java, personToSave.id)
49+
session.refresh(personFound)
50+
4051
assertTrue(personToSave == personFound)
4152
})
4253
}
@@ -48,6 +59,7 @@ class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() {
4859
session.persist(personToSave)
4960
val personFound = session.find(Person::class.java, personToSave.id)
5061
session.refresh(personFound)
62+
5163
assertTrue(personToSave == personFound)
5264
})
5365
}

0 commit comments

Comments
 (0)