I'm having a problem with bi-directional OneToOne mapping and searching from association mapped by @IndexedEmbedded. Seems that search returns all rows from database but only those rows that have identifier matched the search. But those elements not matching the search have null identifier. When session.load(id) is tried for those elements exception is thrown.
Roughly I have following mapping :
Code:
@Entity
@Indexed
public class Place {
@Id
@GeneratedValue
@DocumentId
private Long id;
@Field
private String name;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "place")
@IndexedEmbedded
private Address address;
....
}
@Entity
@Indexed
public class Address {
@Id
@GeneratedValue
@DocumentId
private Long id;
@Field
private String street;
@Field
private String city;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="place_f_id")
@ContainedIn
private Place place;
...
}
When I execute following query
Code:
MultiFieldQueryParser parser = new MultiFieldQueryParser(
new String[]{ "address.street" },
new StandardAnalyzer());
Query query = null;
try {
query = parser.parse(search);
} catch (ParseException e) {
throw new RuntimeException(e.getLocalizedMessage());
}
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Place.class);
List<Place> result = hibQuery.list();
I will get following error
Code:
java.lang.IllegalArgumentException: id to load is required for loading
at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:51)
at org.hibernate.event.LoadEvent.<init>(LoadEvent.java:33)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:792)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:788)
at org.hibernate.search.engine.QueryLoader.load(QueryLoader.java:84)
at org.hibernate.search.query.FullTextQueryImpl.list(FullTextQueryImpl.java:244)
Error happens in the query loader which received all database rows but only those rows that matched the search have identifiers.
When this code is executed in the QueryLoader
Code:
for (EntityInfo entityInfo : entityInfos) {
Object element = session.load( entityInfo.clazz, entityInfo.id );
if ( Hibernate.isInitialized( element ) ) {
//all existing elements should have been loaded by the query,
//the other ones are missing ones
result.add( element );
}
}
session.load will of course throw this exception when trying to fetch object with null identifier.
Am I missing something in the set up or in the query or is this bug in hibernate search? I can fix this in the QueryLoader by checking for null values but I don't know if it will cause side effects.
Thanks for your help.