-->
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.  [ 5 posts ] 
Author Message
 Post subject: Embedded update problem using IndexEmbedded ContainedIn
PostPosted: Fri May 21, 2010 6:46 am 
Newbie

Joined: Fri May 21, 2010 4:20 am
Posts: 6
I have 2 classes Item and Desk. An item can be "desked" at one Desk. Only the Item is indexed in Lucene. (Desk is indexed using the IndexEmbedded annotation)
The problem is that when the name of a Desk is updated, the Item index is not updated, altough I'm using IndexEmbedded and ContainedIn...

Here are the 2 classes:
Code:
@Entity
@Indexed
@Table(name="ITEMS")
public final class Item {
    private static final int MAX_SHORT_STRING_LENGTH = 55;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqItems")
    @SequenceGenerator(name="seqItems", sequenceName="SEQ_ITEMS",allocationSize = 1)
    private Long id;

    @ManyToOne(fetch= FetchType.LAZY)
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name="desk_id", nullable=true)
    @IndexedEmbedded
    private Desk desk;
}


Code:
@Entity
@Table(name="DESKS")
public final class Desk {
   
    @Id
    @GeneratedValue(strategy= GenerationType.SEQUENCE, generator="seqDesks")
    @SequenceGenerator(name="seqDesks", sequenceName="SEQ_DESKS",allocationSize = 1)
    private Long id;

    @Column(name="name", nullable = false)
    @Field(index= Index.TOKENIZED)
    @Analyzer(impl = SimpleAnalyzer.class) //Only applies the toLowerCase Tokenizer
    private String name;

    @ContainedIn
    @OneToMany(mappedBy="desk", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
    private Set<Item> items;
}


The test I use to reproduce the problem:
Code:
@Test
    public void testSave() throws LuceneQueryFactoryException, ParseException {
       
        //Save Wire
        Wire wire = new Wire();
        wire.setName(DeskDaoImplIT.class.getSimpleName() + "Wire");
        wireDao.persist(wire);

        //Save desk
        Desk desk = new Desk();
        desk.setName(DeskDaoImplIT.class.getSimpleName() + "Desk");
        deskDao.persist(desk);
        assertNotNull(desk.getId());

        //Save item
        Item item = new Item();
        item.setCode("testitem");
        item.setDesk(desk);
        item.setWire(wire);
        itemDao.persist(item);
        assertNotNull(item.getId());

        itemDao.flushToIndexes();

        LuceneQueryFactory factory = LuceneQueryFactoryAPIImpl.getInstance();
        QueryDto q = new QueryDto();
        DeskDto deskDto = new DeskDto();
        deskDto.setName(desk.getName());
        q.setDesk(deskDto);
        PaginationItemModelDto result = itemDao.searchItems(factory.createLuceneQuery(q), 10,0, SortDto.NONE);
        assertTrue(result.getResultSize() > 0);

        desk.setName("newdeskname");
        itemDao.update(item);

        Desk deskFromDao = deskDao.find(desk.getId());
        assertEquals(desk, deskFromDao);

        itemDao.flushToIndexes();

        q = new QueryDto();
        deskDto = new DeskDto();
        deskDto.setName(desk.getName());
        q.setDesk(deskDto);
        result = itemDao.searchItems(factory.createLuceneQuery(q), 10,0, SortDto.NONE);
        assertTrue(result.getResultSize() > 0);
    }

It is the last assert that is failing (assertTrue)

I'm using Hibernate Search 3.2.0.Final:
Code:
<dependency>
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-search</artifactId>
       <version>3.2.0.Final</version>
</dependency>


Can anybody tell me what I'm doing wrong?

PS
If I add the @indexed annotation above Desk. The update is executed in the Desk index, but still not in the Item index.


Top
 Profile  
 
 Post subject: Re: Embedded update problem using IndexEmbedded ContainedIn
PostPosted: Fri May 21, 2010 9:03 am 
Hibernate Team
Hibernate Team

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

there seems to be a lot happening behind the scenes in your example. For example transaction management. However, one question which comes in my mind when looking at your code is, whether you properly update both sides of your bidirectional relationship? If you add a desk to an item, do you also properly update the items set in desk? It's the responsibility of the developer to update both sides of a bidirectional relationship.

--Hardy


Top
 Profile  
 
 Post subject: Re: Embedded update problem using IndexEmbedded ContainedIn
PostPosted: Fri May 21, 2010 9:34 am 
Newbie

Joined: Fri May 21, 2010 4:20 am
Posts: 6
Thanks for your reply.

Indeed, adding the following code to the test fixes the problem:
Code:
Set items = new TreeSet();
items.add(item);
desk.setItems(items);


If for example, 100 items are desked at some desk. Will I need to query all those items. Adding them to the desk I'm updating before I persist desk with the new name??


Top
 Profile  
 
 Post subject: Re: Embedded update problem using IndexEmbedded ContainedIn
PostPosted: Fri May 21, 2010 9:53 am 
Newbie

Joined: Fri May 21, 2010 4:20 am
Posts: 6
Changing my saveDesk function did the trick in the application.

When receiving a DeskDto from the frontend. Instead of just creating the desk using the parameters from the DeskDto. I did the following:
Code:
Desk desk = deskDao.find(deskDto.getId());
desk.setName(deskDto.getName());
deskDao.update(desk);


With this code, the Item index was updated as expected.


Top
 Profile  
 
 Post subject: Re: Embedded update problem using IndexEmbedded ContainedIn
PostPosted: Wed May 26, 2010 4:02 am 
Newbie

Joined: Fri May 21, 2010 4:20 am
Posts: 6
Well both: in the test I manually added the items to the desk object, where in the application, hibernate did this for me when I loaded the desk:

Code:
Desk desk = deskDao.find(deskDto.getId());


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