Hibernate Search Won't return correct results - hibernate-annotations

I built an example project for hibernate search and it works fine without any exception but when I search a string that I have some objects with that string, it returns empty. I don't know what should I do!!! Can somebody help me...
Project is maven based and I use hibernate annotation both for hibernate and hibernate search.
Here is my hibernate.cfg.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/eprnews
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">***</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<!--
<property name="hibernate.hbm2ddl.auto">create-drop</property>
Hibernate Search -->
<!-- org.hibernate.search.store.FSDirectoryProvider -->
<!-- org.hibernate.search.store.RAMDirectoryProvider for test -->
<!--
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property>
-->
<property name="hibernate.search.default.directory_provider">filesystem</property>
<property name="hibernate.search.default.indexBase">./indexes</property>
<!--
-->
<property name="hibernate.search.worker.execution">async</property>
<property name="hibernate.search.default.optimizer.transaction_limit.max">100</property>
<!-- Mapped classes -->
<mapping class="net.leemoo.test.entity.NewsEntity"/>
</session-factory>
</hibernate-configuration>
and here is my entity:
#Entity
#Table(name="news")
#Indexed
#AnalyzerDef(
name = "PersianAnal",
charFilters = #CharFilterDef(factory = PersianCharFilterFactory.class),
tokenizer = #TokenizerDef(factory = StandardTokenizerFactory.class),
filters={
#TokenFilterDef(factory = ArabicNormalizationFilterFactory.class),
#TokenFilterDef(factory = PersianNormalizationFilterFactory.class)
}
)
public class NewsEntity {
#Id
#GeneratedValue
private Long id;
#Field
#Analyzer(definition="PersianAnal")
private String title;
#Field
#Analyzer(definition="PersianAnal")
private String newsAbstract;
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#Analyzer(definition="PersianAnal")
private String content;
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#DateBridge(resolution=Resolution.DAY)
private Date creationDate;
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#DateBridge(resolution=Resolution.DAY)
private Date modifiedDate;
private Integer viewsCounter;
#Field
#Analyzer(definition="PersianAnal")
private String keywords;
private Boolean jqueryNews;
private Boolean photoNews;
and setters and getters...
and this is the code that i use for searching:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
FullTextSession fullTextSession = Search.getFullTextSession(session);
try {
// fullTextSession.createIndexer().startAndWait();
QueryBuilder gb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(NewsEntity.class).get();
Query query = gb.keyword().
onFields("title", "newsAbstract", "content").
matching("test").createQuery();
org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, NewsEntity.class);
List<NewsEntity> result = (List<NewsEntity>)hibQuery.list();
System.out.println(result.size());
} catch (Exception e) {
// e.printStackTrace();
}
session.getTransaction().commit();
session.close();
What should I do? Please help me....

I think I made a silly mistake in that I commented my index creator in last code phrase. I should run it once in the beginning for indexes to be created. I'm sorry again that I took your time.

Related

Transactions not persisting - Spring MVC with Hibernate

I have a spring MVC (4.1.6) app, using Hibernate ORM (5.3.7) on top of a MySQL 8 Database.
I have a view with a form that should create a row in the DB when it is submitted, but it does not. I can tell the information gets to my #Service bean and DAO #Repository bean. I can also successfully query data from the DB and display it on a view, if I update the DB directly through SQL.
I have read a number of posts and tried to reconfigure my application context and servlet context XMLs with no luck. Can anyone tell me where I am going wrong?
Below are some snippets.
Web_XML
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/applicationContext-dao.xml
/WEB-INF/spring/applicationContext-security.xml
</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
applicationContext-web.xml
<context:component-scan base-package="edu.neu.snowfinder">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<interceptors>
<beans:bean class= "org.springframework.orm.hibernate4.support.OpenSessionInViewInterceptor">
<beans:property name="sessionFactory" ref="sessionFactory"/>
</beans:bean>
</interceptors>
applicationContext-dao.xml
<bean id="sessionFactory" class = "org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name = "dataSource" ref = "dataSource"/>
<property name = "configLocation">
<value> classpath:hibernate.cfg.xml</value>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id = "transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref = "sessionFactory"/>
<property name="dataSource" ref = "dataSource"/>
</bean>
UserServiceImpl
#Service
public class UserServiceImpl implements UserService {
#Autowired
#Qualifier("userHibernateDAO")
private UserDAO userDAO;
#Override
#Transactional
public void createUser(User user) {
System.out.println("Does the Sevice have the user?"+user.getFirstname());
userDAO.createUser(user);
}
#Override
#Transactional
public Collection<User> listUsers() {
return userDAO.listUsers();
}
}
UserHibernateDAO
#Repository
public class UserHibernateDAO implements UserDAO {
#Autowired
private SessionFactory sessionFactory;
#Override
public void createUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
System.out.println("Does the DAO have the user?"+user.getFirstname());
System.out.println("Does the DAO have a session?"+sessionFactory.getCurrentSession().toString());
Serializable id = sessionFactory.getCurrentSession().save(user);
session.getTransaction().commit();
System.out.println("Whats the saved User's ID??"+id.toString());
}
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name = "hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name = "hibernate.showsql">true</property>
<property name = "hibernate.formatsql">true</property>
<property name = "hibernate.hbm2ddl.auto">create</property>
<mapping class = "edu.neu.snowfinder.model.User"/>
</session-factory>
</hibernate-configuration>

