Skip to content

Commit ae00af9

Browse files
committed
HHH-19672 - Add overloads of #find accepting entity-name
1 parent d12704c commit ae00af9

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

hibernate-core/src/main/java/org/hibernate/Session.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,28 @@ public interface Session extends SharedSessionContract, EntityManager {
527527
@Override
528528
<T> T find(Class<T> entityType, Object id);
529529

530+
/**
531+
* Return the persistent instance of the named entity type with the given identifier,
532+
* or null if there is no such persistent instance.
533+
* <p/>
534+
* Differs from {@linkplain #find(Class, Object)} in that this form accepts
535+
* the entity name of a {@linkplain org.hibernate.metamodel.RepresentationMode#MAP dynamic entity}.
536+
*
537+
* @see #find(Class, Object)
538+
*/
539+
Object find(String entityName, Object primaryKey);
540+
541+
/**
542+
* Return the persistent instance of the named entity type with the given identifier
543+
* using the specified options, or null if there is no such persistent instance.
544+
* <p/>
545+
* Differs from {@linkplain #find(Class, Object, FindOption...)} in that this form accepts
546+
* the entity name of a {@linkplain org.hibernate.metamodel.RepresentationMode#MAP dynamic entity}.
547+
*
548+
* @see #find(Class, Object, FindOption...)
549+
*/
550+
Object find(String entityName, Object primaryKey, FindOption... options);
551+
530552
/**
531553
* Return the persistent instances of the given entity class with the given identifiers
532554
* as a list. The position of an instance in the returned list matches the position of its

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,16 @@ public <T> T find(EntityGraph<T> entityGraph, Object primaryKey, FindOption... o
875875
return delegate.find( entityGraph, primaryKey, options );
876876
}
877877

878+
@Override
879+
public Object find(String entityName, Object primaryKey) {
880+
return delegate.find( entityName, primaryKey );
881+
}
882+
883+
@Override
884+
public Object find(String entityName, Object primaryKey, FindOption... options) {
885+
return delegate.find( entityName, primaryKey, options );
886+
}
887+
878888
@Override
879889
public <T> T getReference(Class<T> entityClass, Object id) {
880890
return delegate.getReference( entityClass, id );

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionLazyDelegator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,16 @@ public <T> T find(EntityGraph<T> entityGraph, Object primaryKey, FindOption... o
804804
return this.lazySession.get().find( entityGraph, primaryKey, options );
805805
}
806806

807+
@Override
808+
public Object find(String entityName, Object primaryKey) {
809+
return this.lazySession.get().find( entityName, primaryKey );
810+
}
811+
812+
@Override
813+
public Object find(String entityName, Object primaryKey, FindOption... options) {
814+
return this.lazySession.get().find( entityName, primaryKey, options );
815+
}
816+
807817
@Override
808818
public void lock(Object entity, LockModeType lockMode) {
809819
this.lazySession.get().lock( entity, lockMode );

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,6 +2598,19 @@ private void checkTransactionNeededForUpdateOperation() {
25982598
checkTransactionNeededForUpdateOperation( "No active transaction" );
25992599
}
26002600

2601+
@Override
2602+
public Object find(String entityName, Object primaryKey) {
2603+
final IdentifierLoadAccessImpl<Object> loadAccess = byId( entityName );
2604+
return loadAccess.load( primaryKey );
2605+
}
2606+
2607+
@Override
2608+
public Object find(String entityName, Object primaryKey, FindOption... options) {
2609+
final IdentifierLoadAccessImpl<Object> loadAccess = byId( entityName );
2610+
setLoadAccessOptions( options, loadAccess );
2611+
return loadAccess.load( primaryKey );
2612+
}
2613+
26012614
@Override
26022615
public <T> T getReference(Class<T> entityClass, Object id) {
26032616
checkOpen();
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.dynamicmap;
6+
7+
import org.hibernate.ReadOnlyMode;
8+
import org.hibernate.graph.RootGraph;
9+
import org.hibernate.testing.orm.junit.DomainModel;
10+
import org.hibernate.testing.orm.junit.SessionFactory;
11+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
12+
import org.junit.jupiter.api.AfterEach;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
/**
22+
* @author Steve Ebersole
23+
*/
24+
@SuppressWarnings("JUnitMalformedDeclaration")
25+
@DomainModel(xmlMappings = "org/hibernate/orm/test/dynamicmap/artist.xml")
26+
@SessionFactory
27+
public class FindOperationTests {
28+
@BeforeEach
29+
void setUp(SessionFactoryScope factoryScope) {
30+
factoryScope.inTransaction( (session) -> {
31+
final Map<String,Object> entity = new HashMap<>();
32+
entity.put( "id", 1 );
33+
entity.put( "name", "Led Zepplin" );
34+
session.persist( "artist", entity );
35+
} );
36+
}
37+
38+
@AfterEach
39+
void tearDown(SessionFactoryScope factoryScope) {
40+
factoryScope.dropData();
41+
}
42+
43+
@Test
44+
void testSimpleFind(SessionFactoryScope factoryScope) {
45+
factoryScope.inTransaction( (session) -> {
46+
final Object artist = session.find( "artist", 1 );
47+
checkResult( artist );
48+
49+
} );
50+
}
51+
52+
@Test
53+
void testFindWithOptions(SessionFactoryScope factoryScope) {
54+
factoryScope.inTransaction( (session) -> {
55+
final Object artist = session.find( "artist", 1, ReadOnlyMode.READ_ONLY );
56+
checkResult( artist );
57+
assertThat( session.isReadOnly( artist ) ).isTrue();
58+
} );
59+
}
60+
61+
@Test
62+
void testFindWithGraph(SessionFactoryScope factoryScope) {
63+
factoryScope.inTransaction( (session) -> {
64+
final RootGraph<Map<String, ?>> artistGraph = session.getFactory().createGraphForDynamicEntity( "artist" );
65+
artistGraph.addAttributeNodes( "id", "name" );
66+
final Object artist = session.find( artistGraph, 1, ReadOnlyMode.READ_ONLY );
67+
checkResult( artist );
68+
assertThat( session.isReadOnly( artist ) ).isTrue();
69+
} );
70+
}
71+
72+
private void checkResult(Object incoming) {
73+
assertThat( incoming ).isNotNull();
74+
75+
//noinspection unchecked
76+
final Map<String, Object> artist = (Map<String, Object>) incoming;
77+
assertThat( artist.get( "id" ) ).isEqualTo( 1 );
78+
assertThat( artist.get( "name" ) ).isEqualTo( "Led Zepplin" );
79+
}
80+
}

0 commit comments

Comments
 (0)