-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to persist cascade for non-null embedded id (#1242)
- Loading branch information
Showing
4 changed files
with
262 additions
and
6 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
127 changes: 127 additions & 0 deletions
127
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/H2EmbeddedCascadeSpec.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,127 @@ | ||
package io.micronaut.data.jdbc.h2 | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.core.annotation.NonNull | ||
import io.micronaut.data.annotation.Embeddable | ||
import io.micronaut.data.annotation.Join | ||
import io.micronaut.data.annotation.MappedEntity | ||
import io.micronaut.data.jdbc.annotation.JdbcRepository | ||
import io.micronaut.data.model.query.builder.sql.Dialect | ||
import io.micronaut.data.repository.CrudRepository | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import javax.persistence.CascadeType | ||
import javax.persistence.Column | ||
import javax.persistence.EmbeddedId | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.Id | ||
import javax.persistence.JoinColumn | ||
import javax.persistence.ManyToOne | ||
import javax.persistence.OneToMany | ||
|
||
@MicronautTest | ||
@H2DBProperties | ||
class H2EmbeddedCascadeSpec extends Specification implements H2TestPropertyProvider { | ||
@AutoCleanup | ||
@Shared | ||
ApplicationContext applicationContext = ApplicationContext.run(getProperties()) | ||
|
||
@Shared | ||
@Inject | ||
TemplateRepository templateRepository = applicationContext.getBean(TemplateRepository) | ||
|
||
void "test embedded cascade"() { | ||
when: | ||
Template template = new Template() | ||
template.name = "Template test" | ||
|
||
Tag tag = new Tag() | ||
TagPK tagPK = new TagPK() | ||
tagPK.tag = "New tag" | ||
tagPK.template = template | ||
tag.id = tagPK | ||
|
||
template.tags << tag | ||
|
||
templateRepository.save(template) | ||
template = templateRepository.findById(template.id).get() | ||
then: | ||
template | ||
template.tags.size() == 1 | ||
} | ||
|
||
} | ||
|
||
@JdbcRepository(dialect = Dialect.H2) | ||
interface TemplateRepository extends CrudRepository<Template, Long> { | ||
|
||
@Join("tags") | ||
@Override | ||
Optional<Template> findById(Long aLong); | ||
} | ||
|
||
@MappedEntity | ||
class Template { | ||
|
||
@Id | ||
@GeneratedValue | ||
Long id | ||
|
||
@NonNull | ||
String name | ||
|
||
@OneToMany(mappedBy = "id.template", cascade = CascadeType.ALL) | ||
Set<Tag> tags = [] as Set | ||
|
||
} | ||
|
||
@MappedEntity | ||
class Tag { | ||
|
||
@EmbeddedId | ||
TagPK id | ||
|
||
} | ||
|
||
@Embeddable | ||
class TagPK implements Serializable { | ||
|
||
@NonNull | ||
@Column(name = "tag") | ||
String tag | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "template_id") | ||
@Column(name = "template_id") | ||
Template template | ||
|
||
@Override | ||
boolean equals(o) { | ||
if (this.is(o)) { | ||
return true | ||
} | ||
if (getClass() != o.class) { | ||
return false | ||
} | ||
|
||
TagPK that = (TagPK) o | ||
|
||
if (tag != that.tag) { | ||
return false | ||
} | ||
return template == that.template | ||
} | ||
|
||
@Override | ||
int hashCode() { | ||
int result | ||
result = tag.hashCode() | ||
result = 31 * result + (template != null ? template.hashCode() : 0) | ||
return result | ||
} | ||
} | ||
|
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
127 changes: 127 additions & 0 deletions
127
data-r2dbc/src/test/groovy/io/micronaut/data/r2dbc/h2/H2EmbeddedCascadeSpec.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,127 @@ | ||
package io.micronaut.data.r2dbc.h2 | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.core.annotation.NonNull | ||
import io.micronaut.data.annotation.Embeddable | ||
import io.micronaut.data.annotation.Join | ||
import io.micronaut.data.annotation.MappedEntity | ||
import io.micronaut.data.model.query.builder.sql.Dialect | ||
import io.micronaut.data.r2dbc.annotation.R2dbcRepository | ||
import io.micronaut.data.repository.reactive.ReactorCrudRepository | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import reactor.core.publisher.Mono | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import javax.persistence.CascadeType | ||
import javax.persistence.Column | ||
import javax.persistence.EmbeddedId | ||
import javax.persistence.GeneratedValue | ||
import javax.persistence.Id | ||
import javax.persistence.JoinColumn | ||
import javax.persistence.ManyToOne | ||
import javax.persistence.OneToMany | ||
|
||
@MicronautTest | ||
class H2EmbeddedCascadeSpec extends Specification implements H2TestPropertyProvider { | ||
@AutoCleanup | ||
@Shared | ||
ApplicationContext applicationContext = ApplicationContext.run(getProperties()) | ||
|
||
@Shared | ||
@Inject | ||
TemplateRepository templateRepository = applicationContext.getBean(TemplateRepository) | ||
|
||
void "test embedded cascade"() { | ||
when: | ||
Template template = new Template() | ||
template.name = "Template test" | ||
|
||
Tag tag = new Tag() | ||
TagPK tagPK = new TagPK() | ||
tagPK.tag = "New tag" | ||
tagPK.template = template | ||
tag.id = tagPK | ||
|
||
template.tags << tag | ||
|
||
templateRepository.save(template).block() | ||
template = templateRepository.findById(template.id).blockOptional().get() | ||
then: | ||
template | ||
template.tags.size() == 1 | ||
} | ||
|
||
} | ||
|
||
@R2dbcRepository(dialect = Dialect.H2) | ||
interface TemplateRepository extends ReactorCrudRepository<Template, Long> { | ||
|
||
@Join("tags") | ||
@Override | ||
Mono<Template> findById(Long aLong); | ||
} | ||
|
||
@MappedEntity | ||
class Template { | ||
|
||
@Id | ||
@GeneratedValue | ||
Long id | ||
|
||
@NonNull | ||
String name | ||
|
||
@OneToMany(mappedBy = "id.template", cascade = CascadeType.ALL) | ||
Set<Tag> tags = [] as Set | ||
|
||
} | ||
|
||
@MappedEntity | ||
class Tag { | ||
|
||
@EmbeddedId | ||
TagPK id | ||
|
||
} | ||
|
||
@Embeddable | ||
class TagPK implements Serializable { | ||
|
||
@NonNull | ||
@Column(name = "tag") | ||
String tag | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "template_id") | ||
@Column(name = "template_id") | ||
Template template | ||
|
||
@Override | ||
boolean equals(o) { | ||
if (this.is(o)) { | ||
return true | ||
} | ||
if (getClass() != o.class) { | ||
return false | ||
} | ||
|
||
TagPK that = (TagPK) o | ||
|
||
if (tag != that.tag) { | ||
return false | ||
} | ||
return template == that.template | ||
} | ||
|
||
@Override | ||
int hashCode() { | ||
int result | ||
result = tag.hashCode() | ||
result = 31 * result + (template != null ? template.hashCode() : 0) | ||
return result | ||
} | ||
} | ||
|