Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. Exception in thread "main" java.lang.IllegalArgumentException: java.lang.Object is not an indexed entity or a subclass of an indexed entity
  2.  
  3.  
  4. I have referred the link : http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/
  5.  
  6. package netgloo;
  7.  
  8. import org.springframework.boot.SpringApplication;
  9. import org.springframework.boot.autoconfigure.SpringBootApplication;
  10.  
  11. @SpringBootApplication
  12. public class Application {
  13.  
  14. public static void main(String[] args) {
  15. SpringApplication.run(Application.class, args);
  16. }
  17.  
  18. }
  19.  
  20. package netgloo;
  21.  
  22. import javax.persistence.EntityManager;
  23. import javax.persistence.PersistenceContext;
  24.  
  25. import org.hibernate.search.jpa.FullTextEntityManager;
  26. import org.hibernate.search.jpa.Search;
  27. import org.springframework.context.ApplicationListener;
  28. import org.springframework.context.event.ContextRefreshedEvent;
  29. import org.springframework.stereotype.Component;
  30.  
  31. /**
  32. * The only meaning for this class is to build the Lucene index at application
  33. * startup. This is needed in this example because the database is filled
  34. * before and each time the web application is started. In a normal web
  35. * application probably you don't need to do this.
  36.  
  37. */
  38. @Component
  39. public class BuildSearchIndex implements ApplicationListener<ContextRefreshedEvent> {
  40.  
  41. @PersistenceContext
  42. private EntityManager entityManager;
  43.  
  44. /**
  45. * Create an initial Lucene index for the data already present in the
  46. * database.
  47. * This method is called during Spring's startup.
  48. *
  49. * @param event Event raised when an ApplicationContext gets initialized or
  50. * refreshed.
  51. */
  52. @Override
  53. public void onApplicationEvent(final ContextRefreshedEvent event) {
  54. try {
  55. FullTextEntityManager fullTextEntityManager =
  56. Search.getFullTextEntityManager(entityManager);
  57. fullTextEntityManager.createIndexer().startAndWait();
  58. }
  59. catch (InterruptedException e) {
  60. System.out.println(
  61. "An error occurred trying to build the serach index: " +
  62. e.toString());
  63. }
  64. return;
  65. }
  66.  
  67.  
  68. }
  69.  
  70. package netgloo.controllers;
  71.  
  72. import java.util.List;
  73.  
  74. import netgloo.models.Employee;
  75. import netgloo.search.EmployeeSearch;
  76.  
  77.  
  78. import org.springframework.beans.factory.annotation.Autowired;
  79. import org.springframework.stereotype.Controller;
  80. import org.springframework.ui.Model;
  81. import org.springframework.web.bind.annotation.RequestMapping;
  82. import org.springframework.web.bind.annotation.ResponseBody;
  83.  
  84. @Controller
  85. public class MainController {
  86. @Autowired
  87. private EmployeeSearch emprSearch;
  88.  
  89. @RequestMapping("/")
  90. @ResponseBody
  91. public String index() {
  92. return "Welcome !!!:)";
  93. }
  94.  
  95. @RequestMapping("/search")
  96. public String search(String query, Model model) {
  97. List<Employee> searchResults = null;
  98. try {
  99. searchResults = emprSearch.search(query);
  100. } catch (Exception ex) {
  101. return "";
  102. }
  103. model.addAttribute("searchResults", searchResults);
  104. return "search";
  105. }
  106. }
  107.  
  108. package netgloo.models;
  109.  
  110. import javax.persistence.Entity;
  111. import javax.persistence.GeneratedValue;
  112. import javax.persistence.GenerationType;
  113. import javax.persistence.Id;
  114. import javax.persistence.Table;
  115. import javax.validation.constraints.NotNull;
  116.  
  117. @Entity
  118. @Table(name = "employees")
  119. public class Employee {
  120.  
  121. // An autogenerated id (unique for each user in the db)
  122. @Id
  123. @GeneratedValue(strategy = GenerationType.AUTO)
  124. private long id;
  125.  
  126. @NotNull
  127. private String email;
  128.  
  129. @NotNull
  130. private String name;
  131.  
  132. // Public methods
  133.  
  134. public Employee() { }
  135.  
  136. public Employee(long id) {
  137. this.id = id;
  138. }
  139.  
  140. public Employee(String email, String name) {
  141. this.email = email;
  142. this.name = name;
  143. }
  144.  
  145. // Getter and setter methods
  146. // ...
  147.  
  148. }
  149.  
  150. package netgloo.search;
  151.  
  152. import java.util.List;
  153.  
  154. import javax.persistence.EntityManager;
  155. import javax.persistence.PersistenceContext;
  156. import javax.transaction.Transactional;
  157.  
  158. import netgloo.models.Employee;
  159.  
  160. import org.hibernate.search.jpa.FullTextEntityManager;
  161. import org.hibernate.search.query.dsl.QueryBuilder;
  162. import org.springframework.stereotype.Repository;
  163.  
  164. @Repository
  165. @Transactional
  166. public class EmployeeSearch {
  167.  
  168. // Spring will inject here the entity manager object
  169. @PersistenceContext
  170. private EntityManager entityManager;
  171.  
  172. public List<Employee> search(String text) {
  173.  
  174. // get the full text entity manager
  175. FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
  176. .getFullTextEntityManager(entityManager);
  177.  
  178. // create the query using Hibernate Search query
  179. QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Employee.class)
  180. .get();
  181.  
  182. // a very basic query by keywords
  183. org.apache.lucene.search.Query query = queryBuilder.keyword().onFields("name", "city", "email").matching(text)
  184. .createQuery();
  185.  
  186. // wrap Lucene query in an Hibernate Query object
  187. org.hibernate.search.jpa.FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(query, Employee.class);
  188.  
  189. // execute search and return results (sorted by relevance as default)
  190. @SuppressWarnings("unchecked")
  191. List<Employee> results = jpaQuery.getResultList();
  192.  
  193. return results;
  194. }
  195.  
  196. }
  197.  
  198. <!DOCTYPE HTML>
  199. <html xmlns:th="http://www.thymeleaf.org">
  200.  
  201. <head>
  202. <title>Search</title>
  203. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  204. </head>
  205.  
  206. <body>
  207.  
  208. <ol>
  209. <li th:each="employee : ${searchResults}">
  210. <b><span th:text="${employee.username}"></span></b> -
  211. <span th:text="${employee.city}"></span> -
  212. <span th:text="${employee.email}"></span>
  213. </li>
  214. </ol>
  215.  
  216. <p th:if="${searchResults.isEmpty()}">
  217. <strong>Hint</strong>: the query "<a href='/search?query=amy'>amy</a>"
  218. should return some results.
  219. </p>
  220.  
  221. </body>
  222.  
  223. </html>
  224.  
  225. # Datasource settings
  226. spring.datasource.driverClassName=com.mysql.jdbc.Driver
  227. spring.datasource.url=jdbc:mysql://localhost:3306/myschema
  228. spring.datasource.username=root
  229. spring.datasource.password=admin123
  230.  
  231. # Hibernate settings
  232. spring.jpa.hibernate.ddl-auto=create
  233. spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
  234. spring.jpa.hibernate.naming_strategy =org.hibernate.cfg.ImprovedNamingStrategy
  235. spring.jpa.show-sql = true
  236.  
  237. # Specify the DirectoryProvider to use (the Lucene Directory)
  238. spring.jpa.properties.hibernate.search.default.directory_provider = filesystem
  239.  
  240. # Using the filesystem DirectoryProvider you also have to specify the default
  241. # base directory for all indexes (make sure that the application have write
  242. # permissions on such directory)
  243. spring.jpa.properties.hibernate.search.default.indexBase = c:\indexeFiles
  244.  
  245.  
  246. # ===============================
  247. # = THYMELEAF
  248. # ===============================
  249.  
  250. spring.thymeleaf.cache = false
  251. server.port=9001
  252.  
  253. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  254. <modelVersion>4.0.0</modelVersion>
  255. <groupId>netgloo</groupId>
  256. <artifactId>spring-boot-hibernate-search</artifactId>
  257. <version>0.0.1-SNAPSHOT</version>
  258.  
  259. <name>spring-boot-hibernate-search</name>
  260. <description>How to integrate Hibernate Search in Spring Boot</description>
  261.  
  262. <parent>
  263. <groupId>org.springframework.boot</groupId>
  264. <artifactId>spring-boot-starter-parent</artifactId>
  265. <version>1.2.3.RELEASE</version>
  266. <relativePath />
  267. </parent>
  268.  
  269. <dependencies>
  270. <dependency>
  271. <groupId>org.springframework.boot</groupId>
  272. <artifactId>spring-boot-starter-web</artifactId>
  273. </dependency>
  274. <dependency>
  275. <groupId>org.springframework.boot</groupId>
  276. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  277. </dependency>
  278. <dependency>
  279. <groupId>org.springframework.boot</groupId>
  280. <artifactId>spring-boot-starter-data-jpa</artifactId>
  281. </dependency>
  282. <dependency>
  283. <groupId>mysql</groupId>
  284. <artifactId>mysql-connector-java</artifactId>
  285. </dependency>
  286.  
  287. <!-- Maven artifact identifier for Hibernate Search -->
  288. <dependency>
  289. <groupId>org.hibernate</groupId>
  290. <artifactId>hibernate-search-orm</artifactId>
  291. <version>4.5.1.Final</version>
  292. </dependency>
  293.  
  294. <!-- Optional: to use JPA 2.1 -->
  295. <dependency>
  296. <groupId>org.hibernate</groupId>
  297. <artifactId>hibernate-entitymanager</artifactId>
  298. </dependency>
  299.  
  300. </dependencies>
  301.  
  302. <properties>
  303. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  304. <start-class>netgloo.Application</start-class>
  305. <java.version>1.7</java.version>
  306. </properties>
  307.  
  308. <build>
  309. <plugins>
  310. <plugin>
  311. <groupId>org.springframework.boot</groupId>
  312. <artifactId>spring-boot-maven-plugin</artifactId>
  313. </plugin>
  314. </plugins>
  315. </build>
  316.  
  317. </project>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement