From 67092ccc494ce985e78f047a2ca8af133255e282 Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Thu, 8 Mar 2018 08:31:06 -0300 Subject: [PATCH] BAEL-1159 - Added sample with null fields (#3756) * Squashed commit of the following: commit b31955df9638a6217a804e61faa230d8eacb293b Author: Juan Moreno 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 Date: Wed Feb 21 23:26:20 2018 -0300 Fix package names commit b31955df9638a6217a804e61faa230d8eacb293b Author: Juan Moreno 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 --- spring-mvc-kotlin/pom.xml | 2 +- .../kotlin/com/baeldung/kotlin/jpa/Person.kt | 13 ++++++++++-- .../com/baeldung/kotlin/jpa/PhoneNumber.kt | 17 ++++++++++++++++ .../jpa/HibernateKotlinIntegrationTest.kt | 20 ++++++++++++++----- 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt diff --git a/spring-mvc-kotlin/pom.xml b/spring-mvc-kotlin/pom.xml index 28a5681c03..8f9c58854d 100644 --- a/spring-mvc-kotlin/pom.xml +++ b/spring-mvc-kotlin/pom.xml @@ -20,7 +20,7 @@ UTF-8 5.2.13.Final - 1.2.21 + 1.2.30 4.3.10.RELEASE 3.0.7.RELEASE 1.4.196 diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt index 801bd1b0a5..076dcdac46 100644 --- a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/Person.kt @@ -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) \ No newline at end of file + @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?) \ No newline at end of file diff --git a/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt new file mode 100644 index 0000000000..25894275f6 --- /dev/null +++ b/spring-mvc-kotlin/src/main/kotlin/com/baeldung/kotlin/jpa/PhoneNumber.kt @@ -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) \ No newline at end of file diff --git a/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt index 8f54373406..94559812fa 100644 --- a/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt +++ b/spring-mvc-kotlin/src/test/kotlin/com/baeldung/kotlin/jpa/HibernateKotlinIntegrationTest.kt @@ -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 @@ -21,7 +22,7 @@ class HibernateKotlinIntegrationTest : BaseCoreFunctionalTestCase() { } override fun getAnnotatedClasses(): Array> { - return arrayOf(Person::class.java) + return arrayOf(Person::class.java, PhoneNumber::class.java) } override fun configure(configuration: Configuration) { @@ -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", "jhon@test.com", Arrays.asList(PhoneNumber(0, "202-555-0171"), PhoneNumber(0, "202-555-0102"))) session.persist(personToSave) - assertTrue(session.contains(personToSave)) - }) - doInHibernate(({ this.sessionFactory() }), { session -> 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) }) }