-->
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: @ContainedIn and @IndexEmbedded Collections
PostPosted: Mon Oct 15, 2007 1:16 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

I have 2 classes Contact and Address. The following shows snippets of code for both classes:
Contact.java
Code:
@ContainedIn
    @OneToMany(targetEntity=com.amin.phonebook.domain.Address.class, cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
    @Type(type="java.util.Set")
    private Set<Address> addresses;


Address.java
Code:
has address1, address2,

   @ManyToOne
   @JoinColumn(name="C_CONTACT_ID")
   @IndexedEmbedded
   private Contact contact;


I have some code to perform searching on Address

Code:

org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query, Contact.class );
                  


Using the following fields:
Code:
"addresses.address1", "addresses.address2", "addresses.town", "addresses.county", "addresses.country",
                        "addresses.postcode",


I'm not sure if this is a problem or perhaps i am doing it incorrectly. When i ran my test case using the above snippets of code i was not getting any results (for example i was trying to search on address1). I have set up Contact according to the documentation. However when i looked at sample test cases (hibernate search distribution) i noticed collections had @IndexEmbedded instead of @ContainedIn. So i updtaed my code to the following:
Code:
@ContainedIn
    @OneToMany(targetEntity=com.amin.phonebook.domain.Address.class, cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
    @IndexedEmbedded
    @Type(type="java.util.Set")
    private Set<Address> addresses;



And then when I ran my test case it worked. I was wondering if someone could tell me whether i use just @IndexEmbedded or @ContainedIn for querying collections. Obviously @IndexEmbedded worked but I am not sure if this is the correct approach. Any help would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 4:52 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi

Any chance if someone might be able to help on this matter....


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 11:49 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@IndexedEmbedded on addresses of Contact means that you will be able to do queries like addressed.address1

@IndexedEmbedded on contact of Address means that you will be able to do queries like contact.name

@ContainedIn is here to say, if you update the object having a @ContainedIn, you need to update the index data of the associated object
So typically @ContainedIn is on the other side of an @indexedEmbedded

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 02, 2007 12:21 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hi there


Thanks for your reply. So basically i need to have both.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 02, 2007 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
but not in the same property

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 12:07 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Hmm...sorry i'm slightly confused. If i need to do queries such as addresses.address1 = something then i need @IndexEmbedded on addresses in Contact. But I presume I need @ContainedIn as well on addresses based on the description:

"@ContainedIn is useful on embedded objects that are also entities (like Address in this example): it basically means that when an address entity is updated, the index document of the associated Place(s), also has to be updated."


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 05, 2007 6:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no you need @ContainedIn on the other side of the relationship

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 4:38 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
Ok. So I need to do the following:

Code:
    @OneToMany(targetEntity=com.amin.phonebook.domain.Address.class, cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
    @IndexedEmbedded
    @Type(type="java.util.Set")
    private Set<Address> addresses;


and on the Address side do the following:

Code:
@ContainedIn
@ManyToOne
   @JoinColumn(name="C_CONTACT_ID")
    private Contact contact;


This means that when Address is updated then the index on Contact needs to be updated as well because it contains a set of address entities (sorry talking out aloud then!)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 06, 2007 10:40 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes you're correct, except that you forgot the mappedBy attribute in @OneToMany to make it a true bidirectional relationship (your current example has 2 unidirectional relationships)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 07, 2007 5:01 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
ok..so final result should look like this

Contact.java
Code:
@OneToMany(mappedBy="contact" targetEntity=com.amin.phonebook.domain.Address.class, cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
    @IndexedEmbedded
    @Type(type="java.util.Set")
    private Set<Address> addresses;


Address.java
Code:
@ContainedIn
@ManyToOne
   @JoinColumn(name="C_CONTACT_ID")
    private Contact contact;




Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 09, 2007 6:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes, targetEntity is not useful though

_________________
Emmanuel


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.