I've been talking about it in the last posts and it finally is done. I streamlined the whole startup process to not require extending a specific class. Before I go into detail on how this is done, let's take a look into what dependencies are needed to use Hibernate Search GenericJPA:
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-search-jpa</artifactId> | |
</dependency> | |
<!-- This is only needed in a Java EE environment --> | |
<dependency> | |
<groupId>org.hibernate</groupId> | |
<artifactId>hibernate-search-ejb</artifactId> | |
</dependency> |
The "hibernate-serach-jpa" module contains all the code needed to enable Hibernate Search in your JPA based application. The "hibernate-search-ejb" module contains the additional startup logic for Java EE containers (currently only default named EMF and properties @/META-INF/hsearch.properties are supported in this module. If you need different behaviour, you can just startup your Search module like in the Java SE case).
Starting up the Hibernate Search GenericJPA
Now, let's take a look into how we start this whole thing up in a Java SE environment:
Properties properties = new Properties(); | |
properties.setProperty( "org.hibernate.search.genericjpa.searchfactory.triggerSource", "org.hibernate.search.genericjpa.db.events.MySQLTriggerSQLStringSource" ); | |
properties.setProperty( "org.hibernate.search.genericjpa.searchfactory.type", "sql"); | |
JPASearchFactoryController searchFactoryController = Setup.createUnmanagedSearchFactory( emf, properties ); | |
//... | |
//use the searchFactoryController | |
//... | |
//in Java SE the user has to close this! | |
searchFactoryController.close(); |
If you are using the EJB module in a Java EE context, the startup is already done for you and you can just inject it like this:
@Inject | |
private JPASearchFactoryController searchFactoryController; |
Configuration
No matter where your properties come from (manually specified or loaded from the EJB module), you will have to define some settings:
#must be set | |
org.hibernate.search.genericjpa.searchfactory.triggerSource=org.hibernate.search.genericjpa.db.events.MySQLTriggerSQLStringSource | |
#must be set ('sql' or 'manual-updates') | |
org.hibernate.search.genericjpa.searchfactory.type=sql | |
#this is a really low value, you might want to set it to a higher value | |
org.hibernate.search.genericjpa.searchfactory.batchSizeForUpdates=2 | |
#the time between checks for updates in ms | |
org.hibernate.search.genericjpa.searchfactory.updateDelay=100 | |
#default is 'false' | |
org.hibernate.search.genericjpa.searchfactory.useJTATransactions=false | |
#the name of your searchfactory, as of now this doesn't have any impact, default is 'default' | |
org.hibernate.search.genericjpa.searchfactory.name=default |
#Wildfly | |
org.hibernate.serach.genericjpa.searchfactory.transactionManagerProvider.jndi=java:jboss/TransactionManager | |
#TomEE | |
org.hibernate.serach.genericjpa.searchfactory.transactionManagerProvider.jndi=java:comp/TransactionManager | |
#GlassFish | |
org.hibernate.serach.genericjpa.searchfactory.transactionManagerProvider.jndi=java:appserver/TransactionManager |
This list might change in the future and I might have forgotten some properties in this posting. For a complete overview, you can always take a look at the Constants class
Enjoyed your approach to explaining how it works, hope to see more blog posts from you. thank you!
AntwortenLöschenHibernate Online Training | Java Online Training
Hibernate Training in Chennai Java Training in Chennai