-->
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.  [ 14 posts ] 
Author Message
 Post subject: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Mon Sep 28, 2009 7:17 pm 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
Hi guys,

I've been working on trying to get hibernate-search 3.1.1.GA working for me. So far, mostly working! ie. I can search for most terms. What I'm having a problem with at the moment is searching a term that is embedded using a @ManyToOne association and @IndexEmbedded. Here are my two objects:

Code:
@Entity
@Indexed
@Table(name = "users")
public class User extends DataObject implements Serializable
{
   private static final long serialVersionUID = 1L;
   
   @Field
   @Column(name = "first_name", length = 64)
   private String firstName;
   
   @Field
   @Column(name = "last_name", length = 64)
   private String lastName;
   
   @Field(index = Index.TOKENIZED)
   @Column(name = "email_address", length = 128)
   private String emailAddress;
   
   @Column(name = "password", length = 512)
    private String password = "";
   
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER )
    @JoinColumn(name="user_type")
   private UserType userType;
   
    @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER )
    @JoinColumn(name="organization")
    @IndexedEmbedded
   private Organization organization;
}


Here is Organization:

Code:
@Entity
@Indexed
@Table(name = "organization")
public class Organization extends DataObject implements Serializable
{
   private static final long serialVersionUID = 1L;
   
   @Field(index = Index.TOKENIZED, store = Store.YES)
   @Column(name = "name", length = 64)
   private String name = "";
   
   @Field(index = Index.TOKENIZED, store = Store.YES)
   @Column(name = "description", length = 256)
   private String description = "";
   
   @Field(index = Index.TOKENIZED, store = Store.YES)
   @Column(name = "access_code", length = 32)
   private String accessCode;
}


DataObject looks like this:

Code:
@MappedSuperclass
public class DataObject implements Serializable
{
   private static final long serialVersionUID = 1L;
   
   @Id @GeneratedValue @Column(name = "id")
   private Long id;
}


Which is just a parent object for all of my objects. I can search Organizations and Users correctly.. however, if I try to search for organization.accessCode on a User, it fails. Here is my search code:

Code:
MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] { "id", "firstName", "lastName", "emailAddress", "organization.accessCode"}, new StandardAnalyzer());

org.apache.lucene.search.Query query = parser.parse( terms );
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, User.class);


What am I doing wrong here? I've used Luke to check out the indexes. What is weird is that organization.id IS indexed on User, but nothing else! I'm at a loss here.

Thanks again for any help!

[EDIT] I've also tried to add this to Organization just as a test:

Code:
   @OneToMany(mappedBy="organization")
   @ContainedIn
   private List<User> users;


Also, as a test, I set both as using the same index, which also didn't have any effect using @Indexed(index = "shared").


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Tue Sep 29, 2009 3:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I cannot see anything obvious wrong. Here are some ideas to help you to get closer to solving the problem. First of all, how does your indexing code look like? Is there anything in your log file? Maybe set the log4j category for org.hibernate.search to debug. Are you sure that accessCode is not null in the organization you are indexing? Null values are getting ignored.

If you never want to search Organization entities directly you don't have to mark Organization with @Indexed. Also be aware of the fact that since your association is only unidirectional (you don't have a link from Organization back to user and no @ContainedIn) indexed User documents won't get updated when the associated organization gets modified.


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Tue Sep 29, 2009 12:40 pm 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
Hi,

Thank you for the reply. Here is the log that I am receiving when I save out an Organization (which I do first):

Code:
28 Sep 2009 20:23:36,908 DEBUG http-8080-3  org.hibernate.pretty.Printer - listing entities:
28 Sep 2009 20:23:36,908 DEBUG http-8080-3  org.hibernate.pretty.Printer - com.jcnet.vo.Organization{titleFontSize=24, id=1, users=null, title=null, subtitleFontSize=16, description=Test Organizations, name=Test, subtitle=null, percentageAdded=10.0, dollarsAdded=0.0, accessCode=TEST}
28 Sep 2009 20:23:36,914 DEBUG http-8080-3  org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
28 Sep 2009 20:23:36,914 DEBUG http-8080-3  org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
28 Sep 2009 20:23:36,914 DEBUG http-8080-3  org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
28 Sep 2009 20:23:36,914 DEBUG http-8080-3  com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@4e39f16f [managed: 10, unused: 9, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@180fb0b0)
28 Sep 2009 20:23:36,923 DEBUG pool-2-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Skipping usage of an IndexReader for updates
28 Sep 2009 20:23:36,924 DEBUG pool-2-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Opening an IndexWriter for update


So first, you can see accessCode is definitely being set to the organization. Then when I create the user, I get the following bit of log:

