Hibernate Search not indexing - java

I write Spring web-app using Hibernate Search 4.5.1. When I try search it returns emty list. I think that problem in indexing. Dir for indexes was created, but after entity saving files in dir are not changing.
This is my spring config file
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<context:component-scan base-package="project"/>
<context:annotation-config/>
<jpa:repositories base-package="project" transaction-manager-ref="hibernateTransactionManager"
entity-manager-factory-ref="entityManagerFactory"/>
<bean id="entityManagerFactory" name="managerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="project"/>
<property name="dataSource" ref="postgresDataSource"/>
<property name="validationMode" value="NONE"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL81Dialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
</property>
</bean>
<bean id="postgresDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/LabourExchange"/>
<property name="username" value="postgres"/>
<property name="password" value="123456"/>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="postgresDataSource"/>
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="postgresDataSource"/>
<property name="packagesToScan" value="project"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.postgres.dialect}</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">8</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">/var/lucene/indexes</prop>
</props>
</property>
</bean>
<!--security config-->
<security:http auto-config="true" use-expressions="true">
<security:intercept-url pattern="/pages/**" access="isAuthenticated()"/>
<security:form-login
login-page="/auth/login.jsf"
default-target-url="/pages/home.jsf"
authentication-failure-url="/auth/login.jsf?status=error"/>
<security:logout logout-success-url="/hello.jsf"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider user-service-ref="UserDetailsService">
</security:authentication-provider>
</security:authentication-manager>
</beans>
My entity file
#Entity
#Table(name = "resumes")
#Indexed
public class Resume {
#Id #Column(name = "id") #GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "title")
#Field(index= Index.YES, analyze= Analyze.YES, store= Store.NO)
#NotBlank #Size(min = 10, max = 100)
private String title;
#Column(name = "text")
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#NotBlank #Size(min = 50, max = 10000)
private String text;
#Column(name = "salary")
#NotNull
private double salary;
#ManyToOne(optional = false)
#JoinColumn(name = "creator_id")
private User creator;
public User getCreator() {
return creator;
}
public void setCreator(User creator) {
this.creator = creator;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}

I solved the problem by moving
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">/var/lucene/indexes</prop>
to jpaProperties tag

Related

problem while creating table from maven spring mvc

I am not able to auto-create table from maven spring mvc.
I was doing a spring mvc project using maven. By far i have got no errors but when i am trying to create table in my database using applicationconfig.xml it is not working. i have searched over the internet but my applicationconfig.xml seems fine to me. I have got no errors. Still the table is not created..
applicationconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.milan.entities" /><!--scans model/entity/domain(name 3 but same) and registers-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/inventorymanagement" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager" />
</bean>
<tx:annotation-driven />
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>
User Class
package com.milan.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class User implements Serializable{
#Id
#Column(name = "USER_ID")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long userId;
#Column(name = "USERNAME")
private String userName;
#Column(name = "PASSWORD")
private String password;
#Column(name = "IS_ENABLEd")
private boolean isEnabled;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isIsEnabled() {
return isEnabled;
}
public void setIsEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
}
There is no error while running the program however the table is not created automatically.
Put #Entity on your User class, if the table still not created try putting below annotations on the class:
#Entity
#Table(name = "`user`")
Could be happening because User is a reserve word in DB.

Caused by: org.hibernate.MappingException: Unable to load class declared as <mapping class="xxxxx"/> in the configuration:

In my spring web application I am getting this error on launching my spring application
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/spring/data.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/data.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as <mapping class="reshetyk.alexey.diary.domain.DiaryUser"/> in the configuration:
Also I have this as a second error
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/data.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Unable to load class declared as <mapping class="reshetyk.alexey.diary.domain.DiaryUser"/> in the configuration:
The class DiaryUser is defined in the package and have the following properties assuming the getters and setters are well defined
this is the complete code of my entity class for DiaryUser class.java
#Entity
#Table(name = "USERS")
public class DiaryUser implements Serializable {
#Id
#Column(name = "ID_USER")
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#Column(name = "LOGIN", unique = true, nullable = false)
private String login;
#Column(name = "PASSWORD", nullable = false)
private String password;
public DiaryUser() {
}
public DiaryUser(Integer id) {
this.id = id;
}
public DiaryUser(String login) {
this.login = login;
}
public DiaryUser(Integer id, String login, String password) {
this.id = id;
this.login = login;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
I have equally defined this in my hibernate.cfg.xml file
<hibernate-configuration>
<session-factory>
<mapping class="reshetyk.alexey.diary.domain.DiaryUser" />
<mapping class="reshetyk.alexey.diary.domain.DiaryCategory" />
<mapping class="reshetyk.alexey.diary.domain.DiaryRecord" />
</session-factory>
</hibernate-configuration>
this is my data.xml file that contains my database definition configuration for hibernate mappings
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
I am kind of confused as I dont know why I am getting this error?
Instead of using LocalSessionFactoryBean,try using the AnnotationSessionFactoryBean instead and specify the mapping classes as shown below(check the link:AnnotationSessionFactoryBean)
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>test.package.Foo</value>
<value>test.package.Bar</value>
</list>
</property>
</bean>
or
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="test.package"/>
</bean>
instead of defining the mappings under hibernate.cfg.xml.
Your sessionFactory details in data.xml file can be modified as below:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--<property name="configLocation" value="classpath:hibernate.cfg.xml"/>-->
<property name="annotatedClasses">
<list>
<value>reshetyk.alexey.diary.domain.DiaryUser</value>
<value>reshetyk.alexey.diary.domain.DiaryCategory</value>
<value>reshetyk.alexey.diary.domain.DiaryRecord</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.connection.charSet">UTF-8</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Please make sure that mapping resource is correctly defined.

Why Hibernate in showing two different Queries in the console for single Query?`

I am new to this Hibernate framework and using Hibernate 3 just to start using this framework.
I have a small module that is executing a update query.
public void saveProduct(Product prod) {
String hql = "UPDATE Product set description = :description, price = :price,ctr=ctr+1 WHERE id = :id";
Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
query.setParameter("description", prod.getDescription());
query.setParameter("price", prod.getPrice());
query.setParameter("id", prod.getId());
logger.info(prod.toString());
int result = query.executeUpdate();
logger.info("Rows affected: " + result);
}
These are logs for this particular module and i have issue in this part hibernate is showing two different update queries one without column ctr and other one with column ctr or is it a normal behaviour:
May 23, 2016 5:55:37 PM com.mogae.dashboard.db.dao.ProductDaoImp saveProduct
INFO: Description: test311;Price: 1.7279999999999998
Hibernate:
update
products
set
description=?,
price=?
where
id=?
Hibernate:
update
products
set
description=?,
price=?,
ctr=ctr+1
where
id=?
May 23, 2016 5:55:37 PM com.mogae.dashboard.db.dao.ProductDaoImp saveProduct
INFO: Rows affected: 1
This is my xml configuration for DB connectivity:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- the parent application context definition for the springapp application -->
<aop:config>
<aop:advisor pointcut="execution(* *..ProductManager.*(..))" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<bean id="productDao" class="com.mogae.dashboard.db.dao.ProductDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.password}"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
</beans>
and this is my hibernate mapping file :
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mogae.dashboard.actions.domain.Product" table="products" lazy="false">
<id name="id" column="id" type="int">
<generator class="native">
<param name="sequence">products_id_seq</param>
</generator>
</id>
<property name="description" column="description" type="string" />
<property name="price" column="price" type="double" />
</class>
</hibernate-mapping>
and this is my entity (Product) class:
package com.mogae.dashboard.actions.domain;
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("Description: " + description + ";");
buffer.append("Price: " + price);
return buffer.toString();
}
private String description;
private Double price;
private int id;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Please help i have been searching for this issue on the google and various hibernate forums but haven't found one.

Spring Data JPA query doesn't work, column does not exists

I use spring data jpa in my web-app, i have entity user
#Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String password;
private String email;
private Boolean enabled;
private String name;
private String lastname;
private String userRole;
public User() {
}
public User(String password, String email, Boolean enabled, String name, String lastname, String userRole) {
this.password = password;
this.email = email;
this.enabled = enabled;
this.name = name;
this.lastname = lastname;
this.userRole = userRole;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO, generator = "users_id_seq")
#SequenceGenerator(name="users_id_seq", sequenceName="users_id_seq", allocationSize = 1)
#Column(name = "id", nullable = false)
public Long getId() {
return id;
}
//Other columns
}
And i have UserRepository interface which extends CrudRepository.
When i call method findAll in my Controller, I get this error
01-May-2016 22:45:58.674 WARN [http-nio-8080-exec-3] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: 0, SQLState: 42703
01-May-2016 22:45:58.675 ERROR [http-nio-8080-exec-3] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions Error: column user0_.id does not exist
Position: 8
My spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="com.birthright.repository"/>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.birthright.entity"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/AutoService"/>
<property name="username" value="postgres"/>
<property name="password" value="root"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf"/>
</bean>
</beans>
My table in postgresql
CREATE TABLE public."user"
(
id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
password character varying,
email character varying,
enabled boolean,
name character varying,
lastname character varying,
user_role character varying,
CONSTRAINT users_pkey PRIMARY KEY (id)
)
User is a reserved keyword in PostgreSQL. With a default naming strategy you, probably, had User table name.
Don't know why #Table(name = "user", schema = "public") works. Maybe PostgreSQL doesn't consider public.user as a keyword opposite User.
Please use plural names for tables. And using a system or subsystem prefix for a table name (xxx_users) is a good idea too.
A naming strategy can be used for such approach. Refer this as an example: Hibernate5NamingStrategy
An example of prefixes:
StrategyOptions

Spring-JPA "Class is not entity"

I have an issue with my project using Spring (4.2.4 Release) and JPA (2.1)
To be brief the problem is in the part of code in file "Book.java":
#Table(name = "book")
#NamedQueries({
#NamedQuery(name = "Book.getBooks", query="select b from com.jpaProSpring.Book b ")
})
#Entity(name = "Book")
public class Book implements Serializable {
private int id;
private String title;
private String description;
public Book() {
}
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id")
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
#Column
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
#Column
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
}
and particulary in
query = "select b from com.jpaProSpring.Book b" )
My IntelliJ highlights the Book and says that class is not en entity. And I have no idea, why it is so given that I was doing an example from the book.
Link to my project https://github.com/yuraguz/LearnORM.git
My spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3310/testdb"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.jpaProSpring" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<context:annotation-config />
<context:component-scan base-package="com.jpaProSpring" />
</beans>
P.S: I know entities must be declared in persistence.xml in META-INF/ source. But if I understand correctly Spring4 make it possible to config project without persistance.xml. So, I don't use it.
If you use InellyJ Idea, possible you didn't add JPA facet in your project. Try open menu "File"->"Project Structure", then select in list of Project Settings section named "Facets". Check if you have configured JPA facet in list of facets. If you don't see JPA facet, just add it (by pressing "+" button). Optionally, you can specify persistence.xml (if you have it) and Default JPA provider. Hope this will help.

Categories