Skip to content

Commit

Permalink
Ignore bridge methods when checking for @embeddable annotation on @Em…
Browse files Browse the repository at this point in the history
…bedded/@EmbeddedId
  • Loading branch information
yrodiere committed Oct 26, 2023
1 parent 1f4061c commit 83cd68b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ private void enlistEmbeddedsAndElementCollections(Collector collector) throws Bu
break;
case METHOD:
var method = target.asMethod();
if (method.isBridge()) {
// Generated by javac for covariant return type override.
// There's another method with a more specific return type, ignore this one.
continue;
}
collectEmbeddedType(embeddedTypes, method.declaringClass(), method, method.returnType(), true);
break;
default:
Expand All @@ -343,6 +348,11 @@ private void enlistEmbeddedsAndElementCollections(Collector collector) throws Bu
break;
case METHOD:
var method = target.asMethod();
if (method.isBridge()) {
// Generated by javac for covariant return type override.
// There's another method with a more specific return type, ignore this one.
continue;
}
collectElementCollectionTypes(embeddedTypes, method.declaringClass(), method, method.returnType());
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.GeneratedValue;
Expand All @@ -31,6 +32,9 @@
* Checks that the missing @Embeddable check doesn't mistakely report
* types that are annotated with @Embeddable (https://github.com/quarkusio/quarkus/issues/35598)
* or generic type parameters on @Embedded field types (https://github.com/quarkusio/quarkus/issues/36065)
* or overriden getters annotated with @EmbeddedId/@Embedded where the supertype getter returns a type not annotated
* with @Embeddable
* (https://github.com/quarkusio/quarkus/issues/36421).
*/
public class HibernateEntityEnhancerPresentEmbeddableTest {

Expand All @@ -41,7 +45,9 @@ public class HibernateEntityEnhancerPresentEmbeddableTest {
.addClasses(EntityWithEmbedded.class, EmbeddableWithAnnotation.class,
ExtendedEmbeddableWithAnnotation.class,
NestingEmbeddableWithAnnotation.class,
GenericEmbeddableWithAnnotation.class))
GenericEmbeddableWithAnnotation.class,
EntityWithEmbeddedId.class, EntityWithEmbeddedIdAndOverriddenGetter.class,
EmbeddableIdWithAnnotation.class))
.withConfigurationResource("application.properties")
.overrideConfigKey("quarkus.hibernate-orm.implicit-naming-strategy", "component-path");

Expand All @@ -50,7 +56,7 @@ public class HibernateEntityEnhancerPresentEmbeddableTest {

// Just test that the generic embeddeds work correctly over a persist/retrieve cycle
@Test
public void smokeTest() {
public void embedded_smokeTest() {
Long id = QuarkusTransaction.requiringNew().call(() -> {
EntityWithEmbedded entity = new EntityWithEmbedded();
entity.setName("name");
Expand Down Expand Up @@ -97,6 +103,36 @@ public void smokeTest() {
});
}

// Just test that the embeddedIds work correctly over a persist/retrieve cycle
@Test
public void embeddedId_smokeTest() {
QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedId entity1 = new EntityWithEmbeddedId();
entity1.setId(new EmbeddableIdWithAnnotation("1"));
em.persist(entity1);
});

QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedId entity = em.find(EntityWithEmbeddedId.class, new EmbeddableIdWithAnnotation("1"));
assertThat(entity).isNotNull();
});
}

@Test
public void embeddedIdAndOverriddenGetter_smokeTest() {
QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedIdAndOverriddenGetter entity1 = new EntityWithEmbeddedIdAndOverriddenGetter();
entity1.setId(new EmbeddableIdWithAnnotation("2"));
em.persist(entity1);
});

QuarkusTransaction.requiringNew().run(() -> {
EntityWithEmbeddedIdAndOverriddenGetter entity = em.find(EntityWithEmbeddedIdAndOverriddenGetter.class,
new EmbeddableIdWithAnnotation("2"));
assertThat(entity).isNotNull();
});
}

@Entity
public static class EntityWithEmbedded {

Expand Down Expand Up @@ -290,4 +326,55 @@ public void setValue(T value) {
}
}

@Entity
public static class EntityWithEmbeddedId {
@EmbeddedId
private EmbeddableIdWithAnnotation id;

public EmbeddableIdWithAnnotation getId() {
return id;
}

public void setId(EmbeddableIdWithAnnotation id) {
this.id = id;
}
}

@MappedSuperclass
public interface Identifiable {
Object getId();
}

@Entity
public static class EntityWithEmbeddedIdAndOverriddenGetter implements Identifiable {
private EmbeddableIdWithAnnotation id;

@Override
@EmbeddedId
public EmbeddableIdWithAnnotation getId() {
return id;
}

public void setId(EmbeddableIdWithAnnotation id) {
this.id = id;
}
}

@Embeddable
public static class EmbeddableIdWithAnnotation {
private String text;

protected EmbeddableIdWithAnnotation() {
// For Hibernate ORM only - it will change the property value through reflection
}

public EmbeddableIdWithAnnotation(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

}

0 comments on commit 83cd68b

Please sign in to comment.