Hibernate keep old data - mysql

I'm beginer in Hibernate and Spring MVC, and i have a repetitive problem on retrieving data, sometimes when i refresh my page or just submit a form, i get old data that i had updated or even deleted.
Here is my configurations file of hibernate :
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/cotaDB18</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> -->
<property name="hibernate.connection.isolation">4</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
I retrieve data like this :
#SuppressWarnings("unchecked")
#Override
public List<Tache> getTachesByPhase(int idPhase) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String hql = "From Tache T where T.phase=" + idPhase + " Order by T.index";
Query query = session.createQuery(hql);
return query.list();
}
Please i need your help, i don't find the solution

Related

spring + hibernate integration Table "table_name " is missing

I'm integrate spring with hibernate , I'm using dropwizard configaration files as : persistence file have the configuration files are in resources folder no issue with loading xml files. There is issue with found table .
persistence.xml file as :
<persistence-unit name="bfm" transaction-type="RESOURCE_LOCAL" >
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.bcits.parkingmanagement.model.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Configuring JDBC properties -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/anuj" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
</persistence-unit>
</persistence>
In spring application config xml as : there I show only hibernate configuration
<context:annotation-config />
<context:component-scan base-package="com.bcits.parkingmanagement.model" />
<jdbc:embedded-database id="dataSource" type="HSQL" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:spring/persistence.xml" />
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Table User missing
javax.persistence.PersistenceException: [PersistenceUnit: bfm] Unable to build EntityManagerFactory
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
... 18 more
Caused by: org.hibernate.HibernateException: Missing table: user
Please anyone help me how can I resolve this error. I want to configuration of spring with hibernate.I thing it is connect to database but can't found table. Table name and colume name exactly same. Database name also correct and no issue with username and password of database .
User model is: Here User is model name and user is table which have two column one is user_id and user_name. Issue is that i had used user instead of User in query. Because User is model name and user is table name .Correct model is given below
#NamedQueries({
#NamedQuery( name="getUserName" , query="select name from User")
})
#Entity
#Table( name ="user")
public class User {
#Id
#Column( name="user_id")
private int id;
#Column( name="user_name")
private String name;
//getter setter
}

Couchbase + xmemcached

I am using couchbase and xmemcached client in multithreaded java SE application. I am constantly observing that couchbase is unable to save last few keys whereas traditional memcached is setting all keys excellently. I am using exaclty same xmemcachedclient configuration in both cases.
Spring Configuration
<bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
<property name="configuration">
<bean class="com.google.code.yanf4j.config.Configuration">
<property name="handleReadWriteConcurrently" value="true" />
</bean>
</property>
<property name="servers">
<value>127.0.0.1:11211</value>
</property>
<!-- server's weights -->
<property name="weights">
<list>
<value>2</value>
</list>
</property>
<!-- nio connection pool size -->
<!-- <property name="connectionPoolSize" value="4"></property> -->
<!-- Use binary protocol,default is TextCommandFactory -->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory" />
</property>
<!-- Distributed strategy -->
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator" />
</property>
<!-- Serializing transcoder -->
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
<!-- ByteBuffer allocator -->
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator" />
</property>
</bean>
below is my test code that checks for missing keys
#Test
public void test100000HitMiss()
{
ArrayList<Integer> hits = new ArrayList<Integer>();
ArrayList<Integer> misses = new ArrayList<Integer>();
for(Integer i=0; i < 100000; i++)
{
try
{
Object val = memcachedClient.get("2/C/TALAL" + i.toString());
if(null == val)
misses.add(i);
else
hits.add(i);
}
catch (Exception ex)
{
logger.error(ex);
}
}
logger.info("Hits: " + hits.size() + " - Misses: " + misses.size());
}
Every time I run this test, I get 3 or 4 keys in misses array

error in hibernate query

my hibernate.cfg.xml is this :
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/****</property>
<property name="hibernate.connection.username">***</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactor y</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="hbm2ddl.auto">create</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<mapping class="entities.MisurazioneOraria"/>
<mapping class=....
....>
<listener type="post-insert" class="MisurazioneOrariaInsertListener"/>
</session-factory>
</hibernate-configuration>
i try to do query from this line code that is the listener :
ArrayList<MisurazioneOraria> mis = (ArrayList<MisurazioneOraria>) session.createQuery("from (misurazione_oraria) where sensore_idsensore = :idsensore").setParameter("idsensore", m.getSensore().getIdSensore()).list();
and the error is that :
mag 27, 2012 6:30:33 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
Grave: Could not synchronize database state with session
org.hibernate.QueryException: in expected: misurazione_oraria [from (misurazione_oraria) where sensore_idsensore = :idsensore]
my mysql version is 5.5.23. thanks for the help.
Your entity class is named MisurazioneOraria, not misurazione_oraria. HQL uses entities and fields/properties. It doesn't use table and column names.
Read the HQL documentation.
Moreover, Query.list() returns a List, and not necessarily an ArrayList. You shouldn't cast the result of the query to ArrayList.
change it to
List<MisurazioneOraria> mis = session.createQuery("from MisurazioneOraria where idSensore = :idsensore").setParameter("idsensore", m.getSensore().getIdSensore()).list();

Using Hibernate with SQL Server 2008

I'm using SQL Server 2008 with Hibernate 3.0 at data access layer of my application. The problem is that I'm unable to read from database.
Here is my Hibernate config;
<property name="connection.driver_class"> com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://localhost;databaseName=test;integratedSecurity=true;</property>
<property name="connection.username">not required</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="user.hbm.xml"/>
Here is user.hbm.xml
<class name="ammar.User" table="user" schema="dbo" catalog="test">
<id name="userId" type="int" column="userId" >
<generator class="assigned"/>
</id>
<property name="userName">
<column name="userName" />
</property>
</class>
And here is code I'm using to get data from database.
SessionFactory sF = new Configuration().configure().buildSessionFactory();
Session session = sF.openSession();
User user = (User) session.get(User.class, 1);
if(user!=null)
System.out.println(user.getUserName());
else
System.out.println("Not Found");
SQLGrammarException as well as SQLServerException occurs when I run this.
Urgent reply is needed.
Solved. Actually 'user' a is reserved word in SQL Server 2008. Anyhow, Thanks to #ManuPK.

Could not create the driver from NHibernate.Driver.MySqlDataDriver

I used this code in my hibernate.cfg.xml configuration:
<?xml version="1.0"?>
<hibernate-configuration xmlns= "urn:nhibernate-configuration-2.2">
<session-factory>
<!-- SQLServer: Provider --><!--
<property name="connection.provider"> NHibernate.Connection.DriverConnectionProvider </property>-->
<!-- SQLServer: Driver -->
<property name="connection.driver_class"> NHibernate.Driver.MySqlDataDriver </property>
<!-- SQLServer: Dialeto -->
<property name="dialect"> NHibernate.Dialect.MySQLDialect </property>
<!-- SQLServer: Connection String -->
<property name="connection.connection_string"> Database=nhibernate;Data Source=localhost;User Id=root;Password=xxxx</property>
<!-- Classe que serĂ¡ utilizada para Proxy
(LazyLoading) -->
<property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </property>
</session-factory>
</hibernate-configuration>
I have a problem creating the database.
NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.MySqlDataDriver. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.HibernateException: The IDbCommand and IDbConnection implementation in the assembly MySql.Data could not be found. ...
What should I do?
It's simple: add Mysql.Data.dll to the project's bin folder.