-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: indexing enum associations
PostPosted: Thu Jul 22, 2010 3:41 pm 
Newbie

Joined: Wed Jul 21, 2010 11:30 am
Posts: 3
I'm new to Hibernate Search and I'm trying to convert our domain model from our current search, technology Compass GPS. I'm having trouble indexing an enum association in an entity. The current implementation is:

@Entity
@Indexed("A")
public class A {
...
@SearchableProperty
@CollectionOfElements(fetch=FetchType.EAGER)
@Enumerated(EnumType.STRING)
private Set<B> b = new HashSet<B>();
...
}

My first attempt using Hibernate 3.5.1, and Hibernate Search 3.2.0 is:

@Field( index = Index.UN_TOKENIZED, store = Store.YES )
@FieldBridge( impl = EnumBridge.class )
@ElementCollection( fetch = FetchType.EAGER )
@Enumerated(EnumType.STRING)
private Set<B> b = new HashSet<B>();

where B looks something like this:

public enum B {
EntryA("Entry-A"),
EntryB("Entry-B");
...
}

But I get the exception:

org.hibernate.search.SearchException: Unable to instanciate FieldBridge for b

If I leave out the FieldBridge, then I get this exception:

org.hibernate.search.SearchException: Unable to guess FieldBridge for b


Top
 Profile  
 
 Post subject: Re: indexing enum associations
PostPosted: Fri Jul 23, 2010 9:04 am 
Beginner
Beginner

Joined: Mon Mar 01, 2010 12:24 pm
Posts: 22
Hi,

What does your EnumBridge look like? Have you already tried to use @IndexedEmbedded on your set of B?


Top
 Profile  
 
 Post subject: Re: indexing enum associations
PostPosted: Fri Jul 23, 2010 10:10 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
if it's org.hibernate.search.bridge.builtin.EnumBridge you're referring to, it doesn't support collections of enums.
Please write a new implementation of org.hibernate.search.bridge.TwoWayStringBridge, then use that. If you want to contribute the code we could include it in the project, this seems a reasonable feature to provide out-of-the box, apparently noone noticed before that it's missing.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: indexing enum associations
PostPosted: Fri Jul 23, 2010 1:38 pm 
Newbie

Joined: Wed Jul 21, 2010 11:30 am
Posts: 3
I appreciate the offer of adding an Enum/String Bridge. That would be terrific. I'm so new that I'm not confident enough to contribute at this time.

I tried to implement a TwoWayStringBridge for my enum, and now I'm getting a different exception:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

I am running this within a Spring 2.5.4 SpringJunit4ClassRunner test, but I am not using TransactionalConfiguration. I'm getting the session and starting the transaction manually inside my test:

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
A a = new A();
a.addB(B.EntryA);
session.save(a);
tx.commit();
session.close();

If the B collection of enums field is not indexed (by commenting out the @Field and @FieldBridge), then the object is persisted. If the collection is indexed then I get the "two open sessions" exception.


Top
 Profile  
 
 Post subject: Re: indexing enum associations
PostPosted: Fri Jul 23, 2010 7:27 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I appreciate the offer of adding an Enum/String Bridge. That would be terrific. I'm so new that I'm not confident enough to contribute at this time.

you don't need to provide something perfect, but anything working is a good starting point for then discuss the details later.

Quote:
I tried to implement a TwoWayStringBridge for my enum, and now I'm getting a different exception:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions

maybe something is wrong with your model; could you show the code needed to reproduce?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: indexing enum associations
PostPosted: Tue Jul 27, 2010 11:45 am 
Newbie

Joined: Wed Jul 21, 2010 11:30 am
Posts: 3
Quote:
you don't need to provide something perfect, but anything working is a good starting point for then discuss the details later.



Quote:
maybe something is wrong with your model; could you show the code needed to reproduce?


Based on company policy, I'm not able to contribute back to open source projects or post code.

I did get a little further with this issue. I enabled the Spring transaction management and injected a basic service layer DAO, wihch got me past the transaction problems. After more research and testing, I started working on a TwoWayString2FieldBridgeAdaptor to ensure that the set of enums is handled properly in and out of the index and all the terms are searchable.

If there is another method for abstracting the enum, maybe using a OneToMany association, I'd be open to suggestions. Thanks for your help.


Top
 Profile  
 
 Post subject: Re: indexing enum associations
PostPosted: Tue Jul 27, 2010 4:33 pm 
Beginner
Beginner

Joined: Wed Nov 10, 2004 5:48 pm
Posts: 32
Location: Portland
Quote:
Based on company policy, I'm not able to contribute back to open source projects or post code.


Perhaps your company should not use any open source projects then. Having an improvement and refusing to contribute it back is kinda counter to the spirit of the thing...


Top
 Profile  
 
 Post subject: Re: indexing enum associations
PostPosted: Wed Nov 27, 2013 5:10 am 
Newbie

Joined: Sat Mar 26, 2011 5:03 am
Posts: 2
I've written the following bridge to solve the problem. 

Unsderscores are removed to avoid search problem with enum values having the same prefix / suffix (e.g. : DARK_BLUE, LIGHT_BLUE)

Code:
package org.cyclick.commons.hibernate.search.bridge;

import java.util.Collection;

import org.hibernate.search.bridge.StringBridge;

/**
 * Transform a collection of enum constants into a string representation.
 * 
 * <p>The result if a string containing the names of every enum constants separated by a white space.</p> 
 * 
 * <p>"_" are removed from the enum constants name.</p>
 * 
 * @author Thomas Vanstals
 * @since 2.2.0 
 */
public class CollectionOfEnumsBridge implements StringBridge {

   private static final String UNDERSCORE = "_";

   private static final String WHITE_SPACE = " ";
   
   private static final String EMPTY_STRING = "";

   @Override
   public String objectToString(Object object) {
      String result = null;
      if (object != null && object instanceof Collection<?>) {
         StringBuilder sb = new StringBuilder();
         Collection<?> items = (Collection<?>) object;
         for (Object item : items) {
            if (item != null && item.getClass().isEnum()) {
               if (sb.length() > 0) {
                  sb.append(WHITE_SPACE);
               }
               Enum<?> e = (Enum<?>) item;
               sb.append(e.name().replace(UNDERSCORE, EMPTY_STRING));
            }
         }
         result = sb.toString();
      }
      return result;
   }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.