-->
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.  [ 11 posts ] 
Author Message
 Post subject: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 4:46 am 
Newbie

Joined: Thu Jul 07, 2011 5:18 am
Posts: 10
Hi, I'm using hibernate-search 3.6.3 with hibernate 3.4.0 and JPA. Indexing works almost perfect, but I can't get hibernate index my nested relation automatically. When I run my manual indexing function, all the indexes are correctly updated.

I have the following classes :
Code:
@Entity
@Indexed
public class One {
@IndexedEmbedded
private List<Two> two;

@OneToMany(mappedBy= "one", cascade = CascadeType.ALL)
public List<Two> getTwo() {
  return two;
}

}






@Entity
public class Two{
@ContainedIn
private One one;

@IndexedEmbedded
private List<Three> three;

@ManyToOne
@JoinColumn(name = "one_fk")
public One getOne(){
  return one;
}

@OneToMany(mappedBy="two", cascade = CascadeType.ALL)
public List<three> getThree() {
  return three;
}

}






@Entity
public class Three {
@ContainedIn
private Two two;
@Field(index=Index.TOKENIZED, store=Store.NO)
private String name;

@ManyToOne
@JoinColumn(name = "two_fk")
public Two two() {
  return two;
}
}




Does anyone know how why hibernate-search doesn't automatically update the index ?


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 5:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Have you tried to place your annotation consistently, either on the getter or on the fields?
And are you sure it works with manual indexing, but not with automatic indexing? Can you post your code between Session open and close?

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 5:47 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
hibernate-search 3.6.3 with hibernate 3.4.0

I guess it's the other way around, since these versions don't exist?

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


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 5:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
that as well :-)


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 6:35 am 
Newbie

Joined: Thu Jul 07, 2011 5:18 am
Posts: 10
s.grinovero wrote:
Quote:
hibernate-search 3.6.3 with hibernate 3.4.0

I guess it's the other way around, since these versions don't exist?


oops, yes thats true :)


hardy.ferentschik wrote:
Have you tried to place your annotation consistently, either on the getter or on the fields?


Yes I tried that, unfortunately without any differences.


hardy.ferentschik wrote:
And are you sure it works with manual indexing, but not with automatic indexing?


