-->
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: Associated entity
PostPosted: Fri Nov 05, 2010 4:44 am 
Beginner
Beginner

Joined: Wed Nov 03, 2010 1:05 pm
Posts: 24
Hello,

Small question, I have association between 2 entities (Person & Version) as follow:

This class is part of person entity:

Code:
@MappedSuperclass
public abstract class VersionedObject implements Cloneable, Serializable {

  @Id
  @DocumentId
  @FieldBridge(impl=PkFieldBridge.class)
  private VersionKey pk;


VersionKey:

Code:
@Embeddable
public class VersionKey implements Serializable, Cloneable {
  @JoinColumn(name="V_NO")
  @ManyToOne(targetEntity=Version.class, fetch=FetchType.EAGER)
  private Version version;

  @Column(nullable=false)
  private Integer id;


Here the fetch type is eager.
Each time that I use hibernate (not hibernate search) to retrieve the person entity, I have the Version object attached without any problem.

But once that I use hibernate search, I have all my Persons objects but without the version attached.
Do I have to add something in the Version entity?
Currently it's defined like a simple entity:

Code:
@Entity
@Table(name="VERSIONS")
public class Version implements Serializable {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="V_NO", length=10)
  private Integer valueNb;

  @Column(nullable=false)
  @Temporal(TemporalType.TIMESTAMP)
  private Date tstamp;

  @Column(length=UserProfile.CAID_LENGTH, nullable=false)
  private String userId;

  @Column(length=500)
  private String message;


Top
 Profile  
 
 Post subject: Re: Associated entity
PostPosted: Fri Nov 05, 2010 6:32 am 
Beginner
Beginner

Joined: Wed Nov 03, 2010 1:05 pm
Posts: 24
Maybe it's only because of the custom bridge.

In the custom bridge that implements TwoWayFieldBridge I have the get & set method.

If I had only one attribute used as Id, it could be easy.
In those method, how can I deal with two attributes that are used to find the version of the person entity?
I imagine that I only have to return the keys (the id attribute from VersionKey & the valueNb from Version )in order for H.S to find the object?

Thanks a lot :-)


Top
 Profile  
 
 Post subject: Re: Associated entity
PostPosted: Fri Nov 05, 2010 9:11 am 
Beginner
Beginner

Joined: Wed Nov 03, 2010 1:05 pm
Posts: 24
Here is what I got so far for the custom bridge:

Code:
public class PkFieldBridge implements TwoWayFieldBridge {

  public Object get(String fieldName, Document doc) {
   
    VersionKey versionPK = new VersionKey();
    versionPK.setVersion(new Version());
    Field field = null;   

    field = doc.getField(fieldName + ".id");
    versionPK.setId(Integer.parseInt(field.stringValue()));
   
    field = doc.getField(fieldName + ".valueNb");
    versionPK.getVersion().setValueNb(Integer.parseInt(field.stringValue()));

    return versionPK;
  }

  public String objectToString(Object obj) {
    VersionKey questionPK = (VersionKey) obj;
    StringBuilder sb = new StringBuilder();
    sb.append(questionPK.getId()).append(" ").append(questionPK.getVersion().getValueNb());

    return sb.toString();
  }

  public void set(String fieldName, Object obj, Document doc, LuceneOptions luceneOptions) {
   
    VersionKey versionPK = (VersionKey) obj;
   
    Field field = new Field(fieldName + ".id", String.valueOf(versionPK.getId().intValue()), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);

    field = new Field(fieldName + ".valueNb", String.valueOf(versionPK.getVersion().getValueNb().intValue()), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);
  }

}


What I do it's only instantiate the VersionKey with the Id + the id of the version.
If I give only the id's, I suppose it's going to search in the db the associated Person entity + the versionKey
(After HS request) But When I check the Version object from the return Person entity, I can see that the version is almost empty except the valueNb attribute: Version::[ valueNb:=2836 tstamp:=null userId:=null message:=null tags:=null]


Top
 Profile  
 
 Post subject: Re: Associated entity
PostPosted: Fri Nov 05, 2010 11:53 am 
Beginner
Beginner

Joined: Wed Nov 03, 2010 1:05 pm
Posts: 24
I fixed the problem by setting all the attributes from Version in the index instead of just setting the id from versionPk and valueNb from Version.

I don't know if it's the correct way to fix that?
If someone can give me an advice.


Code:
public class PkFieldBridge implements TwoWayFieldBridge {

  public Object get(String fieldName, Document doc) {
   
    VersionKey versionPK = new VersionKey();
    versionPK.setVersion(new Version());
    Field field = null;
   
    field = doc.getField(fieldName + ".id");
    versionPK.setId(Integer.parseInt(field.stringValue()));
   
    field = doc.getField(fieldName + ".valueNb");
    versionPK.getVersion().setValueNb(Integer.parseInt(field.stringValue()));
   
   
    field = doc.getField(fieldName + ".message");
    versionPK.getVersion().setMessage(field.stringValue());
   
    field = doc.getField(fieldName + ".tags");
    versionPK.getVersion().setTags(getTagSet(field.stringValue()));
   
    field = doc.getField(fieldName + ".tstamp");
    versionPK.getVersion().setTstamp(getTimeStamp(field.stringValue()));
   
    field = doc.getField(fieldName + ".userId");
    versionPK.getVersion().setUserId(field.stringValue());
   
    return versionPK;
  }

  public String objectToString(Object obj) {
    VersionKey questionPK = (VersionKey) obj;
    StringBuilder sb = new StringBuilder();
    sb.append(questionPK.getId()).append(" ").append(questionPK.getVersion().getValueNb());

    return sb.toString();
  }


  public void set(String fieldName, Object obj, Document doc, LuceneOptions luceneOptions) {
    VersionKey versionPK = (VersionKey) obj;
   
    Field field = new Field(fieldName + ".id", String.valueOf(versionPK.getId().intValue()), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);

    field = new Field(fieldName + ".valueNb", String.valueOf(versionPK.getVersion().getValueNb().intValue()), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);
   
   
   
   
    field = new Field(fieldName + ".message", String.valueOf(versionPK.getVersion().getMessage()), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);
   
    field = new Field(fieldName + ".tags", String.valueOf(setTagSet(versionPK.getVersion().getTags())), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);
   
   
    field = new Field(fieldName + ".tstamp", String.valueOf(setTimeStamp(versionPK.getVersion().getTstamp())), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);
   
    field = new Field(fieldName + ".userId", String.valueOf(versionPK.getVersion().getUserId()), luceneOptions.getStore(), luceneOptions.getIndex());

    if (luceneOptions.getBoost() != null) {
      field.setBoost(luceneOptions.getBoost());
    }

    doc.add(field);
   
  }


Top
 Profile  
 
 Post subject: Re: Associated entity
PostPosted: Mon Nov 08, 2010 4:08 am 
Beginner
Beginner

Joined: Wed Nov 03, 2010 1:05 pm
Posts: 24
Good morning :-)

I'm wondering if this code is correct or not.. cause I can see duplicate entries in my index.

step 1°/ If I create a person object --> I have 1 new entry in the DB (Person)+ I have 1 new entry in the DB (Version) + 1 new entry in the index.

step 2°/ If I update the person object --> The person & version objects are updated in the DB + the first entry in the index (the creation of the person in step1) is duplicated in the index + new entry in the index with the updated person.

Is this behavior because of the custom bridge?

Have a nice week !


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.