The problem is, that when I run a query, I get the proper number of rows, but they are all for the first record. Why am I not able to see values from all rows? The database table definitely has 23 different rows of different data.
Anything I can supply/do to debug? Thanks!
I have the following components:
Code:
package com.nimsoft.ca.search;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;
/**
* The persistent class for the best_practices__kav database table.
*
*/
@Entity
@Indexed
@Table(name = "best_practices__kav")
public class BestPractices {
private char id;
private String articletype;
private Date firstpublisheddate;
private Date lastpublisheddate;
private Date lastmodifieddate;
private String articlenumber;
private String title;
private String summary;
private String best_practices__c;
public BestPractices() {
}
public BestPractices(char id, String articletype, Date firstpublisheddate, Date lastpublisheddate, Date lastmodifieddate, String articlenumber, String title, String summary, String best_practices__c) {
this.id = id;
this.articletype = articletype;
this.firstpublisheddate = firstpublisheddate;
this.lastpublisheddate = lastpublisheddate;
this.lastmodifieddate = lastmodifieddate;
this.articlenumber = articlenumber;
this.title = title;
this.summary = summary;
this.best_practices__c = best_practices__c;
}
@Id
@Field(index = Index.NO, analyze = Analyze.NO, store = Store.YES)
public char getId() {
return this.id;
}
public void setId(char id) {
this.id = id;
}
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
public String getArticleType() {
return this.articletype;
}
public void setArticleType(String articletype) {
this.articletype = articletype;
}
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
public String getArticleNumber() {
return this.articlenumber;
}
public void setArticleNumber(String articlenumber) {
this.articlenumber = articlenumber;
}
@Field(index = Index.NO, analyze = Analyze.NO, store = Store.YES)
@DateBridge(resolution=Resolution.MINUTE)
public Date getFirstPublishedDate() {
return firstpublisheddate;
}
public void setFirstPublishedDate(Date firstpublisheddate) {
this.firstpublisheddate = firstpublisheddate;
}
@Field(index = Index.NO, analyze = Analyze.NO, store = Store.YES)
@DateBridge(resolution=Resolution.MINUTE)
public Date getLastPublishedDate() {
return lastpublisheddate;
}
public void setLastPublishedDate(Date lastpublisheddate) {
this.lastpublisheddate = lastpublisheddate;
}
@Field(index = Index.NO, analyze = Analyze.NO, store = Store.YES)
@DateBridge(resolution=Resolution.MINUTE)
public Date getLastModifiedDate() {
return lastmodifieddate;
}
public void setLastModifiedDate(Date lastmodifieddate) {
this.lastmodifieddate = lastmodifieddate;
}
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
public String getSummary() {
return this.summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
public String getBest_Practices__c() {
return this.best_practices__c;
}
public void setBest_Practices__c(String best_practices__c) {
this.best_practices__c = best_practices__c;
}
}
Code:
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
public class App {
private static void doIndex() throws InterruptedException {
Session session = HibernateUtil.getSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();
fullTextSession.close();
}
private static List<BestPractices> search(String queryString) {
Session session = HibernateUtil.getSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(BestPractices.class).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("title").matching(queryString).createQuery();
// wrap Lucene query in a javax.persistence.Query
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, BestPractices.class);
@SuppressWarnings("unchecked")
List<BestPractices> bestPracticesList = fullTextQuery.list();
fullTextSession.close();
return bestPracticesList;
}
private static void displayBestPracticesTableData() {
Session session = null;
try {
session = HibernateUtil.getSession();
/* List<BestPractices> bps = session.createQuery("from BestPractices").list();
for ( BestPractices bp : bps ) {
System.out.println(bp.getSummary());
}*/
Query query = session.createQuery("from BestPractices").setMaxResults(25);
// .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List l = query.list();
Iterator i1 = l.iterator();
BestPractices bp = null;
while (i1.hasNext()) {
bp = (BestPractices)i1.next();
System.out.println(bp.getArticleNumber());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally{
if(session != null) {
session.close();
}
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("\n\n******Data stored in Best_Practices__KAV table******\n");
displayBestPracticesTableData();
// Create an initial Lucene index for the data already present in the database
doIndex();
Scanner scanner = new Scanner(System.in);
String consoleInput = null;
while (true) {
// Prompt the user to enter query string
System.out.print("\n\nEnter search key (To exit type 'X')");
consoleInput = scanner.nextLine();
if("X".equalsIgnoreCase(consoleInput)) {
System.out.println("End");
System.exit(0);
}
List<BestPractices> result = search(consoleInput);
System.out.println("\n\n>>>>>>Record found for '" + consoleInput + "'");
for (BestPractices bestPractices : result) {
System.out.println(bestPractices);
}
}
}
}
Output is as follows:
******Data stored in Best_Practices__KAV table******
13:11:47,102 INFO Version:37 - HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
13:11:47,119 INFO Version:41 - HHH000412: Hibernate Core {4.2.0.Final}
13:11:47,134 INFO Environment:239 - HHH000206: hibernate.properties not found
13:11:47,134 INFO Environment:342 - HHH000021: Bytecode provider name : javassist
13:11:47,197 INFO Configuration:1933 - HHH000043: Configuring from resource: /hibernate.cfg.xml
13:11:47,197 INFO Configuration:1952 - HHH000040: Configuration resource: /hibernate.cfg.xml
13:11:47,447 INFO Configuration:2074 - HHH000041: Configured SessionFactory: null
13:11:48,087 INFO DriverManagerConnectionProviderImpl:98 - HHH000402: Using Hibernate built-in connection pool (not for production use!)
13:11:48,291 INFO DriverManagerConnectionProviderImpl:134 - HHH000115: Hibernate connection pool size: 20
13:11:48,291 INFO DriverManagerConnectionProviderImpl:137 - HHH000006: Autocommit mode: false
13:11:48,291 INFO DriverManagerConnectionProviderImpl:151 - HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://138.42.135.63;databaseName=Salesforce Backups;]
13:11:48,291 INFO DriverManagerConnectionProviderImpl:156 - HHH000046: Connection properties: {user=sa, password=****}
13:11:49,213 INFO Dialect:128 - HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
13:11:49,307 INFO TransactionFactoryInitiator:68 - HHH000399: Using default transaction strategy (direct JDBC transactions)
13:11:49,448 INFO ASTQueryTranslatorFactory:48 - HHH000397: Using ASTQueryTranslatorFactory
13:11:49,558 INFO Version:39 - HSEARCH000034: Hibernate Search 4.2.0.Final
13:11:49,730 WARN ConfigContext:301 - HSEARCH000075: Configuration setting hibernate.search.lucene_version was not specified, using LUCENE_CURRENT.
13:11:50,667 DEBUG QueryTranslatorImpl:265 - parse() - HQL: from com.nimsoft.ca.search.BestPractices
13:11:50,700 DEBUG QueryTranslatorImpl:283 - --- HQL AST ---
\-[QUERY] Node: 'query'
\-[SELECT_FROM] Node: 'SELECT_FROM'
\-[FROM] Node: 'from'
\-[RANGE] Node: 'RANGE'
\-[DOT] Node: '.'
+-[DOT] Node: '.'
| +-[DOT] Node: '.'
| | +-[DOT] Node: '.'
| | | +-[IDENT] Node: 'com'
| | | \-[IDENT] Node: 'nimsoft'
| | \-[IDENT] Node: 'ca'
| \-[IDENT] Node: 'search'
\-[IDENT] Node: 'BestPractices'
13:11:50,701 DEBUG ErrorCounter:82 - throwQueryException() : no errors
13:11:50,926 DEBUG FromElement:157 - FromClause{level=1} : com.nimsoft.ca.search.BestPractices (<no alias>) -> bestpracti0_
13:11:50,926 DEBUG HqlSqlWalker:629 - processQuery() : ( SELECT ( FromClause{level=1} best_practices__kav bestpracti0_ ) )
13:11:50,926 DEBUG HqlSqlWalker:869 - Derived SELECT clause created.
13:11:50,942 DEBUG JoinProcessor:175 - Using FROM fragment [best_practices__kav bestpracti0_]
13:11:50,942 DEBUG QueryTranslatorImpl:252 - --- SQL AST ---
\-[SELECT] QueryNode: 'SELECT' querySpaces (best_practices__kav)
+-[SELECT_CLAUSE] SelectClause: '{derived select clause}'
| +-[SELECT_EXPR] SelectExpressionImpl: 'bestpracti0_.id as id1_0_' {FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=best_practices__kav,tableAlias=bestpracti0_,origin=null,columns={,className=com.nimsoft.ca.search.BestPractices}}}
| \-[SQL_TOKEN] SqlFragment: 'bestpracti0_.articleNumber as articleN2_0_, bestpracti0_.articleType as articleT3_0_, bestpracti0_.best_Practices__c as best4_0_, bestpracti0_.firstPublishedDate as firstPub5_0_, bestpracti0_.lastModifiedDate as lastModi6_0_, bestpracti0_.lastPublishedDate as lastPubl7_0_, bestpracti0_.summary as summary8_0_, bestpracti0_.title as title9_0_'
\-[FROM] FromClause: 'from' FromClause{level=1, fromElementCounter=1, fromElements=1, fromElementByClassAlias=[], fromElementByTableAlias=[bestpracti0_], fromElementsByPath=[], collectionJoinFromElementsByPath=[], impliedElements=[]}
\-[FROM_FRAGMENT] FromElement: 'best_practices__kav bestpracti0_' FromElement{explicit,not a collection join,not a fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=best_practices__kav,tableAlias=bestpracti0_,origin=null,columns={,className=com.nimsoft.ca.search.BestPractices}}
13:11:50,942 DEBUG ErrorCounter:82 - throwQueryException() : no errors
13:11:50,958 DEBUG QueryTranslatorImpl:235 - HQL: from com.nimsoft.ca.search.BestPractices
13:11:50,958 DEBUG QueryTranslatorImpl:236 - SQL: select bestpracti0_.id as id1_0_, bestpracti0_.articleNumber as articleN2_0_, bestpracti0_.articleType as articleT3_0_, bestpracti0_.best_Practices__c as best4_0_, bestpracti0_.firstPublishedDate as firstPub5_0_, bestpracti0_.lastModifiedDate as lastModi6_0_, bestpracti0_.lastPublishedDate as lastPubl7_0_, bestpracti0_.summary as summary8_0_, bestpracti0_.title as title9_0_ from best_practices__kav bestpracti0_
13:11:50,958 DEBUG ErrorCounter:82 - throwQueryException() : no errors
13:11:50,989 DEBUG SQL:104 - select top 25 bestpracti0_.id as id1_0_, bestpracti0_.articleNumber as articleN2_0_, bestpracti0_.articleType as articleT3_0_, bestpracti0_.best_Practices__c as best4_0_, bestpracti0_.firstPublishedDate as firstPub5_0_, bestpracti0_.lastModifiedDate as lastModi6_0_, bestpracti0_.lastPublishedDate as lastPubl7_0_, bestpracti0_.summary as summary8_0_, bestpracti0_.title as title9_0_ from best_practices__kav bestpracti0_
Hibernate: select top 25 bestpracti0_.id as id1_0_, bestpracti0_.articleNumber as articleN2_0_, bestpracti0_.articleType as articleT3_0_, bestpracti0_.best_Practices__c as best4_0_, bestpracti0_.firstPublishedDate as firstPub5_0_, bestpracti0_.lastModifiedDate as lastModi6_0_, bestpracti0_.lastPublishedDate as lastPubl7_0_, bestpracti0_.summary as summary8_0_, bestpracti0_.title as title9_0_ from best_practices__kav bestpracti0_
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
000003990
13:11:51,504 DEBUG SQL:104 - select count(*) as y0_ from best_practices__kav this_
Hibernate: select count(*) as y0_ from best_practices__kav this_
13:11:51,504 INFO SimpleIndexingProgressMonitor:84 - HSEARCH000027: Going to reindex 23 entities
13:11:51,520 DEBUG SQL:104 - select this_.id as y0_ from best_practices__kav this_
Hibernate: select this_.id as y0_ from best_practices__kav this_
13:11:51,536 DEBUG SQL:104 - select this_.id as id1_0_0_, this_.articleNumber as articleN2_0_0_, this_.articleType as articleT3_0_0_, this_.best_Practices__c as best4_0_0_, this_.firstPublishedDate as firstPub5_0_0_, this_.lastModifiedDate as lastModi6_0_0_, this_.lastPublishedDate as lastPubl7_0_0_, this_.summary as summary8_0_0_, this_.title as title9_0_0_ from best_practices__kav this_ where this_.id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select this_.id as id1_0_0_, this_.articleNumber as articleN2_0_0_, this_.articleType as articleT3_0_0_, this_.best_Practices__c as best4_0_0_, this_.firstPublishedDate as firstPub5_0_0_, this_.lastModifiedDate as lastModi6_0_0_, this_.lastPublishedDate as lastPubl7_0_0_, this_.summary as summary8_0_0_, this_.title as title9_0_0_ from best_practices__kav this_ where this_.id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
13:11:51,536 DEBUG SQL:104 - select this_.id as id1_0_0_, this_.articleNumber as articleN2_0_0_, this_.articleType as articleT3_0_0_, this_.best_Practices__c as best4_0_0_, this_.firstPublishedDate as firstPub5_0_0_, this_.lastModifiedDate as lastModi6_0_0_, this_.lastPublishedDate as lastPubl7_0_0_, this_.summary as summary8_0_0_, this_.title as title9_0_0_ from best_practices__kav this_ where this_.id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select this_.id as id1_0_0_, this_.articleNumber as articleN2_0_0_, this_.articleType as articleT3_0_0_, this_.best_Practices__c as best4_0_0_, this_.firstPublishedDate as firstPub5_0_0_, this_.lastModifiedDate as lastModi6_0_0_, this_.lastPublishedDate as lastPubl7_0_0_, this_.summary as summary8_0_0_, this_.title as title9_0_0_ from best_practices__kav this_ where this_.id in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
13:11:51,551 DEBUG SQL:104 - select this_.id as id1_0_0_, this_.articleNumber as articleN2_0_0_, this_.articleType as articleT3_0_0_, this_.best_Practices__c as best4_0_0_, this_.firstPublishedDate as firstPub5_0_0_, this_.lastModifiedDate as lastModi6_0_0_, this_.lastPublishedDate as lastPubl7_0_0_, this_.summary as summary8_0_0_, this_.title as title9_0_0_ from best_practices__kav this_ where this_.id in (?, ?, ?)
Hibernate: select this_.id as id1_0_0_, this_.articleNumber as articleN2_0_0_, this_.articleType as articleT3_0_0_, this_.best_Practices__c as best4_0_0_, this_.firstPublishedDate as firstPub5_0_0_, this_.lastModifiedDate as lastModi6_0_0_, this_.lastPublishedDate as lastPubl7_0_0_, this_.summary as summary8_0_0_, this_.title as title9_0_0_ from best_practices__kav this_ where this_.id in (?, ?, ?)
13:11:51,583 INFO SimpleIndexingProgressMonitor:88 - HSEARCH000028: Reindexed 23 entities
Enter search key (To exit type 'X')