I have this function in the DAO:
Code:
public void updateIndex() {
      FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
      try {
         fullTextEntityManager.createIndexer().startAndWait();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
}


If I run this function after something changed in the DB, Indexes are updated perfectly.


hardy.ferentschik wrote:
Can you post your code between Session open and close?


Since I'm using JPA I do not have any Session open and close ? Or did I get something wrong ?
I'm using it with Spring 3.0.5.RELEASE


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 7:04 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
Since I'm using JPA I do not have any Session open and close ? Or did I get something wrong ?
I'm using it with Spring 3.0.5.RELEASE

in this case what happens between the EntityManager#open and close or whatever you do with the EntityManager.
Now that you mentioned Spring I wonder how you set up transactions. Which transaction manager are you using? Search for Spring and transaction and you will find a lot of post around this topic. Not all transaction managers will work and propagate properly the transaction end to Hibernate Search.

You can also turn on debug/trace level logging to see what happens within Hibernate Search.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 7:22 am 
Newbie

Joined: Thu Jul 07, 2011 5:18 am
Posts: 10
hardy.ferentschik wrote:
Quote:
Since I'm using JPA I do not have any Session open and close ? Or did I get something wrong ?
I'm using it with Spring 3.0.5.RELEASE

in this case what happens between the EntityManager#open and close or whatever you do with the EntityManager.
Now that you mentioned Spring I wonder how you set up transactions. Which transaction manager are you using? Search for Spring and transaction and you will find a lot of post around this topic. Not all transaction managers will work and propagate properly the transaction end to Hibernate Search.

You can also turn on debug/trace level logging to see what happens within Hibernate Search.

--Hardy


I'm using this :

Code:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEmf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>


I just annotated the service-class with @Repository and @Transaction. With the Entitymanager I take the data out of the DAOs to the service. And after that I'm using the data in Beans with JSF. Sry I have no clue what you mean with what happens between open and close or where I can see what happens between open and close. I just annotated Entitymanager with @PersistenceContext in the DAO-class and use it to save, update and so on. I never manually call open or close.
But thanks for your help, I'll try to search for Spring with transaction, maybe I find something useful.


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 7:49 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
toxor wrote:
I just annotated the service-class with @Repository and @Transaction.

I assume you mean @Transactional, right?

toxor wrote:
With the Entitymanager I take the data out of the DAOs to the service. And after that I'm using the data in Beans with JSF. Sry I have no clue what you mean with what happens between open and close or where I can see what happens between open and close.

I mean what happens in your DAOs or with other words within your transactional methods. In this type of setup you don't have an explicit call to open and close of the entity manager, but that does not mean they don't exist. The framework (spring in this case is taking care of it).

toxor wrote:
I just annotated Entitymanager with @PersistenceContext in the DAO-class and use it to save, update and so on. I never manually call open or close.

Because the framework does it for you.

In which container are your running. Depending on the container you need to pick the right transaction manager.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 8:04 am 
Newbie

Joined: Thu Jul 07, 2011 5:18 am
Posts: 10
hardy.ferentschik wrote:
toxor wrote:
I just annotated the service-class with @Repository and @Transaction.

I assume you mean @Transactional, right?


Oops I did it again, so sry, you are right its ofc @Transactional.

hardy.ferentschik wrote:
In which container are your running. Depending on the container you need to pick the right transaction manager.


This is my remaining db config :
Code:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
       <property name="driverClass" value="${db.driverClassName}"/>
       <property name="jdbcUrl" value="${db.url}"/>
       <property name="user" value="${db.username}"/>
       <property name="password" value="${db.password}"/>
</bean>


<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="dataSource" ref="dataSource"/>
     <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">                 
                <property name="generateDdl" value="true"/>
            </bean>
        </property>   
        <property name="persistenceUnitName" value="myTool"/>   
</bean>




hardy.ferentschik wrote:
I mean what happens in your DAOs or with other words within your transactional methods. In this type of setup you don't have an explicit call to open and close of the entity manager, but that does not mean they don't exist. The framework (spring in this case is taking care of it).


Ah ok, yes that is what I thought. My DAOs just look like this :

Code:
public class OneDAOimpl implements IOneDAO {
@PersistenceContext
private EntityManager em;

public void save(One one) {
em.persist(one);
}

public One findById(long Id) {
return em.find(One.class, Id);
}

public void update(One one) {
em.merge(one);
}

public void remove(One one) {
em.remove(one);
}

public void updateIndex() {
  ...
}

public List<One> searchByKey(String key) {
      
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity( One.class ).get();
org.apache.lucene.search.Query query = qb.keyword().onFields("two.three.name").matching(key).createQuery();
javax.persistence.Query persistenceQuery = fullTextEntityManager.createFullTextQuery(query, One.class);
@SuppressWarnings("unchecked")
List<One> result = persistenceQuery.getResultList();      
return result;
}

}


Last edited by toxor on Tue Aug 16, 2011 8:55 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 8:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
When you are updating entities are you updating both ends of the bidirectional association? How does the code look like you use when updating an entity and which in your opinion should trigger a index update?


Top
 Profile  
 
 Post subject: Re: Hibernate Search - Nested Objects - no update of index
PostPosted: Tue Aug 16, 2011 8:54 am 
Newbie

Joined: Thu Jul 07, 2011 5:18 am
Posts: 10
hardy.ferentschik wrote:
When you are updating entities are you updating both ends of the bidirectional association? How does the code look like you use when updating an entity and which in your opinion should trigger a index update?


I have this function in the service-class which creates Object Two, while Object One already exist. It looks like this :
Code:
public void createTwo_ID(List<Three> threeList, long one_ID) {
One one= ioneDAO.findById(one_ID);
Two two= itwoDAO.createNew();
two.setOne(one);
for(Three t : threeList) {
String name = t.getName();
Three three = ithreeDAO.createNew();
three.setName(name);
three.setTwo(two);
ithreeDAO.save(three);
}

itwoDAO.save(two);
//ioneDAO.update(one);
}


I hoped that ioneDAO.update(one) would update the index, but that also failed.


EDIT : Ok your are rigth, as you can see I'm not updating both ends of all associations.
Thank you very much for helping me. I update now both ends and now it is updated correctly.


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