-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Correct OneToMany relationship for immutable data classes (#1072)
Fixes #1054
- Loading branch information
Showing
7 changed files
with
252 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/one2many/OneToManyChildrenSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package io.micronaut.data.jdbc.h2.one2many | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.data.annotation.* | ||
import io.micronaut.data.jdbc.annotation.JdbcRepository | ||
import io.micronaut.data.jdbc.h2.H2DBProperties | ||
import io.micronaut.data.jdbc.h2.H2TestPropertyProvider | ||
import io.micronaut.data.model.query.builder.sql.Dialect | ||
import io.micronaut.data.repository.CrudRepository | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import javax.inject.Inject | ||
|
||
@MicronautTest | ||
@H2DBProperties | ||
class OneToManyChildrenSpec extends Specification implements H2TestPropertyProvider { | ||
@AutoCleanup | ||
@Shared | ||
ApplicationContext applicationContext = ApplicationContext.run(getProperties()) | ||
|
||
@Shared | ||
@Inject | ||
ParentRepository parentRepository = applicationContext.getBean(ParentRepository) | ||
|
||
void 'test one-to-many hierarchy'() { | ||
given: | ||
def children = [] | ||
Parent parent = new Parent(name: "parent", children: children) | ||
children.add new Child(name: "A", parent: parent) | ||
children.add new Child(name: "B", parent: parent) | ||
children.add new Child(name: "C", parent: parent) | ||
when: | ||
parentRepository.save(parent) | ||
then: | ||
parent.id | ||
parent.children.size() == 3 | ||
parent.children.forEach { | ||
verifyAll(it) { | ||
it.id | ||
it.parent | ||
it.name | ||
} | ||
} | ||
when: | ||
parent = parentRepository.findById(parent.id).get() | ||
then: | ||
parent.id | ||
parent.children.size() == 3 | ||
parent.children.forEach { | ||
verifyAll(it) { | ||
it.id | ||
it.parent | ||
it.name | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
@JdbcRepository(dialect = Dialect.H2) | ||
interface ParentRepository extends CrudRepository<Parent, Long> { | ||
|
||
@Join(value = "children", type = Join.Type.FETCH) | ||
@Override | ||
Optional<Parent> findById(Long id); | ||
} | ||
|
||
@MappedEntity("x_product") | ||
class Parent { | ||
String name | ||
@Relation(value = Relation.Kind.ONE_TO_MANY, mappedBy = "parent", cascade = Relation.Cascade.ALL) | ||
List<Child> children | ||
@Id | ||
@GeneratedValue | ||
Long id | ||
} | ||
|
||
@MappedEntity("x_child") | ||
class Child { | ||
String name | ||
@Relation(value = Relation.Kind.MANY_TO_ONE) | ||
Parent parent | ||
@Id | ||
@GeneratedValue | ||
Long id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
doc-examples/jdbc-example-kotlin/src/main/kotlin/example/Child.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package example | ||
|
||
import io.micronaut.data.annotation.GeneratedValue | ||
import io.micronaut.data.annotation.Id | ||
import io.micronaut.data.annotation.MappedEntity | ||
import io.micronaut.data.annotation.Relation | ||
|
||
@MappedEntity | ||
data class Child( | ||
|
||
val name: String, | ||
|
||
@Relation(Relation.Kind.MANY_TO_ONE) | ||
val parent: Parent? = null, | ||
|
||
@field:Id @GeneratedValue val id: Int? = null | ||
|
||
|
||
|
||
) { | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (other !is Child) return false | ||
|
||
if (name != other.name) return false | ||
if (parent?.id != other.parent?.id) return false | ||
if (id != other.id) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = name.hashCode() | ||
result = 31 * result + parent?.id.hashCode() | ||
result = 31 * result + (id ?: 0) | ||
return result | ||
} | ||
|
||
override fun toString(): String { | ||
return "Child(name='$name', parent=${parent?.id}, id=$id)" | ||
} | ||
|
||
|
||
} | ||
|
19 changes: 19 additions & 0 deletions
19
doc-examples/jdbc-example-kotlin/src/main/kotlin/example/Parent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package example | ||
|
||
import io.micronaut.data.annotation.GeneratedValue | ||
import io.micronaut.data.annotation.Id | ||
import io.micronaut.data.annotation.MappedEntity | ||
import io.micronaut.data.annotation.Relation | ||
|
||
@MappedEntity | ||
data class Parent( | ||
|
||
val name: String, | ||
|
||
@Relation(value = Relation.Kind.ONE_TO_MANY, mappedBy = "parent", cascade = [Relation.Cascade.ALL]) | ||
val children: List<Child>, | ||
|
||
@field:Id @GeneratedValue | ||
val id: Int? = null | ||
|
||
) |
16 changes: 16 additions & 0 deletions
16
doc-examples/jdbc-example-kotlin/src/main/kotlin/example/ParentRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package example | ||
|
||
import example.Parent | ||
import io.micronaut.data.annotation.Join | ||
import io.micronaut.data.jdbc.annotation.JdbcRepository | ||
import io.micronaut.data.model.query.builder.sql.Dialect | ||
import io.micronaut.data.repository.CrudRepository | ||
import java.util.Optional | ||
|
||
@JdbcRepository(dialect = Dialect.H2) | ||
abstract class ParentRepository : CrudRepository<Parent, Int> { | ||
|
||
@Join(value = "children", type = Join.Type.FETCH) | ||
abstract override fun findById(id: Int): Optional<Parent> | ||
|
||
} |
Oops, something went wrong.