alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (TestCustomColumnReadAndWrite.java)

This example Hibernate source code file (TestCustomColumnReadAndWrite.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

action, document, document, double, folder, folder, owner, owner, session, size_in_kb, size_in_kb, size_in_mb, testcustomcolumnreadandwrite, transaction

The Hibernate TestCustomColumnReadAndWrite.java source code

package org.hibernate.test.instrument.cases;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.hibernate.test.instrument.domain.Document;
import org.hibernate.test.instrument.domain.Folder;
import org.hibernate.test.instrument.domain.Owner;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

/**
 * @author Rob.Hasselbaum
 */
public class TestCustomColumnReadAndWrite extends AbstractExecutable {
	public void execute() {
		Session s = getFactory().openSession();
		Transaction t = s.beginTransaction();
		final double SIZE_IN_KB = 20480;
		final double SIZE_IN_MB = SIZE_IN_KB / 1024d;
		Owner o = new Owner();
		Document doc = new Document();
		Folder fol = new Folder();
		o.setName("gavin");
		doc.setName("Hibernate in Action");
		doc.setSummary("blah");
		doc.updateText("blah blah");	
		fol.setName("books");
		doc.setOwner(o);
		doc.setFolder(fol);
		doc.setSizeKb(SIZE_IN_KB);
		fol.getDocuments().add(doc);
		s.persist(o);
		s.persist(fol);
		t.commit();
		s.close();

		s = getFactory().openSession();
		t = s.beginTransaction();
		
		// Check value conversion on insert
		Double sizeViaSql = (Double)s.createSQLQuery("select size_mb from documents").uniqueResult();
		assertEquals( SIZE_IN_MB, sizeViaSql, 0.01d );

		// Test explicit fetch of all properties
		doc = (Document) s.createQuery("from Document fetch all properties").uniqueResult();
		assertTrue( Hibernate.isPropertyInitialized( doc, "sizeKb" ) );
		assertEquals( SIZE_IN_KB, doc.getSizeKb() );
		t.commit();
		s.close();		

		// Test lazy fetch with custom read
		s = getFactory().openSession();
		t = s.beginTransaction();
		doc = (Document) s.get( Document.class, doc.getId() );
		assertFalse( Hibernate.isPropertyInitialized( doc, "sizeKb" ) );
		assertEquals( SIZE_IN_KB, doc.getSizeKb() );
		s.delete(doc);
		s.delete( doc.getOwner() );
		s.delete( doc.getFolder() );
		t.commit();
		s.close();
	}
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate TestCustomColumnReadAndWrite.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.