Friday, November 6, 2009

Hibernate Lazy Initialization

Lazy Initialization in Hibernate

Hibernate supports the feature of lazy initilasation for both entities and collections, which actually means is, the Hibernate engine loads only those objects that we are querying for and doesn't try to fetch other entities (that are associated with the entity we are querying) or collections.


Lazy initialization load the child objects while loading parent object. To getting this set the lazy false in mapping file or class.lazy false in class file and hibernate will load the child when parent is loaded from the database. by default lazy is true. Lazy loading means that any foreign key references that you have in your table will be loaded only when referred to by the application. Eager loading means everything will be loaded at once.


An attribute 'lazy' can be used to let Hibernate know if the associated entity or collection has to be lazily loaded or prefetched.

<set name="Child" lazy="false" inverse="true">
<key column="FOREIGN_KEY_COL"/>

<one-to-many class="Parent"/>

</set>
This causes the collection to be eagerly fetched rather than doing a lazy fetch. If on the other hand, the attribute value of lazy is set to true, then hibernate will not make an attempt to fire the query for fetchingthe collection object until the request is made by the user.

Read more...

Wednesday, November 4, 2009

Java Program to list the Contents of a Zip File

Java Sample program to list the contents of a zip file

In this program we need to get the entries of the zip file, since each file in a zip file is represented by an entry. In this program assume that the filename of the zip file is 'Test.zip'. By calling the entries method of the ZipFile object we get an Enumeration back that can be used to loop through the entries of the file. We have to cast each element in the Enumeration to a ZipEntry.


import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.io.IOException;
import java.util.Enumeration;

public class ListZipFiles
{
public void doListFiles()
{
try
{
ZipFile zipFile = new ZipFile("Test.zip");
Enumeration zipEntries = zipFile.entries();

while (zipEntries.hasMoreElements())
{
//Process the name, here we just print it out
System.out.println(((ZipEntry)zipEntries.nextElement()).getName());
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
// Command line arguments
public static void main(String[] args) {

new Main().doListFiles();
}
}

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP