Skip to content

ClassCastException when using embeddable ids #2067

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

Merged
merged 2 commits into from
Jan 20, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.hibernate.sql.results.graph.InitializerParent;
import org.hibernate.sql.results.graph.embeddable.EmbeddableInitializer;
import org.hibernate.sql.results.graph.embeddable.internal.EmbeddableForeignKeyResultImpl;
import org.hibernate.sql.results.graph.embeddable.internal.EmbeddableInitializerImpl;

public class ReactiveEmbeddableForeignKeyResultImpl<T> extends EmbeddableForeignKeyResultImpl<T> {

Expand All @@ -22,6 +21,6 @@ public ReactiveEmbeddableForeignKeyResultImpl(EmbeddableForeignKeyResultImpl<T>
public EmbeddableInitializer<?> createInitializer(InitializerParent parent, AssemblerCreationState creationState) {
return getReferencedModePart() instanceof NonAggregatedIdentifierMapping
? new ReactiveNonAggregatedIdentifierMappingInitializer( this, null, creationState, true )
: new EmbeddableInitializerImpl( this, null, null, creationState, true );
: new ReactiveEmbeddableInitializerImpl( this, null, null, creationState, true );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

public class EmbeddedIdWithManyTest extends BaseReactiveTest {

@Override
protected Collection<Class<?>> annotatedEntities() {
return List.of( Flower.class, Fruit.class );
}

@BeforeEach
public void populateDb(VertxTestContext context) {
Seed seed1 = new Seed( 1 );
Flower rose = new Flower( seed1, "Rose" );

Fruit cherry = new Fruit( seed1, "Cherry" );
cherry.addFriend( rose );

Seed seed2 = new Seed( 2 );
Flower sunflower = new Flower( seed2, "Sunflower" );

Fruit apple = new Fruit( seed2, "Apple" );
apple.addFriend( sunflower );

Seed seed3 = new Seed( 3 );
Flower chrysanthemum = new Flower( seed3, "Chrysanthemum" );

Fruit banana = new Fruit( seed3, "Banana" );
banana.addFriend( chrysanthemum );

test(
context,
getMutinySessionFactory().withTransaction( s -> s
.persistAll( cherry, rose, sunflower, apple, chrysanthemum, banana )
)
);
}

@Test
public void test(VertxTestContext context) {
test(
context, getMutinySessionFactory().withTransaction( s -> s
.createSelectionQuery( "from Flower", Flower.class )
.getResultList()
)
);
}

@Embeddable
public static class Seed {

@Column(nullable = false, updatable = false)
private Integer id;

public Seed() {
}

public Seed(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

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

@MappedSuperclass
public static abstract class Plant {

@EmbeddedId
private Seed seed;

@Column(length = 40, unique = true)
private String name;

protected Plant() {
}

protected Plant(Seed seed, String name) {
this.seed = seed;
this.name = name;
}

public Seed getSeed() {
return seed;
}

public void setSeed(Seed seed) {
this.seed = seed;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

@Entity(name = "Fruit")
@Table(name = "known_fruits")
public static class Fruit extends Plant {

@OneToMany(mappedBy = "friend", fetch = FetchType.LAZY)
private List<Flower> friends = new ArrayList<>();

public Fruit() {
}

public Fruit(Seed seed, String name) {
super( seed, name );
}

public void addFriend(Flower flower) {
this.friends.add( flower );
flower.friend = this;
}

public List<Flower> getFriends() {
return friends;
}

@Override
public String toString() {
return "Fruit{" + getSeed().getId() + "," + getName() + '}';
}

}

@Entity(name = "Flower")
@Table(name = "known_flowers")
public static class Flower extends Plant {

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "friend", referencedColumnName = "id", nullable = false)
private Fruit friend;

public Flower() {
}

public Flower(Seed seed, String name) {
super( seed, name );
}

@Override
public String toString() {
return "Flower{" + getSeed().getId() + "," + getName() + '}';
}

}

}
Loading