Skip to content

Commit

Permalink
BAEL-1159 - Added sample with null fields (eugenp#3756)
Browse files Browse the repository at this point in the history
* Squashed commit of the following:

commit b31955df9638a6217a804e61faa230d8eacb293b
Author: Juan Moreno <[email protected]>
Date:   Wed Feb 21 22:45:52 2018 -0300

    Sample code for BAEL-1159 - Working with Kotlin and JPA - [email protected]

* Squashed commit of the following:

commit 4e6e27c914a401ee6bc599c7ffe913384137646a
Author: Juan Moreno <[email protected]>
Date:   Wed Feb 21 23:26:20 2018 -0300

    Fix package names

commit b31955df9638a6217a804e61faa230d8eacb293b
Author: Juan Moreno <[email protected]>
Date:   Wed Feb 21 22:45:52 2018 -0300

    Sample code for BAEL-1159 - Working with Kotlin and JPA - [email protected]

* Added sample with null fields

* Removed unused dependency
  • Loading branch information
earth001 authored and pivovarit committed Mar 8, 2018
1 parent 4526bb6 commit 67092cc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion spring-mvc-kotlin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.2.13.Final</hibernate.version>
<kotlin.version>1.2.21</kotlin.version>
<kotlin.version>1.2.30</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>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package com.baeldung.jpa

import javax.persistence.CascadeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.OneToMany
import javax.persistence.Table

@Entity
@Table(name = "person")
data class Person(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int,
val name: String)
@Column(nullable = false)
val name: String,
@Column(nullable = true)
val email: String?,
@Column(nullable = true)
@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.ALL]) val phoneNumbers: List<PhoneNumber>?)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.jpa

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.Table

@Entity
@Table(name = "phone_number")
data class PhoneNumber(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int,
@Column(nullable = false)
val number: String)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.baeldung.kotlin.jpa

import com.baeldung.jpa.Person
import com.baeldung.jpa.PhoneNumber
import org.hibernate.cfg.Configuration
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase
import org.hibernate.testing.transaction.TransactionUtil.doInHibernate
Expand All @@ -21,7 +22,7 @@ class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() {
}

override fun getAnnotatedClasses(): Array<Class<*>> {
return arrayOf(Person::class.java)
return arrayOf(Person::class.java, PhoneNumber::class.java)
}

override fun configure(configuration: Configuration) {
Expand All @@ -31,13 +32,22 @@ class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() {

@Test
fun givenPerson_whenSaved_thenFound() {
val personToSave = Person(0, "John")
doInHibernate(({ this.sessionFactory() }), { session ->
val personToSave = Person(0, "John", "[email protected]", Arrays.asList(PhoneNumber(0, "202-555-0171"), PhoneNumber(0, "202-555-0102")))
session.persist(personToSave)
assertTrue(session.contains(personToSave))
val personFound = session.find(Person::class.java, personToSave.id)
session.refresh(personFound)
assertTrue(personToSave == personFound)
})
}

@Test
fun givenPersonWithNullFields_whenSaved_thenFound() {
doInHibernate(({ this.sessionFactory() }), { session ->
val personToSave = Person(0, "John", null, null)
session.persist(personToSave)
val personFound = session.find(Person::class.java, personToSave.id)
session.refresh(personFound)
assertTrue(personToSave == personFound)
})
}
Expand Down

0 comments on commit 67092cc

Please sign in to comment.