Hardy,
Thanks for the reply. The issue with the previous example was that it didn't return any results. Here's the search code...
Code:
org.hibernate.Session session = (org.hibernate.Session)entityManager.getDelegate();
final String[] FIELD_NAMES = new String[]{"name", "address", "createdDate", "username", "roles.role", "customer.name"};
FullTextSession fullTextSession = Search.createFullTextSession(session);
QueryParser parser = new MultiFieldQueryParser( FIELD_NAMES, new StandardAnalyzer());
org.apache.lucene.search.Query query=null;
try {
query = parser.parse(searchString);
} catch(ParseException pe) {
LOGGER.debug(pe.toString());
}
org.hibernate.search.FullTextQuery hibernateQuery = fullTextSession.createFullTextQuery(query, Customer.class);
List results = hibernateQuery.list();
... I'm not sure what you mean by "indexing code", but I add the customers like this (which creates the index automatically unless I am mistaken)...
Code:
cust=new Customer("someguy@gmail.com", "password", true, true, true, true, "Some Guy", new java.util.Date(), new HashSet<Role>(), new HashSet<Address>());
entityManager.persist(cust);
addy=new Address("SHIPTO", cust.getUsername() + " Addy", "Addy Line 1", "Addy Line 2", "Addy Line 3", "City", "State", "22345", "223-456-7890", "223-456-7891", "223-456-7892", new java.util.Date(), cust);
entityManager.persist(addy);
...I think the problem here may have been my using xml and annotations in the same setup, so I decided to try an explicitly annotation driven example...
Customer.java
Code:
/** persistent field */
@ManyToMany(targetEntity=example.Customer.model.Role.class, cascade = { CascadeType.PERSIST})
@JoinTable(name="CUSTOMER_ROLES",
joinColumns=@JoinColumn(name="CUSTOMER_ID"),
inverseJoinColumns=@JoinColumn(name="ROLE_ID"))
@Type(type="java.util.Set")
@IndexedEmbedded
private Set roles;
Role.java
Code:
/** persistent field */
@Column(name="ENABLED")
@Field
private String role;
/** persistent field */
@ManyToMany(mappedBy="roles", targetEntity=example.Customer.model.Customer.class)
@JoinTable(name="CUSTOMER_ROLES",
joinColumns=@JoinColumn(name="ROLE_ID"),
inverseJoinColumns=@JoinColumn(name="CUSTOMER_ID"))
@ContainedIn
private Set customers;
...and I got an error at startup ...
Code:
Caused by: org.hibernate.AnnotationException: Associations marked as mappedBy must not define database mappings like @JoinTable or @JoinColumn: example.Customer.model.Role.customers
at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:476)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1897)
at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:762)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:726)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:359)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)
at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1449)
... the join table is important (to me) for the role functionality, so I'm moving on to another example with addresses....
Customer.java
Code:
/** persistent field */
@OneToMany(mappedBy="customer", targetEntity=example.Customer.model.Address.class, cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
@IndexedEmbedded
@Type(type="java.util.Set")
private Set addresses;
Address.java
Code:
/** persistent field */
@Column(name="NAME")
@Field private String name;
/** persistent field */
@ManyToOne
@ContainedIn
@JoinColumn(name="CUSTOMER_ID")
private example.Customer.model.Customer customer;
... I'm also having problems with this one. For example, searching for the word "addy" should return the
someguy@gmail.com customer (e.g. the 'customer.name' is "someguy@gmail.com Addy" for the address), but it does not. I can see the addy entry in the Address index (using luke), so I know it's there. I'm still not sure what I'm doing wrong.
Thanks,
Ryan