Skip to content
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 @@ -107,6 +107,7 @@
import org.hibernate.graph.spi.RootGraphImplementor;
import org.hibernate.id.Assigned;
import org.hibernate.id.BulkInsertionCapableIdentifierGenerator;
import org.hibernate.id.ForeignGenerator;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.OptimizableGenerator;
import org.hibernate.id.PostInsertIdentityPersister;
Expand Down Expand Up @@ -4124,13 +4125,16 @@ public Boolean isTransient(Object entity, SharedSessionContractImplementor sessi
return false;
}
}
final Boolean unsaved = identifierMapping.getUnsavedStrategy().isUnsaved( id );
if ( unsaved != null && !unsaved ) {
throw new PropertyValueException(
"Detached entity with generated id '" + id + "' has an uninitialized version value '" + version + "'",
getEntityName(),
getVersionColumnName()
);
final Generator identifierGenerator = getGenerator();
if ( identifierGenerator != null && !( identifierGenerator instanceof ForeignGenerator ) ) {
final Boolean unsaved = identifierMapping.getUnsavedStrategy().isUnsaved( id );
if ( unsaved != null && !unsaved ) {
throw new PropertyValueException(
"Detached entity with generated id '" + id + "' has an uninitialized version value '" + version + "'",
getEntityName(),
getVersionColumnName()
);
}
}
}
return isUnsaved;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.hibernate.orm.test.version;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Version;

@DomainModel(
annotatedClasses = {
EntityWithNullVersionAndMapsIdTest.Person.class,
EntityWithNullVersionAndMapsIdTest.Address.class,
}
)
@SessionFactory
@JiraKey("HHH-17380")
public class EntityWithNullVersionAndMapsIdTest {

@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createMutationQuery( "delete from Person" );
session.createMutationQuery( "delete from Address" );

}
);
}

@Test
public void testPersistEntityWithMapsId(SessionFactoryScope scope) {
Address address = new Address( "Alex" );
scope.inTransaction(
session -> {
session.persist( address );
Person person = new Person( address );
session.persist( person );
}
);
}

@Entity(name = "Person")
public static class Person {

@Id
private String name;

@Version
private Long version;

private String description;

@MapsId
@OneToOne
@JoinColumn(name = "name")
private Address address;

public Person() {
}

public Person(Address address) {
this.name = address.getName();
this.address = address;
}

public String getName() {
return name;
}

public Address getAddress() {
return address;
}
}

@Entity(name = "Address")
public static class Address {
@Id
private String name;

@Version
private Long version;

private String description;

public Address() {
}

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

public String getName() {
return name;
}
}
}