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

IdClass with ManyToOne #2005

Merged
merged 2 commits into from
Oct 2, 2024
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 @@ -7,6 +7,8 @@

import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.mapping.NonAggregatedIdentifierMapping;
import org.hibernate.metamodel.mapping.internal.ToOneAttributeMapping;
import org.hibernate.reactive.metamodel.mapping.internal.ReactiveToOneAttributeMapping;
import org.hibernate.reactive.sql.results.graph.entity.internal.ReactiveEntityFetchJoinedImpl;
import org.hibernate.spi.NavigablePath;
import org.hibernate.sql.results.graph.AssemblerCreationState;
Expand Down Expand Up @@ -50,8 +52,15 @@ public Fetch generateFetchableFetch(
boolean selected,
String resultVariable,
DomainResultCreationState creationState) {
// I don't think this is the right approach.
// Fetchable should already be an instance of ReactiveToOneAttributeMapping
// (if it's an instance of ToOneAttributeMapping). But I don't think there is a way right now
// to make it happen without changing ORM.
Fetchable reactiveFetchable = fetchable instanceof ToOneAttributeMapping && !( fetchable instanceof ReactiveToOneAttributeMapping )
? new ReactiveToOneAttributeMapping( (ToOneAttributeMapping) fetchable )
: fetchable;
Fetch fetch = super.generateFetchableFetch(
fetchable,
reactiveFetchable,
fetchablePath,
fetchTiming,
selected,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,320 @@
/* 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.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import org.junit.jupiter.api.Test;

import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;

@Timeout(value = 10, timeUnit = MINUTES)
public class ManyToOneLazyIdClassTest extends BaseReactiveTest {

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

@Test
public void test(VertxTestContext context) {
final UUID aId = UUID.randomUUID();
final EntityA entityA = new EntityA( aId, "testA" );
final UUID bId = UUID.randomUUID();
final EntityB entityB = new EntityB( bId, "testB" );
final Relationship relationship = new Relationship( entityA, entityB, "testRelationship" );

test( context, getMutinySessionFactory()
.withTransaction( s -> s.persistAll( entityA, entityB, relationship ) )
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
.find( EntityA.class, aId )
.invoke( entity -> assertThat( entity ).isEqualTo( entityA ) )
) )
.chain( () -> getMutinySessionFactory().withTransaction( s -> s
.find( EntityB.class, bId )
.invoke( entity -> assertThat( entity ).isEqualTo( entityB ) )
) )
.chain( () -> getMutinySessionFactory().withTransaction( s -> s.find(
Relationship.class,
new RelationshipId( aId, bId )
) ) )
);
}

@Entity
@Table(name = "entity_a")
public static class EntityA {

@Id
@Column(name = "id")
private UUID id;

@Column(name = "name")
private String name;

public EntityA(UUID id, String name) {
this.id = id;
this.name = name;
}

public EntityA() {
}

public UUID getId() {
return id;
}

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

public String getName() {
return name;
}

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

@Override
public String toString() {
return id + ":" + name;
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
EntityA entityA = (EntityA) o;
return Objects.equals( id, entityA.id ) && Objects.equals( name, entityA.name );
}

@Override
public int hashCode() {
return Objects.hash( id, name );
}
}

@Entity
@Table(name = "entity_b")
public static class EntityB {

@Id
@Column(name = "id")
private UUID id;

@Column(name = "name")
private String name;

public EntityB(UUID id, String name) {
this.id = id;
this.name = name;
}

public EntityB() {
}

public UUID getId() {
return id;
}

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

public String getName() {
return name;
}

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

@Override
public String toString() {
return id + ":" + name;
}

@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
EntityB entityB = (EntityB) o;
return Objects.equals( id, entityB.id ) && Objects.equals( name, entityB.name );
}

@Override
public int hashCode() {
return Objects.hash( id, name );
}
}

@Entity( name = "Relationship")
@Table(name = "relationship")
@IdClass(RelationshipId.class)
public static class Relationship {

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "a_id", referencedColumnName = "id")
private EntityA entityA;

@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "b_id", referencedColumnName = "id")
private EntityB entityB;

@Column(name = "dataField")
private String dataField;

public Relationship(EntityA entityA, EntityB entityB, String dataField) {
this.entityA = entityA;
this.entityB = entityB;
this.dataField = dataField;
}

public Relationship() {
}

public EntityA getEntityA() {
return entityA;
}

public void setEntityA(EntityA entityA) {
this.entityA = entityA;
}

public EntityB getEntityB() {
return entityB;
}

public void setEntityB(EntityB entityB) {
this.entityB = entityB;
}

public String getDataField() {
return dataField;
}

public void setDataField(String dataField) {
this.dataField = dataField;
}

@Override
public String toString() {
return entityA + "-" + entityB + ":" + dataField;
}

@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( !( obj instanceof Relationship ) ) {
return false;
}
Relationship other = (Relationship) obj;
if ( entityA == null
|| other.getEntityA() == null
|| entityB == null
|| other.getEntityB() == null
) {
return false;
}
return entityA.equals( other.getEntityA() )
&& entityB.equals( other.getEntityB() );
}

@Override
public int hashCode() {
return Objects.hash( entityA, entityB );
}
}

public static class RelationshipId {

private UUID entityA;
private UUID entityB;

public RelationshipId() {
}

public RelationshipId(UUID entityA, UUID entityB) {
this.entityA = entityA;
this.entityB = entityB;
}

public UUID getEntityA() {
return entityA;
}

public void setEntityA(UUID entityA) {
this.entityA = entityA;
}

public UUID getEntityB() {
return entityB;
}

public void setEntityB(UUID entityB) {
this.entityB = entityB;
}

@Override
public String toString() {
return entityA + ":" + entityB;
}

@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( !( obj instanceof RelationshipId ) ) {
return false;
}
RelationshipId other = (RelationshipId) obj;
if ( entityA == null
|| other.getEntityA() == null
|| entityB == null
|| other.getEntityB() == null
) {
return false;
}
return entityA.equals( other.getEntityA() )
&& entityB.equals( other.getEntityB() );
}

@Override
public int hashCode() {
return Objects.hash( entityA, entityB );
}
}

}
Loading