|
The proposed pull request is working. Unfortunately it is limited to embedded. Is there a way to query entities based on partial composite key information through embedded[Ids].
public void embeddedIdQuery() throws Exception {
EntityManager entityManager = emf.createEntityManager();
OgmSession session = (OgmSession) HibernateUtils.getHibernateSession(entityManager); Transaction transaction = session.beginTransaction();
try { // only the firstName information is available for query. List<?> result1 = session.createQuery( "from Person e where *e.id.firstName* = 'Emmanuel'" ).list(); // the entire composite key elements have to be available in order to identify a entity PersonPK emmanuelPk = new PersonPK(); emmanuelPk.setFirstName( "Emmanuel" ); emmanuelPk.setLastName( "Bernard" ); Object result2 = session.get(Person.class, emmanuelPk); transaction.commit(); }
finally { session.clear(); session.close(); }
}
@Entity @Indexed public class Person {
@EmbeddedId @FieldBridge(impl = PersonPKBridge.class) @DocumentId private PersonPK id;
private String favoriteColor;
public PersonPK getId() { return id; }
public void setId(PersonPK id) { this.id = id; }
@Field public String getFavoriteColor() { return favoriteColor; }
public void setFavoriteColor(String favoriteColor) { this.favoriteColor = favoriteColor; }
}
@Embeddable public class PersonPK implements Serializable {
private static final long serialVersionUID = 1L;
private String firstName; private String lastName;
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
@Override public boolean equals(Object o) { if ( this == o ) { return true; }
if ( !( o instanceof PersonPK ) ) { return false; } PersonPK personPK = (PersonPK) o; if ( firstName != null ? !firstName.equals( personPK.firstName ) : personPK.firstName != null ) { return false; }
if ( lastName != null ? !lastName.equals( personPK.lastName ) : personPK.lastName != null ) { return false; }
return true; }
@Override public int hashCode() { int result; result = ( firstName != null ? firstName.hashCode() : 0 ); result = 31 * result + ( lastName != null ? lastName.hashCode() : 0 ); return result; }
}
public class PersonPKBridge implements TwoWayFieldBridge {
@Override public Object get(String name, Document document) { PersonPK id = new PersonPK(); IndexableField field = document.getField( name + ".firstName" ); id.setFirstName( field.stringValue() ); field = document.getField( name + ".lastName" ); id.setLastName( field.stringValue() ); return id; }
@Override public String objectToString(Object object) { PersonPK id = (PersonPK) object; StringBuilder sb = new StringBuilder(); sb.append( id.getFirstName() ).append( " " ).append( id.getLastName() ); return sb.toString(); }
@Override public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { PersonPK id = (PersonPK) value; //store each property in a unique field luceneOptions.addFieldToDocument( name + ".firstName", id.getFirstName(), document ); luceneOptions.addFieldToDocument( name + ".lastName", id.getLastName(), document ); //store the unique string representation in the named field luceneOptions.addFieldToDocument( name, objectToString( id ), document ); }
}
public void embeddedIdPersist() { PersonPK emmanuelPk = new PersonPK(); emmanuelPk.setFirstName( "Emmanuel" ); emmanuelPk.setLastName( "Bernard" ); Person emmanuel = new Person(); emmanuel.setFavoriteColor( "Blue" ); emmanuel.setId( emmanuelPk ); PersonPK petePk = new PersonPK(); petePk.setFirstName( "Pete" ); petePk.setLastName( "Drexler" ); Person pete = new Person(); pete.setFavoriteColor( "Green" ); pete.setId( petePk ); persist(emmanuel, pete); }
|