Skip to content

PropertyAccessException when creating a new object with a one-to-one association #2081

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 23, 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 @@ -182,20 +182,21 @@ private CompletionStage<Object> generateId(
EntityPersister persister) {
return generator
.generate( (ReactiveConnectionSupplier) source, entity )
.thenApply( id -> castToIdentifierType( id, persister ) )
.thenCompose( generatedId -> {
if ( generatedId == null ) {
return failedFuture( new IdentifierGenerationException( "null id generated for: " + entity.getClass() ) );
}
if ( LOG.isDebugEnabled() ) {
LOG.debugf(
"Generated identifier: %s, using strategy: %s",
persister.getIdentifierType().toLoggableString( generatedId, source.getFactory() ),
generator.getClass().getName()
);
}
return completedFuture( generatedId );
} );
.thenApply( id -> {
final Object generatedId = castToIdentifierType( id, persister );
if ( generatedId == null ) {
throw new IdentifierGenerationException( "null id generated for: " + entity.getClass() );
}
if ( LOG.isDebugEnabled() ) {
LOG.debugf(
"Generated identifier: %s, using strategy: %s",
persister.getIdentifierType().toLoggableString( generatedId, source.getFactory() ),
generator.getClass().getName()
);
}
return generatedId;
}
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/* 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 org.junit.jupiter.api.Test;

import io.vertx.junit5.Timeout;
import io.vertx.junit5.VertxTestContext;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;

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

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

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

@Test
public void testPersist(VertxTestContext context) {
NaturalPerson naturalPerson = new NaturalPerson( "natual" );
Person person = new Person( "person", naturalPerson );

test(
context, getMutinySessionFactory()
.withTransaction( session -> session.persist( person ) )
.chain( () -> getMutinySessionFactory()
.withTransaction( session -> session
.find( Person.class, person.getId() )
.invoke( result -> {
assertThat( result ).isNotNull();
assertThat( result.getNaturalPerson() ).isNotNull();
assertThat( result.getNaturalPerson().getId() ).isEqualTo( result.getId() );
} ) )
)
);
}

@Entity
public static class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

@OneToOne(mappedBy = "person", cascade = CascadeType.ALL)
private NaturalPerson naturalPerson;

public Person() {
}

public Person(String name, NaturalPerson naturalPerson) {
this.name = name;
this.naturalPerson = naturalPerson;
naturalPerson.person = this;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public NaturalPerson getNaturalPerson() {
return naturalPerson;
}

@Override
public boolean equals(Object o) {
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Person person = (Person) o;
return Objects.equals( name, person.name );
}

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

@Entity
public static class NaturalPerson {

@Id
private Long id;

@Column
private String name;

@OneToOne(fetch = FetchType.LAZY)
@MapsId
private Person person;

public NaturalPerson() {
}

public NaturalPerson(String name) {
this.name = name;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Person getPerson() {
return person;
}

@Override
public boolean equals(Object o) {
if ( o == null || getClass() != o.getClass() ) {
return false;
}
NaturalPerson that = (NaturalPerson) o;
return Objects.equals( name, that.name );
}

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


}
Loading