Code:
28 Sep 2009 20:24:00,317 DEBUG http-8080-3  org.hibernate.pretty.Printer - listing entities:
28 Sep 2009 20:24:00,317 DEBUG http-8080-3  org.hibernate.pretty.Printer - com.jcnet.vo.User{id=1, lastName=Jones, organization=com.jcnet.vo.Organization#1, emailAddress=test@test.com, firstName=Bob, password=2f855926454df7a805a020d569b98281, userType=com.jcnet.vo.UserType#1}
28 Sep 2009 20:24:00,318 DEBUG http-8080-3  org.hibernate.transaction.JDBCTransaction - committed JDBC Connection
28 Sep 2009 20:24:00,318 DEBUG http-8080-3  org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection
28 Sep 2009 20:24:00,319 DEBUG http-8080-3  org.hibernate.jdbc.ConnectionManager - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
28 Sep 2009 20:24:00,319 DEBUG http-8080-3  com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@4e39f16f [managed: 10, unused: 9, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@180fb0b0)
28 Sep 2009 20:24:00,319 DEBUG pool-1-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Skipping usage of an IndexReader for updates
28 Sep 2009 20:24:00,321 DEBUG pool-1-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Opening an IndexWriter for update


Again, you can see the organization is being set on the User object here. I never get anything more than those last two lines for the index there.

As far as indexing code goes, I am "automatically" indexing on the save. Here is my save code though:

Code:
   public static void saveDataObject(DataObject object)
   {
      Session session = HibernateUtils.getSession();
      Transaction tx = null;
      
      try
      {
         tx = session.beginTransaction();
         tx.setTimeout(3);
         session.saveOrUpdate(object);
         tx.commit();
      }
      catch(final Exception exception)
      {
         if(tx != null)
         {
            tx.rollback();
         }

         session = HibernateUtils.forceSession(object);
         
         try
         {
            tx = session.beginTransaction();
            tx.setTimeout(3);
            session.saveOrUpdate(object);
            tx.commit();
         }
         catch(final RuntimeException e)
         {
            if(tx != null)
            {
               tx.rollback();
            }
            
            throw e;
         }
      }
      finally
      {
         session.close();
      }
   }


Is there anything else I should do to write the index out further?

Yes, I actually tried @ContainedIn late last night and edited my forum post. Didn't effect this problem though:

Code:
   @OneToMany(mappedBy="organization")
   @ContainedIn
   private List<User> users;


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Tue Sep 29, 2009 2:33 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
If you set the log level to trace for AddWorkDelegate you the whole Lucene document which gets indexed gets dumped.

Code:
log4j.logger.org.hibernate.search.backend.impl.lucene.works.AddWorkDelegate=trace


--hardy


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Tue Sep 29, 2009 2:58 pm 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
Hi,

Here is the log output with the added logging:

Organization Save:

Code:
29 Sep 2009 11:50:18,049 DEBUG pool-2-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Skipping usage of an IndexReader for updates
29 Sep 2009 11:50:18,050 DEBUG pool-2-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Opening an IndexWriter for update
29 Sep 2009 11:50:18,051 TRACE pool-2-thread-1  org.hibernate.search.backend.impl.lucene.works.AddWorkDelegate - add to Lucene index: class com.jcnet.vo.Organization#1:Document<stored/uncompressed,indexed<_hibernate_class:com.jcnet.vo.Organization> stored/uncompressed,indexed<id:1> stored/uncompressed,indexed,tokenized<name:Test> stored/uncompressed,indexed,tokenized<description:Test Organization> stored/uncompressed,indexed,tokenized<accessCode:TEST>>


User Save:

Code:
29 Sep 2009 11:50:32,034 DEBUG pool-1-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Skipping usage of an IndexReader for updates
29 Sep 2009 11:50:32,034 DEBUG pool-1-thread-1  org.hibernate.search.backend.impl.lucene.PerDPQueueProcessor - Opening an IndexWriter for update
29 Sep 2009 11:50:32,035 TRACE pool-1-thread-1  org.hibernate.search.backend.impl.lucene.works.AddWorkDelegate - add to Lucene index: class com.jcnet.vo.User#1:Document<stored/uncompressed,indexed<_hibernate_class:com.jcnet.vo.User> stored/uncompressed,indexed<id:1> indexed,tokenized<firstName:Joe> indexed,tokenized<lastName:Cool> indexed,tokenized<emailAddress:test@test.com> indexed,tokenized<emailSplit:test test.com> stored/uncompressed,indexed<organization.id:1>>


Again, this pretty much looks like what I see in Luke. I only see the organization id going in and nothing else.

Every time I do the test, I blow away my database and indexes also so there is a clean result. Does this help? If the rest of the log will help, I can put it somewhere accessible. Just let me know.

-- graum


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Tue Sep 29, 2009 7:14 pm 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
I have more information. Been playing with this again today and I tried a different angle. If you refer to my first post, you'll notice that I have a @MappedSuperclass called DataObject which both User and Organization extend. I thought it was weird that organization.id was indexed with the user but nothing else. So I placed another field in DataObject. I changed it from this:

Code:
@MappedSuperclass
public class DataObject implements Serializable
{
   private static final long serialVersionUID = 1L;
   
   @Id @GeneratedValue @Column(name = "id")
   private Long id;
}


to this:

Code:
@MappedSuperclass
public class DataObject implements Serializable
{
   private static final long serialVersionUID = 1L;
   
   @Id @GeneratedValue @Column(name = "id")
   private Long id;

   @Field(index = Index.TOKENIZED, store = Store.YES)
   private String test = "TESTFIELD";
}


I re-ran my test and low and behold, organization.test was being indexed! Here is the log:

Code:
29 Sep 2009 16:06:28,067 TRACE pool-1-thread-1  org.hibernate.search.backend.impl.lucene.works.AddWorkDelegate - add to Lucene index: class com.jcnet.vo.User#1:Document<stored/uncompressed,indexed<_hibernate_class:com.jcnet.vo.User> stored/uncompressed,indexed<id:1> stored/uncompressed,indexed,tokenized<test:TESTFIELD> indexed,tokenized<firstName:Joe> indexed,tokenized<lastName:Cool> indexed,tokenized<emailAddress:test@test.com> indexed,tokenized<emailSplit:test test.com> stored/uncompressed,indexed<organization.id:1> stored/uncompressed,indexed,tokenized<organization.test:TESTFIELD>>


So is there some special annotation I am missing to make it index more than just the things in DataObject on @IndexEmbedded objects?

-- graum


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Wed Sep 30, 2009 9:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Odd. I need to create a testcase in the codebase and see what's going on. I might be a bug.
The problem could be the fact that you are using saveOrUpdate or that Organization also extends DataObject. I somehow suspect the former. Have you tried just using session.save()? It might be worth to see what happens if put the id directly into Organization and not extend DataObject. I don't think it should matter, but you never know.

--Hardy


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Wed Sep 30, 2009 1:32 pm 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
Very odd.. I tried many different combinations here, and I just could not get anything to take. Here is a list of things I tried:

- Put id straight into User and Organization, removing the extension from DataObject
- Tried session.save instead of session.saveOrUpdate
- Tried a OneToOne relationship between User and Organization
- various fetch and cascade strategies

Nothing seemed to take! I always just get organization.id in there. I know I've seen a couple other people have similar problems, yet no solutions. I thought maybe it had to do with ManyToOne.. for instance, these forums had what seems to be similar problems:

http://lists.jboss.org/pipermail/hibern ... 16069.html
viewtopic.php?f=9&t=998448

The threads just kinda die without a solution, but it seems like I'm not the only one who has experienced this one.

Thanks for sticking with me here. If there is any way I can help track the problem, I will..


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Thu Oct 01, 2009 12:40 am 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
Just so you know, I thought I would just test the latest code base in SVN to see if that would potentially fix the problem since I saw that there was some changes in the IndexEmbedded stuff. Still no go though. So the quest continues in figuring out why this is behaving this way.

-- graum


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Mon Oct 05, 2009 4:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

just wondering whether you have solved your problem? I added your entities to a testcase in the trunk of Hibernate Search and it workd just fine to search on the accessCode. Having a common DataObject base class does not cause any problems and I even used saveOrUpdate.

There must be something else going on in your app.

--Hardy


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Mon Oct 05, 2009 12:44 pm 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
Hi,

Yes, I believe I traced the error to another API that was blowing away my values before I saved them out for some reason. Thanks for sticking with me on this one!

Cheers,
-- graum


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Mon Oct 05, 2009 4:48 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Good to hear. I got worried there for a while and thought there is a major bug.
Feel free to let us know what the problem was, if you think other people might run into this problems as well.

--Hardy


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Wed Oct 07, 2009 8:25 am 
Newbie

Joined: Wed Oct 07, 2009 7:34 am
Posts: 12
Please let us know what the problem was...

I run into this problem too.


Top
 Profile  
 
 Post subject: Re: @IndexEmbedded and @ManyToOne not indexing
PostPosted: Wed Oct 07, 2009 12:20 pm 
Newbie

Joined: Mon Sep 28, 2009 7:02 pm
Posts: 8
Hiya,

My problem in particular was that I'm using hibernate along with a framework called stripes. My issue was that one of my beans, the Organization bean in the instance, was not being fully populated when it was being posted back to the server. Therefore, when it saved the user out, yes, it saved and loaded correctly (data wise) because the Organization's primary key was still in tact, and so when I viewed the data, everything looked fine. But on the initial save, not everything was populated in that organization object. One of those "duh" moments. Basically, when I posted to the action, I made sure to fully load any objects that were supposed to get indexed before saving it out.

Anyways, wiji, I would suggest posting your POJOs and how you are saving if you are continuing to have the issue?

-- graum


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 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.