/
MyEntityRoutingKeyBinder.java
49 lines (43 loc) · 1.76 KB
/
MyEntityRoutingKeyBinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.documentation.mapper.orm.bridge.routingkeybridge.ormcontext;
import org.hibernate.Session;
import org.hibernate.search.mapper.orm.HibernateOrmExtension;
import org.hibernate.search.mapper.pojo.bridge.RoutingKeyBridge;
import org.hibernate.search.mapper.pojo.bridge.binding.RoutingKeyBindingContext;
import org.hibernate.search.mapper.pojo.bridge.mapping.programmatic.RoutingKeyBinder;
import org.hibernate.search.mapper.pojo.bridge.runtime.RoutingKeyBridgeToRoutingKeyContext;
public class MyEntityRoutingKeyBinder implements RoutingKeyBinder {
@Override
public void bind(RoutingKeyBindingContext context) {
context.dependencies()
.useRootOnly();
context.bridge( new Bridge() );
}
//tag::include[]
private static class Bridge implements RoutingKeyBridge {
@Override
public String toRoutingKey(String tenantIdentifier, Object entityIdentifier,
Object bridgedElement, RoutingKeyBridgeToRoutingKeyContext context) {
Session session = context.extension( HibernateOrmExtension.get() ) // <1>
.session(); // <2>
String routingKey;
// ... do something with the session ...
//end::include[]
/*
* I don't know what to do with the session here,
* so I'm just going to extract data from it.
* This is silly, but at least it allows us to check the session was successfully retrieved.
*/
MyData dataFromSession = (MyData) session.getProperties().get( "test.data.indexed" );
routingKey = dataFromSession.name();
//tag::include[]
return routingKey;
}
}
//end::include[]
}