Author: epbernard
Date: 2008-03-07 12:17:13 -0500 (Fri, 07 Mar 2008)
New Revision: 14408
Added:
search/trunk/src/test/org/hibernate/search/test/id/
search/trunk/src/test/org/hibernate/search/test/id/EmbeddedIdTest.java
search/trunk/src/test/org/hibernate/search/test/id/Person.java
search/trunk/src/test/org/hibernate/search/test/id/PersonPK.java
search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java
Modified:
search/trunk/src/java/org/hibernate/search/annotations/FieldBridge.java
search/trunk/src/java/org/hibernate/search/bridge/BridgeFactory.java
Log:
HSEARCH-163 better BridgeFactory error messages
Modified: search/trunk/src/java/org/hibernate/search/annotations/FieldBridge.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/annotations/FieldBridge.java 2008-03-07
16:47:02 UTC (rev 14407)
+++ search/trunk/src/java/org/hibernate/search/annotations/FieldBridge.java 2008-03-07
17:17:13 UTC (rev 14408)
@@ -16,6 +16,7 @@
@Target( {ElementType.FIELD, ElementType.METHOD} )
@Documented
public @interface FieldBridge {
+ //defaault to embed @FieldBridge in @Field
public Class impl() default void.class;
public Parameter[] params() default {};
Modified: search/trunk/src/java/org/hibernate/search/bridge/BridgeFactory.java
===================================================================
--- search/trunk/src/java/org/hibernate/search/bridge/BridgeFactory.java 2008-03-07
16:47:02 UTC (rev 14407)
+++ search/trunk/src/java/org/hibernate/search/bridge/BridgeFactory.java 2008-03-07
17:17:13 UTC (rev 14408)
@@ -137,6 +137,8 @@
}
if ( bridgeAnn != null ) {
Class impl = bridgeAnn.impl();
+ if (impl == void.class)
+ throw new SearchException("@FieldBridge with no implementation class defined in:
" + member.getName() );
try {
Object instance = impl.newInstance();
if ( FieldBridge.class.isAssignableFrom( impl ) ) {
@@ -149,6 +151,10 @@
else if ( org.hibernate.search.bridge.StringBridge.class.isAssignableFrom( impl ) )
{
bridge = new String2FieldBridgeAdaptor( (org.hibernate.search.bridge.StringBridge)
instance );
}
+ else {
+ throw new SearchException("@FieldBridge implementation implements none of the
field bridge interfaces: "
+ + impl + " in " + member.getName() );
+ }
if ( bridgeAnn.params().length > 0 &&
ParameterizedBridge.class.isAssignableFrom( impl ) ) {
Map params = new HashMap( bridgeAnn.params().length );
for ( Parameter param : bridgeAnn.params() ) {
Added: search/trunk/src/test/org/hibernate/search/test/id/EmbeddedIdTest.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/id/EmbeddedIdTest.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/id/EmbeddedIdTest.java 2008-03-07
17:17:13 UTC (rev 14408)
@@ -0,0 +1,43 @@
+//$
+package org.hibernate.search.test.id;
+
+import java.util.List;
+
+import org.hibernate.search.test.SearchTestCase;
+import org.hibernate.search.Search;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.apache.lucene.search.TermQuery;
+import org.apache.lucene.index.Term;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class EmbeddedIdTest extends SearchTestCase {
+ public void testFieldBridge() throws Exception {
+ PersonPK emmPk = new PersonPK();
+ emmPk.setFirstName( "Emmanuel" );
+ emmPk.setLastName( "Bernard" );
+ Person emm = new Person();
+ emm.setFavoriteColor( "Blue" );
+ emm.setId( emmPk );
+ Session s = openSession( );
+ Transaction tx = s.beginTransaction();
+ s.save(emm);
+ tx.commit();
+ s.clear();
+ tx = s.beginTransaction();
+ List results = Search.createFullTextSession( s ).createFullTextQuery(
+ new TermQuery( new Term("id.lastName", "Bernard" ) ) ).list();
+ assertEquals( 1, results.size() );
+ s.delete( results.get( 0 ) );
+ tx.commit();
+ s.close();
+
+ }
+ protected Class[] getMappings() {
+ return new Class[] {
+ Person.class
+ };
+ }
+}
Added: search/trunk/src/test/org/hibernate/search/test/id/Person.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/id/Person.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/id/Person.java 2008-03-07 17:17:13 UTC
(rev 14408)
@@ -0,0 +1,40 @@
+//$
+package org.hibernate.search.test.id;
+
+import javax.persistence.Entity;
+import javax.persistence.EmbeddedId;
+
+import org.hibernate.search.annotations.FieldBridge;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Indexed
+public class Person {
+ @EmbeddedId
+ @FieldBridge(impl = PersonPKBridge.class)
+ @DocumentId
+ private PersonPK id;
+ private String favoriteColor;
+
+ public PersonPK getId() {
+ return id;
+ }
+
+ public void setId(PersonPK id) {
+ this.id = id;
+ }
+
+ @Field
+ public String getFavoriteColor() {
+ return favoriteColor;
+ }
+
+ public void setFavoriteColor(String favoriteColor) {
+ this.favoriteColor = favoriteColor;
+ }
+}
Added: search/trunk/src/test/org/hibernate/search/test/id/PersonPK.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/id/PersonPK.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/id/PersonPK.java 2008-03-07 17:17:13
UTC (rev 14408)
@@ -0,0 +1,49 @@
+//$
+package org.hibernate.search.test.id;
+
+import java.io.Serializable;
+import javax.persistence.Embeddable;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Embeddable
+public class PersonPK implements Serializable {
+ private String firstName;
+ private String lastName;
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public boolean equals(Object o) {
+ if ( this == o ) return true;
+ if ( !( o instanceof PersonPK ) ) return false;
+
+ PersonPK personPK = (PersonPK) o;
+
+ if ( firstName != null ? !firstName.equals( personPK.firstName ) : personPK.firstName
!= null ) return false;
+ if ( lastName != null ? !lastName.equals( personPK.lastName ) : personPK.lastName !=
null ) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ int result;
+ result = ( firstName != null ? firstName.hashCode() : 0 );
+ result = 31 * result + ( lastName != null ? lastName.hashCode() : 0 );
+ return result;
+ }
+}
Added: search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java
===================================================================
--- search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java
(rev 0)
+++ search/trunk/src/test/org/hibernate/search/test/id/PersonPKBridge.java 2008-03-07
17:17:13 UTC (rev 14408)
@@ -0,0 +1,38 @@
+//$
+package org.hibernate.search.test.id;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.hibernate.search.bridge.TwoWayFieldBridge;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class PersonPKBridge implements TwoWayFieldBridge {
+
+ public Object get(String name, Document document) {
+ PersonPK id = new PersonPK();
+ Field field = document.getField( name + ".firstName" );
+ id.setFirstName( field.stringValue() );
+ field = document.getField( name + ".lastName" );
+ id.setLastName( field.stringValue() );
+ return id;
+ }
+
+ public String objectToString(Object object) {
+ PersonPK id = (PersonPK) object;
+ StringBuilder sb = new StringBuilder();
+ sb.append( id.getFirstName() ).append( " " ).append( id.getLastName() );
+ return sb.toString();
+ }
+
+ public void set(String name, Object value, Document document, Field.Store store,
Field.Index index, Float boost) {
+ PersonPK id = (PersonPK) value;
+ Field field = new Field( name + ".firstName", id.getFirstName(), store, index
);
+ if ( boost != null ) field.setBoost( boost );
+ document.add( field );
+ field = new Field( name + ".lastName", id.getLastName(), store, index );
+ if ( boost != null ) field.setBoost( boost );
+ document.add( field );
+ }
+}
Show replies by date