Created
May 13, 2015 11:43
-
-
Save hferentschik/1c0708926c0e77cb310a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Map; | |
import acme.Film; | |
import org.hibernate.HibernateException; | |
import org.hibernate.Query; | |
import org.hibernate.Session; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.cfg.Configuration; | |
import org.hibernate.metadata.ClassMetadata; | |
import org.hibernate.search.FullTextQuery; | |
import org.hibernate.search.FullTextSession; | |
import org.hibernate.search.Search; | |
import org.hibernate.search.exception.SearchException; | |
import org.hibernate.search.query.dsl.QueryBuilder; | |
import org.hibernate.service.ServiceRegistry; | |
import org.hibernate.service.ServiceRegistryBuilder; | |
/** | |
* @author Hardy Ferentschik | |
*/ | |
public class Main { | |
private static final SessionFactory ourSessionFactory; | |
private static final ServiceRegistry serviceRegistry; | |
static { | |
try { | |
Configuration configuration = new Configuration(); | |
configuration.configure(); | |
serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties() ) | |
.buildServiceRegistry(); | |
ourSessionFactory = configuration.buildSessionFactory( serviceRegistry ); | |
} | |
catch ( Throwable ex ) { | |
throw new ExceptionInInitializerError( ex ); | |
} | |
} | |
public static Session getSession() throws HibernateException { | |
return ourSessionFactory.openSession(); | |
} | |
public static void main(final String[] args) throws Exception { | |
indexEntities(); | |
search(); | |
} | |
private static void indexEntities() throws Exception { | |
FullTextSession fullTextSession = Search.getFullTextSession( getSession() ); | |
try { | |
fullTextSession.createIndexer().startAndWait(); | |
System.out.println("Indexing complete"); | |
} | |
catch (InterruptedException e) { | |
Thread.currentThread().interrupt(); | |
System.out.println("thread interrupted"); | |
} | |
finally { | |
fullTextSession.close(); | |
} | |
} | |
private static void search() { | |
Session session = getSession(); | |
try { | |
FullTextSession fullTextSession = Search.getFullTextSession( session ); | |
QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( Film.class ).get(); | |
org.apache.lucene.search.Query query = builder.keyword() | |
.wildcard() | |
.onField( "actors.firstName" ) | |
.matching( "me*" ) | |
.createQuery(); | |
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Film.class ); | |
List<Film> films = fullTextQuery.list(); | |
System.out.println("Search results:"); | |
for ( Film film : films ) { | |
System.out.println( film ); | |
} | |
System.out.println("Done"); | |
} | |
finally { | |
session.close(); | |
} | |
} | |
private static void dumpEntities(Session session) { | |
System.out.println( "querying all the managed entities..." ); | |
final Map metadataMap = session.getSessionFactory().getAllClassMetadata(); | |
for ( Object key : metadataMap.keySet() ) { | |
final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get( key ); | |
final String entityName = classMetadata.getEntityName(); | |
final Query query = session.createQuery( "from " + entityName ); | |
System.out.println( "executing: " + query.getQueryString() ); | |
for ( Object o : query.list() ) { | |
System.out.println( " " + o ); | |
} | |
} | |
} | |
} |
Author
hferentschik
commented
May 13, 2015
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/sakila</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.username">hibernate</property>
<property name="connection.password">hibernate</property>
<mapping class="acme.Actor"/>
<mapping class="acme.Film"/>
<!-- DB schema will be updated if needed -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment