Skip to content

Commit 2ecfdbd

Browse files
committed
HSEARCH-5226 Ignore collecting scale on formula properties
(cherry picked from commit 801c8ac)
1 parent 9e5f8cf commit 2ecfdbd

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.integrationtest.mapper.orm.model;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;
11+
12+
import java.util.HashSet;
13+
import java.util.Set;
14+
15+
import jakarta.persistence.Entity;
16+
import jakarta.persistence.Id;
17+
import jakarta.persistence.ManyToOne;
18+
import jakarta.persistence.OneToMany;
19+
20+
import org.hibernate.SessionFactory;
21+
import org.hibernate.annotations.Formula;
22+
import org.hibernate.search.engine.backend.analysis.AnalyzerNames;
23+
import org.hibernate.search.mapper.pojo.automaticindexing.ReindexOnUpdate;
24+
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
25+
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
26+
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexedEmbedded;
27+
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexingDependency;
28+
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;
29+
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
30+
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.extension.RegisterExtension;
34+
35+
class FormulaPropertyIT {
36+
37+
@RegisterExtension
38+
public BackendMock backendMock = BackendMock.create();
39+
40+
@RegisterExtension
41+
public OrmSetupHelper ormSetupHelper = OrmSetupHelper.withBackendMock( backendMock );
42+
43+
private SessionFactory sessionFactory;
44+
45+
@BeforeEach
46+
void setup() {
47+
backendMock.expectSchema( IndexedEntity.INDEX, b -> b
48+
.field( "string", String.class, b2 -> b2.analyzerName( AnalyzerNames.DEFAULT ) )
49+
).expectSchema( RootEntity.INDEX, b -> b
50+
.objectField( "entityShallow",
51+
b1 -> b1.field( "string", String.class, b2 -> b2.analyzerName( AnalyzerNames.DEFAULT ) ) )
52+
.objectField( "entityMapped",
53+
b1 -> b1.field( "string", String.class, b2 -> b2.analyzerName( AnalyzerNames.DEFAULT ) ) ) );
54+
55+
sessionFactory = ormSetupHelper.start()
56+
.setup( IndexedEntity.class, RootEntity.class );
57+
backendMock.verifyExpectationsMet();
58+
}
59+
60+
@Test
61+
void index() {
62+
with( sessionFactory ).runInTransaction( session -> {
63+
IndexedEntity entity1 = new IndexedEntity();
64+
entity1.id = 1;
65+
entity1.string = "smth";
66+
entity1.amount1 = 10;
67+
entity1.amount2 = 20;
68+
69+
RootEntity rootEntity = new RootEntity();
70+
rootEntity.id = 1;
71+
rootEntity.entityShallow = entity1;
72+
rootEntity.entityMapped = entity1;
73+
entity1.rootEntities.add( rootEntity );
74+
75+
session.persist( entity1 );
76+
session.persist( rootEntity );
77+
78+
backendMock.expectWorks( IndexedEntity.INDEX )
79+
.add( "1", b -> b
80+
.field( "string", "smth" )
81+
);
82+
backendMock.expectWorks( RootEntity.INDEX )
83+
.add( "1", b -> b
84+
.objectField( "entityShallow", b1 -> b1.field( "string", "smth" ) )
85+
.objectField( "entityMapped", b1 -> b1.field( "string", "smth" ) )
86+
);
87+
} );
88+
with( sessionFactory ).runInTransaction( session -> {
89+
IndexedEntity entity1 = session.get( IndexedEntity.class, 1 );
90+
assertThat( entity1.string ).isEqualTo( "smth" );
91+
assertThat( entity1.amountDifference ).isEqualTo( 10 );
92+
} );
93+
}
94+
95+
@Entity(name = "root_entity")
96+
@Indexed(index = RootEntity.INDEX)
97+
public static final class RootEntity {
98+
99+
static final String INDEX = "RootEntity";
100+
101+
@Id
102+
public Integer id;
103+
104+
@IndexedEmbedded
105+
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW)
106+
@ManyToOne
107+
public IndexedEntity entityShallow;
108+
109+
@IndexedEmbedded
110+
@ManyToOne
111+
public IndexedEntity entityMapped;
112+
113+
}
114+
115+
@Entity(name = "indexed")
116+
@Indexed(index = IndexedEntity.INDEX)
117+
public static final class IndexedEntity {
118+
119+
static final String INDEX = "IndexedEntity";
120+
121+
@Id
122+
public Integer id;
123+
124+
@FullTextField
125+
public String string;
126+
127+
public int amount1;
128+
public int amount2;
129+
130+
@Formula("amount2 - amount1")
131+
public int amountDifference;
132+
133+
@OneToMany(mappedBy = "entityMapped")
134+
Set<RootEntity> rootEntities = new HashSet<>();
135+
136+
}
137+
}

mapper/orm/src/main/java/org/hibernate/search/mapper/orm/mapping/impl/HibernateOrmMappingPropertiesMetadataContributor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ else if ( value instanceof SimpleValue ) {
9797
}
9898

9999
private void collectScale(PojoAdditionalMetadataCollectorPropertyNode collector, Value value) {
100+
// If it is a formula we have nothing to collect.
101+
// Additionally, starting with ORM 6.0 this will start causing AssertionFailure exceptions,
102+
// see org.hibernate.mapping.SimpleValue#getColumns()
103+
if ( value.hasFormula() ) {
104+
return;
105+
}
100106
Iterator<Column> columnIterator = value.getColumns().iterator();
101107
Dialect dialect = basicTypeMetadataProvider.getDialect();
102108
Metadata metadata = basicTypeMetadataProvider.getMetadata();

0 commit comments

Comments
 (0)