Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Classbridge - "indexed embedded" polymorphic use case
PostPosted: Tue Feb 02, 2016 7:51 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi All,
I'm trying to index my JournalLogItem with a linkedentity (like a IndexedEmbedded).

I'll try to show my use case with an example:

Code:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntityModel(){
}

public class AddressEntity extends AbstractEntityModel(){
}
public class CarEntity extends AbstractEntityModel(){
}


@Indexed
@Entity
@ClassBridges({ @ClassBridge(name = "MyCustomFieldBridge", impl = MyCustomFieldBridge.class) })
public class JournalLogItem (){

  @IndexEmbedded
  private AbstractEntityModel linkedEntity;

  private String linkedEntityClassName;
  private String linkedEntityId;

  @CustomEntityField(classDiscriminatorField = "linkedEntityClassName", entityIdField = "linkedEntityId") 
  public AbstractEntityModel getLinkedEntity(){
    // ?? how can I return the correct entity?
  }
 
}

public class MyCustomFieldBridge implements FieldBridge {
  @Override
  public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
    if (value instanceof JournalLogItem ) {
      // ?? Can I access to PersistenceContext?
    }
  }
}




How can I return the correct linked entity?
By a custom ClassBridge can I get the entity from the PersistenceContext? Is there a way to get the actual PersistenceContext from FieldBridge?


Thanks,
dario


Top
 Profile  
 
 Post subject: Re: Classbridge - "indexed embedded" polymorphic use case
PostPosted: Wed Feb 03, 2016 2:40 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 386
Let's take a step back, what is the use case you are actually trying to implement, i.e. what are the requirements here? Knowing this will help to find a good solution.

Btw. it seems you try build some sort of auditing mechanism, have you looked into Hibernate Envers?

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Classbridge - "indexed embedded" polymorphic use case
PostPosted: Wed Feb 03, 2016 11:24 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
By a custom ClassBridge can I get the entity from the PersistenceContext?


That's what you get in the "Object value" parameter of the ClassBridge method:

Code:
public void set(String name, Object value, Document document, LuceneOptions luceneOptions)


N.B. when you use a ClassBridge you have to encode all fields on your own, so for example the @IndexEmbedded on linkedEntity will be ignored.

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


Top
 Profile  
 
 Post subject: Re: Classbridge - "indexed embedded" polymorphic use case
PostPosted: Thu Feb 04, 2016 12:12 pm 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi,
my use case is not a auditing mechanism. I already use Hibernate Envers for that scope.

I have a linked abstract entity: I need to indexing this entity at runtime when I will know his classname.
I resolved the issue using @Any/@AnyMetaDef hibernate annotation to describe this kind of relation. Now seem it working.

My doubt is: Can I access to EntityManager during the indexing from the "set" method of the classbridge?

@Sanne wrote: "N.B. when you use a ClassBridge you have to encode all fields on your own, so for example the @IndexEmbedded on linkedEntity will be ignored."
I have a question: The @ContainedIn mechanism is ignored when the ClassBridge is defined? I just bumped another problem.. I'll open another thread for that.


Dario


Top
 Profile  
 
 Post subject: Re: Classbridge - "indexed embedded" polymorphic use case
PostPosted: Thu Feb 04, 2016 5:52 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
My doubt is: Can I access to EntityManager during the indexing from the "set" method of the classbridge?


The short answer is no. Normally I'd expect the entity to have all information you need; is the question you need to run additional queries on the database?
It is ok to walk relations of the entity, even if they are lazy-loaded.
Another trick is to have some @Transient getters on the entity, were you can store additional metadata which you might need at indexing time.

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


Top
 Profile  
 
 Post subject: Re: Classbridge - "indexed embedded" polymorphic use case
PostPosted: Fri Feb 05, 2016 4:34 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi Sanne,
I'll keep it in mind!

I already used transient fields for that scope. Thanks for your suggestions!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 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.