Retrieve data from spring doesn't work after insert

I have a problem when I insert data in my MySQL database with Spring Data Jpa Module.
The problem is the following:
When I startup my Springapplication I create the database and initialize the data with SpringdataJPA and I see the data inserted in the table. Afterwards i wanna insert some data related to inserted data and only 1 of the 3 records inserted before were found. When i restard the server and i dont recreate the data and search for it with the same method every single record is found. I search for 2 weeks for a solution but didnt find anything. I hope you can help me.
NEWS
I think only in words is hard to understand, so I insert the code behind. jpa.xml with all spring jpa configuration. The 2 entities and the initmethods. hopefully you can find my error.
jpa.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="classpath:properties/database/database.properties" />
<!-- Declare a datasource that has pooling capabilities-->
<!-- BoneCP configuration -->
<bean id="SoopDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close" >
<property name="driverClass" value="${database.driver}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="username" value="${database.user}"/>
<property name="password" value="${database.password}"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="idleMaxAge" value="240"/>
<property name="maxConnectionsPerPartition" value="30"/>
<property name="minConnectionsPerPartition" value="5"/>
<property name="partitionCount" value="2"/>
<property name="acquireIncrement" value="5"/>
<!-- <property name="statementsCacheSize" value="100"/> -->
<!-- <property name="releaseHelperThreads" value="2"/> -->
</bean>
<!-- Declare a JPA entityManagerFactory-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="persistenceUnitName" value="SoopDbProvider" />
<property name="dataSource" ref="SoopDataSource"/>
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.soopproject.main.db</value>
</list>
</property>
</bean>
<!-- Db initialization bean -->
<bean id="databaseinit" class="com.soopproject.main.db.init.DatabaseInitialization" init-method="init"/>
<!-- Declare a transaction manager-->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Entities:
BaseEntityWithPrimary
#MappedSuperclass
#Data
#EqualsAndHashCode(callSuper = false)
public class SoopBaseEntityWithPrimary implements
Serializable {
#Id
#GeneratedValue
private long id;
}
Language:
#Entity
#Data
#EqualsAndHashCode(callSuper = true)
public class Language extends BaseEntityWithPrimary {
#Column(nullable = false, unique = true)
private String shortname;
#Column(nullable = false)
private String longname;
#ManyToMany(cascade = CascadeType.ALL)
private List<TranslationsShort> languageTranslationsShorts;
}
InitMethods
#Autowired
private UserRepository userrep;
#Autowired
private LanguageRepository langrep;
#Autowired
private ErrorCodesRepository errorrep;
private void initLanguage() {
User systemuser = null;
try {
systemuser = userrep.findUserByUsername("system");
} catch (Exception ex) {
DatabaseException exception = new DatabaseReadException(ex);
exception.writeLog();
}
List<Language> languages = new LinkedList<>();
Language deutsch = new Language();
deutsch.setCreateUser(systemuser);
deutsch.setShortname("DE");
deutsch.setLongname("Deutsch");
Language english = new Language();
english.setCreateUser(systemuser);
english.setShortname("EN");
english.setLongname("English");
Language italiano = new Language();
italiano.setCreateUser(systemuser);
italiano.setShortname("IT");
italiano.setLongname("Italiano");
languages.add(deutsch);
languages.add(italiano);
languages.add(english);
for (Language lang : languages) {
Language l_help = null;
try {
l_help = langrep.findLanguageByShortname(lang.getShortname());
} catch (Exception ex) {
DatabaseException exception = new DatabaseReadException(ex);
exception.writeLog();
}
if (l_help == null)
try {
langrep.saveAndFlush(lang);
} catch (Exception e) {
DatabaseException exception = new DatabaseWriteException(e);
exception.writeLog();
}
}
}
private void initErrorCodes() {
User systemuser = null;
Language de = null;
Language it = null;
Language en = null;
try {
systemuser = userrep.findUserByUsername("system");
} catch (Exception ex) {
DatabaseException exception = new DatabaseReadException(ex);
exception.writeLog();
}
try {
de = langrep.findLanguageByShortname("DE");
} catch (Exception ex) {
DatabaseException exception = new DatabaseReadException(ex);
exception.writeLog();
}
try {
it = langrep.findLanguageByShortname("IT");
} catch (Exception ex) {
DatabaseException exception = new DatabaseReadException(ex);
exception.writeLog();
}
try {
en = langrep.findLanguageByShortname("EN");
} catch (Exception ex) {
DatabaseException exception = new DatabaseReadException(ex);
exception.writeLog();
}
The problem is that after startup the springapplication and set <prop key="hibernate.hbm2ddl.auto">create</prop> in the method initErrorCodes only the line with it = langrep.findLanguageByShortname("IT"); finds the data in database. The other 2 calls return null (de = langrep.findLanguageByShortname("DE"); and en = langrep.findLanguageByShortname("EN");). Then i stop the application and look to the database and all data is inserted in the table Language. When i restart the server with <prop key="hibernate.hbm2ddl.auto">none</prop> then all 3 method calls return the data???!?!?! I dont get it. So for sure it's not a problem of the method call. But I cant find the error.
What do you mean by 1 of 3 were found? Are you querying the data, or accessing a relationship?
It sounds like some sort of caching is occurring. Some JPA providers enable caching by default, check you cache settings, and ensure you are maintaining bidirectional relationships correctly.
https://en.wikibooks.org/wiki/Java_Persistence/Caching
https://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side

Custom ObjectMapper and NamingStrategy in Spring 3 MVC

I'm using Spring MVC 3 and MappingJacksonHttpMessageConverter in order to get the json data with #ResponseBody. With the default config works ok but now i need to transform the camelCase fields to Pascal casing. For this purpose, i've developed a custom naming strategy:
UpperCaseNamingStrategy.java
public class UpperCaseNamingStrategy extends PropertyNamingStrategy {
#Override
public String nameForField(MapperConfig config, AnnotatedField field, String defaultName){
return convert(defaultName);
}
#Override
public String nameForGetterMethod(MapperConfig config, AnnotatedMethod method, String defaultName){
return convert(defaultName);
}
#Override
public String nameForSetterMethod(MapperConfig config, AnnotatedMethod method, String defaultName){
return convert(defaultName);
}
public String convert(String defaultName){
char[] arr= defaultName.toCharArray();
if(arr.length != 0){
if(Character.isLowerCase(arr[0])){
arr[0] = Character.toUpperCase(arr[0]);
}
}
return new StringBuilder().append(arr).toString();
}
}
I set my custom strategy to the objectMapper and i set the objectMapper in the converter. These are the beans:
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper">
<property name="propertyNamingStrategy" ref="namingStrategy"/>
</bean>
<bean id="namingStrategy" class="es.unican.meteo.util.UpperCaseNamingStrategy"></bean>
The beans are registered properly because i can see it in the log but when i request the json data the behaviour is the same and the converter method is not called. Do I need more configs?
Following changes are suggested as compared to what I did in my project:
Change mapper bean class to "com.fasterxml.jackson.databind.ObjectMapper". I am using Spring 4.3
add #JsonProperty annotation to the property of class which is being serielized/deseralized
Create default constructors in class which is being serielized/deseralized
Best of Luck!

Spring 3.0 exception converting String to java.util.Date on POST

I'm hoping someone can help me since I have been banging my head against a wall for a couple of days on a issue which seems straightforward and which has been documented in other threads on the web.
I am using Smart GWT client (3.0) in conjunction with Spring 3.1 server and using JSON to communicate (with Jackson API 1.9).
The issue is that when I attempt to save a date from my SmartGWT client and it is sent to the server I get the following exception:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'comment' on field 'dateAdded': rejected value [2012-06-27T10:57:47+0100]; codes [typeMismatch.comment.dateAdded,typeMismatch.dateAdded,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [comment.dateAdded,dateAdded]; arguments []; default message [dateAdded]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'dateAdded'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type java.util.Date for value '2012-06-27T10:57:47+0100'; nested exception is java.lang.IllegalArgumentException]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:110)
I have seen this issue in a few other posts, but most relate to not having formatted the Date in the correct format, but I have tried various formats:
- yyyy-MM-dd
- yyyy-MM-dd'T'HH:mm:ssZ
- yyyyMMddHHmmssZ (as per suggestion here: http://code.google.com/p/usersapi/issues/detail?id=8)
So in my code I have done the following:
Configured a CustomObjectMapper:
`
public class CustomObjectMapper extends ObjectMapper {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
public CustomObjectMapper() {
super();
configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false);
setDateFormat(formatter);
getDeserializationConfig().setDateFormat(formatter);
}
}
`
Spring app context thusly:
`
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
<property name="supportedMediaTypes" value="application/json" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.jpmorgan.creditriskreporting.server" />
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
<bean id="jacksonObjectMapper" class="com.jpmorgan.creditriskreporting.server.util.CustomObjectMapper" />
<!-- Client -->
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<ref bean="marshallingConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
`
Bean object:
`
import java.util.Date;
#JsonAutoDetect
public class Comment {
private int id;
private String comment;
private Date dateAdded;
public Comment() {}
public Comment(int id) {
this.id = id;
}
...
//#JsonSerialize(using=JsonDateSerializer.class) -- I had previously tried to use these custom Date serializer class
public Date getDateAdded() {
return dateAdded;
}
//#JsonDeserialize(using=JsonDateDeserializer.class)
public void setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
}
`
EDIT:
Controller Class
This may be where the issue lies, since when I use #RequestBody it works from my Integration tests, however, my Abstract RestDataSource in SmartGWT only works with #ModelAttribute, so I'm not sure how to proceed.
#RequestMapping(value="/", method=RequestMethod.POST)
public #ResponseBody Comment createNewComment2(#ModelAttribute Comment comment) {
log.info("calling createComment with comment: {}", comment);
comment.setDateAdded(new Date());
Comment added = commentDao.create(comment);
log.info("created comment: {}", added);
return commentDao.get(comment);
}
So I can fetch data from the server and the date is displayed in SmartGWT fine. It's only when I do the add data that I get the issue. From Smart GWT Developer Console:
{
"dataSource":"CommentDS",
"operationType":"add",
"componentId":"isc_DynamicForm_1",
"data":{
"userAdded":"sharper",
"dateAdded":"2012-06-27T10:57:47+0100",
"comment":"sample"
},
"callback":{
"target":[DynamicForm ID:isc_DynamicForm_1],
"methodName":"saveEditorReply"
},
"showPrompt":true,
"prompt":"Saving form...",
"oldValues":{
},
"clientContext":{
},
"requestId":"CommentDS$6272"
}
Any help with this is hugely appreciated.
Cheers,
Steve
I found out the issue thanks to http://vkubushyn.wordpress.com/2011/05/31/smart-gwt-restful-spring-mvc
Had to use Spring's InitBinder
#InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
You should add DateFormat into your model.
#DateTimeFormat(pattern = "dd.MM.yyyy")
private Date beginDate;
#DateTimeFormat(pattern = "dd.MM.yyyy")
private Date endDate;
as a function parameter
void functionName** (#RequestParam("beginDate") #DateTimeFormat(pattern = "dd.MM.yyyy")Date beginDate, #RequestParam("endDate") #DateTimeFormat(pattern = "dd.MM.yyyy")Date endDate)
I might be wrong, but as far as I remember the Z stands for timezone in ISOwhoknowswhatformat. And that's 4 chars wide, so I would try this:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZ");
By the way: if this is the issue you should've catched it in your unit tests. You do have unit test for CustomObjectMapper don't you? :P

Hibernate list using cache? -Not updating entity attribute

I have an application that uses hibernate.
I did the following:
Used List to list some entities on database
Logged in my Mysql Database manualy and updated a field in some
entities
Used List again in hibernate doing an identical query as 1
The entity that hibernate listed was not updated.
If I close and open the application. it then shows the entity updated correctly.
Is hibernate using some kind of cache by default?
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/XXX</property>
<property name="hibernate.connection.username">XXXXXXXXXX</property>
<property name="hibernate.connection.password">XXXXXXXXXX</property>
<property name="show_sql">true</property>
Code that lists the entity:
Session s = HibernateUtil.getSession();
Criteria c = s.createCriteria(Solicitacao.class, "s");
//Add some Restrictions here
List<Solicitacao> ls = c.list();
s.close();
My Session factory:
public class HibernateUtil {
private static SessionFactory sessionFactory = null;
static {
// Configurações iniciais de caminho dos diretórios e arquivos
URL url = HibernateUtil.class.getProtectionDomain().getCodeSource().getLocation();
File myFile = null;
try {
myFile = new File(url.toURI());
} catch (Exception ex) {
}
File dir = myFile.getParentFile();
File xml = new File(dir, "hibernate.cfg.xml");
/*
* sessionFactory = new AnnotationConfiguration() .configure("br/com/netradiobrasil/pcp/" +
* "hibernate/hibernate.cfg.xml") .buildSessionFactory();
*/
sessionFactory = new AnnotationConfiguration().configure(xml).buildSessionFactory();
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
I tryed to add those lines in my hibernate.cfg.xml
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
Also tryed to use: session.setCacheMode(CacheMode.IGNORE)
but still didnt solve my problem
Let me guess
After executing this
Session s = HibernateUtil.getSession();
Criteria c = s.createCriteria(Solicitacao.class, "s");
//Add some Restrictions here
List<Solicitacao> ls = c.list();
You changed entries in database manually and then reran the query ? If yes then can you close the session and then rerun your code ?
Adding those lines in my hibernate.cfg.xml - that enables c3p0 fixed my problem
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">40</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">100</property>