I'm trying what is essentially 'hello world' with Hibernate and a Derby database. I already have a Java/JDBC program that interacts with the database correctly and I'm trying to reproduce the project with Hibernate. So far I have only a single table that has a record id. I can populate the corresponding class with data and do a session.save() on it and everything is happy. Following that I have a session.createQuery().list() which lists all the records currently in the database and the brand new record that I just inserted. However, when I use Derby's IJ tool to do a select on the table, all the pre-existing rows are there but the new row is not. I'm guessing that the list() was pulling up the new row from some cache (?) and I'm not clear on how to make sure it gets physically written to the database.
Any help would be greatly appreciated.
-- Carey
Code:
public class Test
{
public static void retrieveImages()
{
// System.out.println( "Retrieving Image list from IMAGE_INFO." );
Session session = HibernateUtil.getSessionFactory().openSession();
@SuppressWarnings("unchecked")
List<Image> imageList = session.createQuery( "from Image" ).list();
for( Image image : imageList )
{
if( image.getImageId() > 560 )
System.out.println( image );
}
session.flush();
session.close();
}
public static void saveImage( Image image )
{
// System.out.println( "\n Saving file name " + image.getFileName() );
Session session = HibernateUtil.getSessionFactory().openSession();
session.save( image );
session.flush();
session.close();
}
public static void main( String args[] )
{
retrieveImages();
Image image = new Image();
image.setFileName( "this_is_a_file_name.jpg" + (new Date()) ); // UNIQUE
image.setPathId( null );
image.setFileSize( 42L );
image.setFileDate( new Timestamp( System.currentTimeMillis() ) );
image.setImageWidth( 1600 );
image.setImageHeight( 900 );
image.setImagePixels( 1600 * 900 );
image.setImageOrientationCd( "L" );
image.setImageAspect( 1600.0f / 900.0f );
image.setImageRating( null );
image.setImageDescription( "This is a short description" );
image.setHtmlInd( false );
image.setModelInd( false );
image.setKeywordInd( false );
image.setGpsLat( null );
image.setGpsLon( null );
System.out.println( image );
System.out.println( "----------------" );
saveImage( image );
System.out.println( image );
System.out.println( "----------------" );
// HibernateUtil.getSessionFactory().close();
}
}