Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing embedded beans #1248

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2017-2020 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.jdbc.h2

import groovy.transform.EqualsAndHashCode
import io.micronaut.core.annotation.Introspected
import io.micronaut.core.annotation.Nullable
import io.micronaut.data.annotation.Embeddable
import io.micronaut.data.annotation.Id
import io.micronaut.data.jdbc.annotation.JdbcRepository
import io.micronaut.data.repository.CrudRepository
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import jakarta.persistence.Embedded
import jakarta.persistence.Entity
import spock.lang.Shared
import spock.lang.Specification

import static io.micronaut.data.model.query.builder.sql.Dialect.H2

@MicronautTest
@H2DBProperties
class H2Embedded2Spec extends Specification {

@Inject
@Shared
FooRepo repo

def filledInnerCanBeRetrieved() {
when:
var saved = repo.save(new Foo(0, new Bar("1", "2")))
var found = repo.findById(saved.id).get()
then:
found.bar == new Bar("1", "2")
}

void partiallyFilledInnerCanBeRetrieved() {
when:
var saved = repo.save(new Foo(0, new Bar("1", null)))
var found = repo.findById(saved.id).get()
then:
found.bar == new Bar("1", null)
}

}

@JdbcRepository(dialect = H2)
interface FooRepo extends CrudRepository<Foo, Integer> {
}

@EqualsAndHashCode
@Embeddable
@Introspected
class Bar {

String bar1
@Nullable
String bar2

Bar(String bar1, @Nullable String bar2) {
this.bar1 = bar1
this.bar2 = bar2
}

}

@EqualsAndHashCode
@Entity
@Introspected
class Foo {

@Id
int id

// If not marked as @Nullable object creation fails
@Nullable
@Embedded
Bar bar

Foo(int id, @Nullable Bar bar) {
this.id = id
this.bar = bar
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,11 @@ private <K> K readEntity(RS rs, MappingContext<K> ctx, @Nullable Object parent,
} else {
v = readProperty(rs, ctx, prop);
if (v == null) {
AnnotationMetadata entityAnnotationMetadata = ctx.persistentEntity.getAnnotationMetadata();
if (entityAnnotationMetadata.hasAnnotation(Embeddable.class) || entityAnnotationMetadata.hasAnnotation(EmbeddedId.class)) {
return null;
} else if (!prop.isOptional() && !nullableEmbedded) {
if (!prop.isOptional() && !nullableEmbedded) {
AnnotationMetadata entityAnnotationMetadata = ctx.persistentEntity.getAnnotationMetadata();
if (entityAnnotationMetadata.hasAnnotation(Embeddable.class) || entityAnnotationMetadata.hasAnnotation(EmbeddedId.class)) {
return null;
}
throw new DataAccessException("Null value read for non-null constructor argument [" + prop.getName() + "] of type: " + persistentEntity.getName());
} else {
args[i] = null;
Expand Down