Author: hardy.ferentschik
Date: 2007-09-20 10:54:45 -0400 (Thu, 20 Sep 2007)
New Revision: 14013
Added:
search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerSerializationTest.java
Modified:
search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java
search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerTest.java
Log:
HSEARCH-117 Made FullTextEntityManagerimpl serializable by making the
SearchFactoryImplementor transient in the FullTextSessionImpl.
Modified: search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java 2007-09-16
19:57:36 UTC (rev 14012)
+++ search/trunk/src/java/org/hibernate/search/impl/FullTextSessionImpl.java 2007-09-20
14:54:45 UTC (rev 14013)
@@ -22,9 +22,9 @@
import org.hibernate.SQLQuery;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
-import org.hibernate.classic.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
+import org.hibernate.classic.Session;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.engine.EntityKey;
import org.hibernate.engine.PersistenceContext;
@@ -40,11 +40,11 @@
import org.hibernate.jdbc.JDBCContext;
import org.hibernate.loader.custom.CustomQuery;
import org.hibernate.persister.entity.EntityPersister;
+import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
-import org.hibernate.search.FullTextQuery;
import org.hibernate.search.SearchFactory;
+import org.hibernate.search.backend.Work;
import org.hibernate.search.backend.WorkType;
-import org.hibernate.search.backend.Work;
import org.hibernate.search.engine.DocumentBuilder;
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.query.FullTextQueryImpl;
@@ -57,12 +57,13 @@
*
* @author Emmanuel Bernard
* @author John Griffin
+ * @author Hardy Ferentschik
*/
public class FullTextSessionImpl implements FullTextSession, SessionImplementor {
private final Session session;
private final EventSource eventSource;
private final SessionImplementor sessionImplementor;
- private SearchFactoryImplementor searchFactory;
+ private transient SearchFactoryImplementor searchFactory;
public FullTextSessionImpl(org.hibernate.Session session) {
this.session = (Session) session;
Modified:
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java
===================================================================
---
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java 2007-09-16
19:57:36 UTC (rev 14012)
+++
search/trunk/src/java/org/hibernate/search/jpa/impl/FullTextEntityManagerImpl.java 2007-09-20
14:54:45 UTC (rev 14013)
@@ -19,7 +19,7 @@
/**
* @author Emmanuel Bernard
*/
-public class FullTextEntityManagerImpl implements FullTextEntityManager {
+public class FullTextEntityManagerImpl implements FullTextEntityManager, Serializable {
private EntityManager em;
private FullTextSession ftSession;
Added:
search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerSerializationTest.java
===================================================================
---
search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerSerializationTest.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerSerializationTest.java 2007-09-20
14:54:45 UTC (rev 14013)
@@ -0,0 +1,117 @@
+// $Id$
+package org.hibernate.search.test.jpa;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.hibernate.search.jpa.FullTextEntityManager;
+import org.hibernate.search.jpa.Search;
+
+/**
+ * Serialization test for entity manager. HSEARCH-117.
+ *
+ * @author Hardy Ferentschik
+ */
+public class EntityManagerSerializationTest extends JPATestCase {
+
+ /**
+ * Test that a entity manager can successfully be serialized and
+ * deserialized.
+ *
+ * @throws Exception
+ * in case the test fails.
+ */
+ public void testSerialization() throws Exception {
+ FullTextEntityManager em = Search.createFullTextEntityManager(factory
+ .createEntityManager());
+
+ indexSearchAssert(em);
+
+ File tmpFile = File.createTempFile("entityManager", "ser", null);
+ serializeEM(em, tmpFile);
+ em = deserializeEM(em, tmpFile);
+
+ indexSearchAssert(em);
+
+ em.close();
+
+ // cleanup
+ tmpFile.delete();
+ }
+
+ private FullTextEntityManager deserializeEM(FullTextEntityManager em,
+ File tmpFile) throws ClassNotFoundException {
+ FileInputStream fis = null;
+ ObjectInputStream in = null;
+ try {
+ fis = new FileInputStream(tmpFile);
+ in = new ObjectInputStream(fis);
+ em = (FullTextEntityManager) in.readObject();
+ in.close();
+ }
+ catch (IOException ex) {
+ ex.printStackTrace();
+ }
+ return em;
+ }
+
+ private File serializeEM(FullTextEntityManager em, File tmpFile) {
+ FileOutputStream fos = null;
+ ObjectOutputStream out = null;
+ try {
+ fos = new FileOutputStream(tmpFile);
+ out = new ObjectOutputStream(fos);
+ out.writeObject(em);
+ out.close();
+ } catch (IOException ex) {
+ ex.printStackTrace();
+ fail();
+ }
+ return tmpFile;
+ }
+
+ public Class[] getAnnotatedClasses() {
+ return new Class[] { Bretzel.class };
+ };
+
+ /**
+ * Helper method for testing the entity manager before and after
+ * serialization.
+ *
+ * @param em
+ * @throws Exception
+ */
+ private void indexSearchAssert(FullTextEntityManager em) throws Exception {
+ em.getTransaction().begin();
+ Bretzel bretzel = new Bretzel(23, 34);
+ em.persist(bretzel);
+ em.getTransaction().commit();
+ em.clear();
+ em.getTransaction().begin();
+ QueryParser parser = new QueryParser("title", new StopAnalyzer());
+ Query query = parser.parse("saltQty:noword");
+ assertEquals(0, em.createFullTextQuery(query).getResultList().size());
+ query = new TermQuery(new Term("saltQty", "23.0"));
+ assertEquals("getResultList", 1, em.createFullTextQuery(query)
+ .getResultList().size());
+ assertEquals("getSingleResult and object retrieval", 23f, ((Bretzel) em
+ .createFullTextQuery(query).getSingleResult()).getSaltQty());
+ assertEquals(1, em.createFullTextQuery(query).getResultSize());
+ em.getTransaction().commit();
+
+ em.clear();
+
+ em.getTransaction().begin();
+ em.remove(em.find(Bretzel.class, bretzel.getId()));
+ em.getTransaction().commit();
+ }
+}
Property changes on:
search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerSerializationTest.java
___________________________________________________________________
Name: svn:executable
+ *
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerTest.java 2007-09-16
19:57:36 UTC (rev 14012)
+++ search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerTest.java 2007-09-20
14:54:45 UTC (rev 14013)
@@ -50,7 +50,7 @@
em.getTransaction().commit();
em.clear();
- //Not really a unit test but a test that shows the method call wiouth failing
+ //Not really a unit test but a test that shows the method call without failing
//FIXME port the index test
em.getTransaction().begin();
em.index( em.find( Bretzel.class, bretzel.getId() ) );
Property changes on:
search/trunk/src/test/org/hibernate/search/test/jpa/EntityManagerTest.java
___________________________________________________________________
Name: svn:keywords
+ Id