These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: HQL Queries not reflecting latest data from database
PostPosted: Thu Jun 19, 2008 6:01 pm 
Newbie

Joined: Fri Aug 05, 2005 2:56 pm
Posts: 11
I currently have an issue when I run an HQL Query.
I populate data into my tables and run the application.
When I execute the HQL query below I get the initial data populated in the database.

I then manual update the data in the database via the MySQL command line.

When I execute the HQL query again I do not receive the updated DB data.

I feel the Queries are being cached but I am not sure where.
Is there a way to force all HQL queries to hit the database???

Thank for any help in advance!
-Kyle

Hibernate version: 3.x under JBoss 4.2.2.GA

Name and version of the database you are using: MySQL 5.0.51

Code between sessionFactory.openSession() and session.close():

session = sessionFactory.openSession();

List<FicBrandDescription> brandDescs = new ArrayList<FicBrandDescription>();
String queryString = "from FicBrandDescription as brandDescription "+
"join fetch brandDescription.theLocale as locale "+
"where locale.theCountryId = :countryId "+
"and locale.theLangId = :langId "+
"order by brandDescription.theTitleTag asc";
Query query = session.createQuery(queryString);
query.setString("langId", aLocale.getLangId());
query.setString("countryId", aLocale.getCountryId());
brandDescs = query.list();

session.flush();
session.close();




Mapping documents:


<?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 name="common_shard">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.autoReconnect">true</property>
<property name="hibernate.connection.autoReconnectForPools">true</property>
<property name="hibernate.connection.is-connection-validation-required">true</property>
<!-- C3P0 Settings - for connection pooling -->
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.timeout">100</property>

<!-- Common Entities -->
<mapping class="com.thesearchagency.transformer.model.common.FicBrand"/>
<mapping class="com.thesearchagency.transformer.model.common.FicBrandDescriptionImage"/>
</session-factory>
</hibernate-configuration>


@Entity
@Table(name = "fic_brand")
public class FicBrand implements Serializable, Comparable<FicBrand>
{
private static final long serialVersionUID = 20080515001L;

@Id
@Column(name = "brand_id")
private Integer theBrandId;

@OneToMany(mappedBy="theBrand")
private Set<FicBrandDescription> theBrandDescriptions;

@Column(name = "name")
private String theName;

@Column(name = "description")
private String theDescription;

@Column(name = "update_sysdate")
private Timestamp theUpdateSysdate;

@Column(name = "create_sysdate", updatable = false)
private Timestamp theCreateSysdate;


/**
* Default Constructor
*/
public FicBrand()
{
super();
}

/**
* @return the brandId
*/
public Integer getBrandId()
{
return theBrandId;
}

/**
* @param anBrandId the brandId to set
*/
public void setBrandId(Integer anBrandId)
{
theBrandId = anBrandId;
}

/**
* @return the brandDescriptions
*/
public Set<FicBrandDescription> getBrandDescriptions()
{
return theBrandDescriptions;
}

/**
* @param anBrandDescriptions the brandDescriptions to set
*/
public void setBrandDescriptions(Set<FicBrandDescription> anBrandDescriptions)
{
theBrandDescriptions = anBrandDescriptions;
}

/**
* @return the name
*/
public String getName()
{
return theName;
}

/**
* @param anName the name to set
*/
public void setName(String anName)
{
theName = anName;
}

/**
* @return the description
*/
public String getDescription()
{
return theDescription;
}

/**
* @param anDescription the description to set
*/
public void setDescription(String anDescription)
{
theDescription = anDescription;
}

/**
* @return the updateSysdate
*/
public Timestamp getUpdateSysdate()
{
return theUpdateSysdate;
}

/**
* @param anUpdateSysdate the updateSysdate to set
*/
public void setUpdateSysdate(Timestamp anUpdateSysdate)
{
theUpdateSysdate = anUpdateSysdate;
}

/**
* @return the createSysdate
*/
public Timestamp getCreateSysdate()
{
return theCreateSysdate;
}

/**
* @param anCreateSysdate the createSysdate to set
*/
public void setCreateSysdate(Timestamp anCreateSysdate)
{
theCreateSysdate = anCreateSysdate;
}

/**
* FicBrand Comparator
*/
public int compareTo(FicBrand anO)
{
return this.theName.toLowerCase().compareTo(anO.theName.toLowerCase());
}

/**
*
*/
public String toString()
{
StringBuffer buff = new StringBuffer();
buff.append("FicBrand ::\n");
buff.append("\tBrandId :: " + this.theBrandId + "\n");
buff.append("\tName :: " + this.theName+ "\n");
buff.append("\tDescription :: " + this.theDescription + "\n");
buff.append("\tBrandDescription(s) - \n");
for(FicBrandDescription brandDesc : this.theBrandDescriptions) {
buff.append("\t\tName :: " + brandDesc.getTitleTag()+"\n");
buff.append("\t\tLocale :: " + brandDesc.getLocale().getLangId()+"_"+brandDesc.getLocale().getCountryId()+"\n");
buff.append("\t\tImageName :: " + brandDesc.getBrandDescriptionImage().getImageFileName()+"\n");
buff.append("\t\tImageFileType :: " + brandDesc.getBrandDescriptionImage().getImageType().getType()+"\n");
}
buff.append("\tCreateSysdate :: " + this.theCreateSysdate + "\n");
buff.append("\tUpdateSysdate :: " + this.theUpdateSysdate + "\n");
return buff.toString();
}


}



