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 @@ -254,9 +254,6 @@ protected Map<EntityMappingType, List<EntityKey>> segmentLoadedValues() {
final Map<EntityMappingType, List<EntityKey>> map = new IdentityHashMap<>();
LockingHelper.segmentLoadedValues( loadedValuesCollector.getCollectedRootEntities(), map );
LockingHelper.segmentLoadedValues( loadedValuesCollector.getCollectedNonRootEntities(), map );
if ( map.isEmpty() ) {
throw new AssertionFailure( "Expecting some values" );
}
return map;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.locking;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import org.hibernate.LockMode;
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 java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@DomainModel(
annotatedClasses = {
UpgradeNoWaitLockTest.Parent.class,
UpgradeNoWaitLockTest.Child.class
}
)
@SessionFactory
public class UpgradeNoWaitLockTest {

@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.getSessionFactory().getSchemaManager().truncateMappedObjects();
}

@Test
@JiraKey( "HHH-19924" )
public void testFindIdOfNotPersistedEntity(SessionFactoryScope scope) {
Child child = new Child( 1L, "And" );
scope.inTransaction(
session ->
session.persist( child )
);

scope.inTransaction(
session -> {
Child c = session.find( Child.class, 2L, LockMode.UPGRADE_NOWAIT );
assertThat( c ).isNull();
}
);
}

@Test
@JiraKey( "HHH-19924" )
public void testFindIdOfPersistedEntity(SessionFactoryScope scope) {
long childId = 1L;
Child child = new Child( childId, "And" );
scope.inTransaction(
session ->
session.persist( child )
);

scope.inTransaction(
session -> {
Child c = session.find( Child.class, childId, LockMode.UPGRADE_NOWAIT );
assertThat( c ).isNotNull();
assertThat( c.getId() ).isEqualTo( childId );
}
);
}

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

@Id
private Long id;

private String name;

@OneToMany(fetch = FetchType.EAGER)
public List<Child> children = new ArrayList<>();

public Parent() {
}

public Parent(Long id, String name) {
this.id = id;
this.name = name;
}

public void add(Child child) {
children.add( child );
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public List<Child> getChildren() {
return children;
}
}


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

@Id
private Long id;

public String name;

@ManyToOne
public Parent parent;

public Child() {
}

public Child(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Parent getParent() {
return parent;
}
}
}