我正在尝试为 ElasticSearch 集成配置 HibernateSearch。
我的 Oracle 数据库中有 Product
表。我尝试从该表中根据产品名称进行搜索。
为此,我尝试将 HibernateSearch (ElasticSearch) 与 Oracle 数据库集成。
我从 HibernateSearch 收到以下错误:
我使用 Oracle 数据库并在我的 pom.xml 文件中添加了所需的依赖项
输入搜索键(要退出,请输入“X”)
JT3312
Exception in thread "main" org.hibernate.search.exception.SearchException: HSEARCH000331: Can't build query for type 'com.test.webservice.model.Product' which is neither configured nor has any configured sub-types.
at org.hibernate.search.query.dsl.impl.ConnectedQueryContextBuilder$HSearchEntityContext.<init>(ConnectedQueryContextBuilder.java:60)
at org.hibernate.search.query.dsl.impl.ConnectedQueryContextBuilder.forEntity(ConnectedQueryContextBuilder.java:42)
at com.test.webservice.elasticsearch.App.search(App.java:74)
at com.test.webservice.elasticsearch.App.main(App.java:153)
正在使用所有最新的依赖项。
hibernate-search-orm ->5.9.1.Final
hibernate-core ->5.2.16.Final`
ojdbc14 -> 10.2.0.4.0
应用程序.java
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<Product> search(String queryString) {
Session session = HibernateUtil.getSession();
FullTextSession fullTextSession = Search.getFullTextSession(session);
QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Product.class).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("name").matching(queryString).createQuery();
// wrap Lucene query in a javax.persistence.Query
org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, Product.class);
List<Product> productList = fullTextQuery.list();
fullTextSession.close();
return productList;
}
private static void displayContactTableData() {
Session session = null;
PropertiesFile propertiesFile= PropertiesFile.getInstance();
String driverClass = propertiesFile.extractPropertiesFile().getProperty("hibernate.connection.driver_class");
String connectionURL = propertiesFile.extractPropertiesFile().getProperty("hibernate.connection.url");
String userName = propertiesFile.extractPropertiesFile().getProperty("hibernate.connection.username");
String password = propertiesFile.extractPropertiesFile().getProperty("hibernate.connection.password");
String dialect = propertiesFile.extractPropertiesFile().getProperty("hibernate.dialect");
String showSQL = propertiesFile.extractPropertiesFile().getProperty("hibernate.show_sql");
try {
//session = HibernateUtil.getSession();
// Fetching saved data
String hql = "from Product";
@SuppressWarnings("unchecked")
Configuration cfg=new Configuration()
.setProperty("hibernate.connection.driver_class", driverClass)
.setProperty("hibernate.connection.url", connectionURL)
.setProperty("hibernate.connection.username", userName)
.setProperty("hibernate.connection.password", password)
.setProperty("hibernate.dialect", dialect)
.setProperty("hibernate.show_sql", showSQL)
.addAnnotatedClass(com.test.webservice.model.Product.class);
SessionFactory factory=cfg.buildSessionFactory();
session=factory.openSession();
Transaction t=session.beginTransaction();
List<Product> productList = session.createQuery(hql).list();
for (Product product : productList) {
System.out.println("Product Name --->"+product.getName());
}
} catch(HibernateException exception){
System.out.println("Problem creating session factory");
exception.printStackTrace();
}finally{
if(session != null) {
session.close();
}
}
}
public static void main(String[] args) throws InterruptedException {
System.out.println("\n\n******Data stored in Contact table******\n");
displayContactTableData();
// Create an initial Lucene index for the data already present in the database
doIndex(); // Error occuring on this line
Scanner scanner = new Scanner(System.in);
String consoleInput = null;
while (true) {
// Prompt the user to enter query string
System.out.println("\n\nEnter search key (To exit type 'X')");
System.out.println();
consoleInput = scanner.nextLine();
if("X".equalsIgnoreCase(consoleInput)) {
System.out.println("End");
System.exit(0);
}
List<Product> result = search(consoleInput);
System.out.println("\n\n>>>>>>Record found for '" + consoleInput + "'");
for (Product product : result) {
System.out.println(product);
}
}
}
}
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">vb</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.search.default.directory_provider">filesystem</property>
<property name="hibernate.search.default.indexBase">C:\lucene\indexes</property>
<mapping class="com.test.webservice.model.Product" />
</session-factory>
</hibernate-configuration>
产品.java
@Entity
@Indexed
@Table(name = "PRODUCT")
public class Product {
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private String name;
@Id
@GeneratedValue
private long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setId(long id) {
this.id = id;
}
@Id
public long getId() {
return id;
}
}
最佳答案
正如我answered to your other question ,问题很可能在于您如何在 HibernateUtil
中引导 Hibernate。您很可能忘记调用 addAnnotatedClass
。
问问自己为什么必须在 displayContactTableData
中注释此行:
//session = HibernateUtil.getSession();
您的 HibernateUtil
中显然有问题,因此请修复它,而不是在某些地方避免它,而在其他地方则避免它。
关于java - 无法为类型 'x.y.z.package' 构建查询,该类型既未配置,也没有任何配置的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50099373/