Author: hardy.ferentschik
Date: 2008-08-11 12:17:19 -0400 (Mon, 11 Aug 2008)
New Revision: 15033
Added:
search/trunk/src/test/org/hibernate/search/test/embedded/StateCandidate.java
Modified:
search/trunk/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java
search/trunk/src/test/org/hibernate/search/test/embedded/State.java
Log:
Testcase for HSEARCH-142
Modified: search/trunk/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java 2008-08-11
14:53:01 UTC (rev 15032)
+++ search/trunk/src/test/org/hibernate/search/test/embedded/EmbeddedTest.java 2008-08-11
16:17:19 UTC (rev 15033)
@@ -18,8 +18,8 @@
/**
* @author Emmanuel Bernard
+ * @author Hardy Ferentschik
*/
-@SuppressWarnings("unchecked")
public class EmbeddedTest extends SearchTestCase {
public void testEmbeddedIndexing() throws Exception {
@@ -42,11 +42,10 @@
s.persist( tower );
tx.commit();
-
FullTextSession session = Search.getFullTextSession( s );
QueryParser parser = new QueryParser( "id", new StandardAnalyzer() );
Query query;
- List result;
+ List<?> result;
query = parser.parse( "address.street:place" );
result = session.createFullTextQuery( query ).list();
@@ -71,7 +70,6 @@
address.getOwnedBy().setName( "Buckhead community" );
tx.commit();
-
s.clear();
session = Search.getFullTextSession( s );
@@ -89,38 +87,37 @@
s.close();
}
-
+
public void testEmbeddedIndexingOneToMany() throws Exception {
Country country = new Country();
- country.setName("Germany");
- List states = new ArrayList<State>();
+ country.setName( "Germany" );
+ List<State> states = new ArrayList<State>();
State bayern = new State();
- bayern.setName("Bayern");
+ bayern.setName( "Bayern" );
State hessen = new State();
- hessen.setName("Hessen");
+ hessen.setName( "Hessen" );
State sachsen = new State();
- sachsen.setName("Sachsen");
- states.add(bayern);
- states.add(hessen);
- states.add(sachsen);
- country.setStates(states);
+ sachsen.setName( "Sachsen" );
+ states.add( bayern );
+ states.add( hessen );
+ states.add( sachsen );
+ country.setStates( states );
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( country );
tx.commit();
-
FullTextSession session = Search.getFullTextSession( s );
QueryParser parser = new QueryParser( "id", new StandardAnalyzer() );
Query query;
- List result;
+ List<?> result;
query = parser.parse( "states.name:Hessen" );
result = session.createFullTextQuery( query ).list();
assertEquals( "unable to find property in embedded", 1, result.size() );
s.close();
- }
+ }
public void testContainedIn() throws Exception {
Tower tower = new Tower();
@@ -151,7 +148,7 @@
FullTextSession session = Search.getFullTextSession( s );
QueryParser parser = new QueryParser( "id", new StandardAnalyzer() );
Query query;
- List result;
+ List<?> result;
query = parser.parse( "address.street:peachtree" );
result = session.createFullTextQuery( query, Tower.class ).list();
@@ -201,7 +198,7 @@
Product p1 = new Product();
p1.setName( "Candide" );
p1.getAuthors().add( a );
- p1.getAuthors().add( a2 ); //be creative
+ p1.getAuthors().add( a2 ); // be creative
Product p2 = new Product();
p2.setName( "Le malade imaginaire" );
@@ -209,7 +206,6 @@
p2.getOrders().put( "Emmanuel", o );
p2.getOrders().put( "Gavin", o2 );
-
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist( a );
@@ -229,17 +225,17 @@
QueryParser parser = new MultiFieldQueryParser( new String[] { "name",
"authors.name" }, new StandardAnalyzer() );
Query query;
- List result;
+ List<?> result;
query = parser.parse( "Hugo" );
result = session.createFullTextQuery( query, Product.class ).list();
assertEquals( "collection of embedded ignored", 1, result.size() );
- //update the collection
+ // update the collection
Product p = (Product) result.get( 0 );
p.getAuthors().add( a4 );
- //PhraseQuery
+ // PhraseQuery
query = new TermQuery( new Term( "orders.orderNumber", "ZERTYD" )
);
result = session.createFullTextQuery( query, Product.class ).list();
assertEquals( "collection of untokenized ignored", 1, result.size() );
@@ -255,29 +251,74 @@
session = Search.getFullTextSession( s );
query = parser.parse( "Proust" );
result = session.createFullTextQuery( query, Product.class ).list();
- //HSEARCH-56
+ // HSEARCH-56
assertEquals( "update of collection of embedded ignored", 1, result.size()
);
s.delete( s.get( Product.class, p1.getId() ) );
s.delete( s.get( Product.class, p2.getId() ) );
tx.commit();
s.close();
+ }
+ /**
+ * Tests that updating an indexed embedded object updates the Lucene index as well.
+ *
+ * @throws Exception in case the test fails
+ * @see HSEARCH-142
+ */
+ public void testEmbeddedObjectUpdate() throws Exception {
+
+ State state = new State();
+ state.setName( "Bavaria" );
+ StateCandidate candiate = new StateCandidate();
+ candiate.setName( "Mueller" );
+ candiate.setState( state );
+
+ Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ s.persist( candiate );
+ tx.commit();
+ s.clear();
+
+ FullTextSession session = Search.getFullTextSession( s );
+ tx = session.beginTransaction();
+
+ QueryParser parser = new MultiFieldQueryParser( new String[] { "name",
"state.name" }, new StandardAnalyzer() );
+ Query query;
+ List<?> result;
+
+ query = parser.parse( "Bavaria" );
+ result = session.createFullTextQuery( query, StateCandidate.class ).list();
+ assertEquals( "IndexEmbedded ignored.", 1, result.size() );
+ tx.commit();
+ s.clear();
+
+ tx = s.beginTransaction();
+ // remove the following line to see the test fails. It should not be necessary to
+ // to also update the name of the candidate. Only updating the state should work
+ // as well.
+ candiate.setName( "Beckstein" );
+ state.setName( "Hessen" );
+ candiate = (StateCandidate) s.merge( candiate );
+ tx.commit();
+ s.clear();
+
+ tx = s.beginTransaction();
+ session = Search.getFullTextSession( s );
+ query = parser.parse( "Hessen" );
+ result = session.createFullTextQuery( query, StateCandidate.class ).list();
+ assertEquals( "IndexEmbedded ignored.", 1, result.size() );
+ tx.commit();
+ s.clear();
+ s.close();
}
- protected void configure(org.hibernate.cfg.Configuration cfg) {
+ protected void configure( org.hibernate.cfg.Configuration cfg ) {
super.configure( cfg );
}
- protected Class[] getMappings() {
- return new Class[] {
- Tower.class,
- Address.class,
- Product.class,
- Order.class,
- Author.class,
- Country.class,
- State.class
- };
+ protected Class<?>[] getMappings() {
+ return new Class[] { Tower.class, Address.class, Product.class, Order.class,
Author.class, Country.class,
+ State.class, StateCandidate.class };
}
}
Modified: search/trunk/src/test/org/hibernate/search/test/embedded/State.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/embedded/State.java 2008-08-11
14:53:01 UTC (rev 15032)
+++ search/trunk/src/test/org/hibernate/search/test/embedded/State.java 2008-08-11
16:17:19 UTC (rev 15033)
@@ -1,10 +1,12 @@
-// $Id:$
+// $Id$
package org.hibernate.search.test.embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
+import javax.persistence.OneToOne;
+import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.Field;
/**
@@ -18,6 +20,10 @@
@Field
private String name;
+
+ @ContainedIn
+ @OneToOne(mappedBy = "state")
+ private StateCandidate candidate;
public Integer getId() {
return id;
@@ -34,4 +40,12 @@
public void setName(String name) {
this.name = name;
}
+
+ public StateCandidate getCandidate() {
+ return candidate;
+ }
+
+ public void setCandidate( StateCandidate candidate ) {
+ this.candidate = candidate;
+ }
}
Added: search/trunk/src/test/org/hibernate/search/test/embedded/StateCandidate.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/embedded/StateCandidate.java
(rev 0)
+++
search/trunk/src/test/org/hibernate/search/test/embedded/StateCandidate.java 2008-08-11
16:17:19 UTC (rev 15033)
@@ -0,0 +1,69 @@
+package org.hibernate.search.test.embedded;
+
+// $Id:$
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToOne;
+
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
+/**
+ *
+ * @author Hardy Ferentschik
+ */
+@Entity
+@Indexed
+public class StateCandidate implements Person {
+
+ @Id @GeneratedValue
+ @DocumentId
+ private int id;
+
+ @Field
+ private String name;
+
+ @OneToOne(cascade = CascadeType.ALL)
+ private Address address;
+
+ @IndexedEmbedded
+ @OneToOne(cascade = CascadeType.ALL)
+ private State state;
+
+ public State getState() {
+ return state;
+ }
+
+ public void setState( State state ) {
+ this.state = state;
+ }
+
+ public Address getAddress() {
+ return address;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setAddress( Address address ) {
+ this.address = address;
+
+ }
+
+ public void setName( String name ) {
+ this.name = name;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId( int id ) {
+ this.id = id;
+ }
+}
Property changes on:
search/trunk/src/test/org/hibernate/search/test/embedded/StateCandidate.java
___________________________________________________________________
Name: svn:eol-style
+ native