Hi,
I'm new on this forum and on hibernate search.
I would like to develop a custom fieldBridge (TwoWayFieldBridge) for a composite key.
Here is the concerned entity:
Code:
@Entity
@Indexed(index="person")
@Table(name="PERSON")
public class Person extends VersionedObject implements Serializable, Cloneable{...}
The pk is stored inside "versionedObject" class defined as follow:
As you can see the pk is an object.
Code:
@MappedSuperclass
public abstract class VersionedObject implements Cloneable, Serializable {
@Id
private VersionKey pk;
...
...
}
Here is the VersionKey object:
So the pk is in fact devided into two elements id & version.
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;
public VersionKey() {
super();
}
Here is the version object:
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;
@CollectionOfElements
@JoinTable(
name="TAGS",
joinColumns=@JoinColumn(name="V_NO")
)
@Column(name="LABEL", length=64)
private Set<String> tags;
I would like to know how do I have to create the TwoWayFieldBridge.
Do I have to save in the index file each attribute from Version Entity + the id from VersionKey Entity and then sets all the attribute of the object... ?
like having the following class:
Code:
public class PkFieldBridge implements TwoWayFieldBridge {
public Object get(String fieldName, Document doc) {
VersionKey questionPK = new VersionKey();
Field field = null;
field = doc.getField(fieldName + ".id");
questionPK.setId(new Integer(field.stringValue()));
field = doc.getField(fieldName + ".valueNb");
Version version = new Version();
field = doc.getField(fieldName + ".message");
version.setMessage(field.stringValue());
field = doc.getField(fieldName + ".userId");
version.setUserId(field.stringValue());
field = doc.getField(fieldName + ".valueNb");
version.setValueNb(Integer.parseInt(field.stringValue()));
questionPK.setVersion(version);
....
// set the date by getting all part of the date (day, month, hour,..) which is stored in the index???
// or can i retrieve the object directly???
...
return questionPK;
}