@Entity
@Table(name = "fic_brand_description")
public class FicBrandDescription implements Serializable
{
private static final long serialVersionUID = 20080519001L;

@Id
@Column(name = "brand_description_id")
private Integer theBrandDescriptionId;

@ManyToOne
@JoinColumn(name="brand_id", referencedColumnName="brand_id", insertable=false, updatable=false)
private FicBrand theBrand;

@OneToOne
@JoinColumn(name = "locale_id", referencedColumnName = "locale_id", insertable = false, updatable = false)
private FicLocale theLocale;

@OneToOne
@JoinColumn(name = "brand_description_id", referencedColumnName = "brand_description_id", insertable = false, updatable = false)
private FicBrandDescriptionImage theBrandDescriptionImage;

@Column(name="primary_keyword")
private String thePrimaryKeyword;

@Column(name="title_tag")
private String theTitleTag;

@Column(name="meta_description_tag")
private String theMetaDescriptionTag;

@Column(name="h1")
private String theH1;

@Column(name="h2")
private String theH2;

@Column(name="h3")
private String theH3;

@Column(name="top_copy")
private String theTopCopy;

@Column(name="bottom_copy")
private String theBottomCopy;

@Column(name = "create_sysdate", updatable = false)
private Timestamp theCreateSysdate;

@Column(name = "update_sysdate")
private Timestamp theUpdateSysdate;

/**
* Default Constructor
*/
public FicBrandDescription()
{
super();
}

/**
* @return the brandDescriptionId
*/
public Integer getBrandDescriptionId()
{
return theBrandDescriptionId;
}

/**
* @param anBrandDescriptionId the brandDescriptionId to set
*/
public void setBrandDescriptionId(Integer anBrandDescriptionId)
{
theBrandDescriptionId = anBrandDescriptionId;
}

/**
* @return the brand
*/
public FicBrand getBrand()
{
return theBrand;
}

/**
* @param anBrand the brand to set
*/
public void setBrand(FicBrand anBrand)
{
theBrand = anBrand;
}

/**
* @return the locale
*/
public FicLocale getLocale()
{
return theLocale;
}

/**
* @param anLocale the locale to set
*/
public void setLocale(FicLocale anLocale)
{
theLocale = anLocale;
}

/**
* @return the brandDescriptionImage
*/
public FicBrandDescriptionImage getBrandDescriptionImage()
{
return theBrandDescriptionImage;
}

/**
* @param anBrandDescriptionImage the brandDescriptionImage to set
*/
public void setBrandDescriptionImage(FicBrandDescriptionImage anBrandDescriptionImage)
{
theBrandDescriptionImage = anBrandDescriptionImage;
}

/**
* @return the primaryKeyword
*/
public String getPrimaryKeyword()
{
return thePrimaryKeyword;
}

/**
* @param anPrimaryKeyword the primaryKeyword to set
*/
public void setPrimaryKeyword(String anPrimaryKeyword)
{
thePrimaryKeyword = anPrimaryKeyword;
}

/**
* @return the titleTag
*/
public String getTitleTag()
{
return theTitleTag;
}

/**
* @param anTitleTag the titleTag to set
*/
public void setTitleTag(String anTitleTag)
{
theTitleTag = anTitleTag;
}

/**
* @return the metaDescriptionTag
*/
public String getMetaDescriptionTag()
{
return theMetaDescriptionTag;
}

/**
* @param anMetaDescriptionTag the metaDescriptionTag to set
*/
public void setMetaDescriptionTag(String anMetaDescriptionTag)
{
theMetaDescriptionTag = anMetaDescriptionTag;
}

/**
* @return the h1
*/
public String getH1()
{
return theH1;
}

/**
* @param anH1 the h1 to set
*/
public void setH1(String anH1)
{
theH1 = anH1;
}

/**
* @return the h2
*/
public String getH2()
{
return theH2;
}

/**
* @param anH2 the h2 to set
*/
public void setH2(String anH2)
{
theH2 = anH2;
}

/**
* @return the h3
*/
public String getH3()
{
return theH3;
}

/**
* @param anH3 the h3 to set
*/
public void setH3(String anH3)
{
theH3 = anH3;
}

/**
* @return the topCopy
*/
public String getTopCopy()
{
return theTopCopy;
}

/**
* @param anTopCopy the topCopy to set
*/
public void setTopCopy(String anTopCopy)
{
theTopCopy = anTopCopy;
}

/**
* @return the bottomCopy
*/
public String getBottomCopy()
{
return theBottomCopy;
}

/**
* @param anBottomCopy the bottomCopy to set
*/
public void setBottomCopy(String anBottomCopy)
{
theBottomCopy = anBottomCopy;
}

/**
* @return the createSysdate
*/
public Timestamp getCreateSysdate()
{
return theCreateSysdate;
}

/**
* @param anCreateSysdate the createSysdate to set
*/
public void setCreateSysdate(Timestamp anCreateSysdate)
{
theCreateSysdate = anCreateSysdate;
}

/**
* @return the updateSysdate
*/
public Timestamp getUpdateSysdate()
{
return theUpdateSysdate;
}

/**
* @param anUpdateSysdate the updateSysdate to set
*/
public void setUpdateSysdate(Timestamp anUpdateSysdate)
{
theUpdateSysdate = anUpdateSysdate;
}

/**
*
*/
public String toString()
{
StringBuffer buff = new StringBuffer();
buff.append("FicBrandDescription ::\n");
buff.append("\tBrandDescriptionId :: " + this.theBrandDescriptionId + "\n");
buff.append("\tBrand :: " + this.theBrand.getName()+ "\n");
buff.append("\tLocale :: " + this.theLocale.getLangId()+"_"+this.theLocale.getCountryId()+ "\n");
buff.append("\tBrandDescriptionImageName :: " + this.theBrandDescriptionImage.getImageFileName()+ "\n");
buff.append("\tPrimaryKeyword :: " + this.thePrimaryKeyword + "\n");
buff.append("\tTitleTag :: " + this.theTitleTag + "\n");
buff.append("\tMetaDescription :: " + this.theMetaDescriptionTag + "\n");
buff.append("\tH1 :: " + this.theH1 + "\n");
buff.append("\tH2 :: " + this.theH2 + "\n");
buff.append("\tH3 :: " + this.theH3 + "\n");
buff.append("\tTopCopy :: " + this.theTopCopy + "\n");
buff.append("\tBottomCopy :: " + this.theBottomCopy + "\n");
buff.append("\tCreateSysdate :: " + this.theCreateSysdate + "\n");
buff.append("\tUpdateSysdate :: " + this.theUpdateSysdate + "\n");
return buff.toString();
}
}



Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 23, 2008 10:59 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Um...you think the data is cached? Perhaps, in the Hibernate cache?

Hibernate caches the data in it's primary cache. If you update the underlying database, you need to call a refresh on Hibernate.

Everything is working as expected.

Check out the JavaDoc on the Hibernate3 refresh method of the Hibernate Session:

Quote:
refresh

public void refresh(Object object)
throws HibernateException

Re-read the state of the given instance from the underlying database. It is inadvisable to use this to implement long-running sessions that span many business tasks. This method is, however, useful in certain special circumstances. For example

* where a database trigger alters the object state upon insert or update
* after executing direct SQL (eg. a mass update) in the same session
* after inserting a Blob or Clob

Parameters:
object - a persistent or detached instance
Throws:
HibernateException





http://www.hibernate.org/hib_docs/v3/api/org/hibernate/Session.html#refresh(java.lang.Object)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.