Commits

Micha Kops  committed 60a16f8

initial import

  • Participants

Comments (0)

Files changed (6)

+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" output="target/classes" path="src/main/java"/>
+	<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
+	<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="target/classes"/>
+</classpath>
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>hibernate-lucene-tutorial</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+	</natures>
+</projectDescription>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>hibernate-lucene-tutorial</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<name>hasCode.com Hibernate Lucene Tutorial</name>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-search</artifactId>
+			<version>4.0.0.Final</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-entitymanager</artifactId>
+			<version>4.0.1.Final</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hsqldb</groupId>
+			<artifactId>hsqldb</artifactId>
+			<version>2.2.8</version>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+
+	<repositories>
+		<repository>
+			<id>maven2-repository.dev.java.net</id>
+			<name>Java.net Repository for Maven</name>
+			<url>http://download.java.net/maven/2/</url>
+			<layout>default</layout>
+		</repository>
+	</repositories>
+</project>

File src/main/java/com/hascode/tutorial/Book.java

+package com.hascode.tutorial;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+
+import org.hibernate.search.annotations.Analyze;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Store;
+
+@Entity
+@Indexed(index = "indexes/books")
+public class Book {
+	private Long id;
+	private String title;
+	private String summary;
+	private String author;
+
+	@Id
+	@GeneratedValue
+	@DocumentId
+	public Long getId() {
+		return id;
+	}
+
+	@Field(name = "title", analyze = Analyze.YES, store = Store.YES)
+	public String getTitle() {
+		return title;
+	}
+
+	@Lob()
+	@Field(name = "summary", analyze = Analyze.YES, store = Store.YES)
+	public String getSummary() {
+		return summary;
+	}
+
+	public void setTitle(final String title) {
+		this.title = title;
+	}
+
+	public void setSummary(final String summary) {
+		this.summary = summary;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	public void setAuthor(final String author) {
+		this.author = author;
+	}
+
+	@Field(name = "author", analyze = Analyze.NO, store = Store.YES)
+	public String getAuthor() {
+		return author;
+	}
+
+}

File src/main/java/com/hascode/tutorial/Library.java

+package com.hascode.tutorial;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+import javax.persistence.Query;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.TermQuery;
+import org.hibernate.search.jpa.FullTextEntityManager;
+
+public class Library {
+	public static void main(final String... args) {
+		// creating persistence context
+		final EntityManagerFactory emf = Persistence
+				.createEntityManagerFactory("hascode-local");
+		final EntityManager em = emf.createEntityManager();
+		final EntityTransaction tx = em.getTransaction();
+
+		tx.begin();
+		// creating some books to be indexed and persisted
+		Book book1 = new Book();
+		book1.setTitle("The big book of nothing");
+		book1.setSummary("This is a book without any content");
+		book1.setAuthor("fred");
+
+		Book book2 = new Book();
+		book2.setTitle("Exciting stories I");
+		book2.setSummary("A compilation of exciting stories - part 1.");
+		book2.setAuthor("selma");
+
+		Book book3 = new Book();
+		book3.setTitle("My life");
+		book3.setSummary("A book about Fred's life.");
+		book3.setAuthor("fred");
+
+		em.persist(book1);
+		em.persist(book2);
+		em.persist(book3);
+		tx.commit();
+
+		// search using lucene
+		FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
+				.getFullTextEntityManager(em);
+
+		org.apache.lucene.search.Query titleQuery = new TermQuery(new Term(
+				"author", "fred"));
+		javax.persistence.Query fullTextQuery = fullTextEntityManager
+				.createFullTextQuery(titleQuery);
+
+		System.out.println("searching using lucene..");
+		List<Book> result = fullTextQuery.getResultList();
+		printResults(result);
+
+		// oldschool JPA search
+		Query query = em
+				.createQuery("SELECT b FROM Book b WHERE b.author=:author");
+		query.setParameter("author", "fred");
+		System.out.println("searching using JPA/JPQL..");
+		result = query.getResultList();
+		printResults(result);
+
+		em.close();
+		emf.close();
+	}
+
+	private static void printResults(final List<Book> result) {
+		System.out.println(String.format("%s items found for author:fred",
+				result.size()));
+		for (Book b : result) {
+			System.out.println("title: " + b.getTitle() + ", summary: "
+					+ b.getSummary() + "(id: " + b.getId() + ")");
+		}
+	}
+}

File src/main/resources/META-INF/persistence.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+	version="1.0">
+	<persistence-unit name="hascode-local"
+		transaction-type="RESOURCE_LOCAL">
+		<provider>org.hibernate.ejb.HibernatePersistence</provider>
+		<class>com.hascode.tutorial.Book</class>
+		<properties>
+			<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
+			<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb" />
+			<property name="javax.persistence.jdbc.user" value="sa" />
+			<property name="javax.persistence.jdbc.password" value="" />
+			<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
+			<property name="hibernate.hbm2ddl.auto" value="update" />
+		</properties>
+	</persistence-unit>
+</persistence>