Friday, March 25, 2011

Insert LONGTEXT in Database using Hibernate

I would like to share my experience while inserting a Big XML file to the Database, The column dataType is LONGTEXT. The XML File sine is about 1.2MB, when i tried to insert in database using the following Hibernate Code i got the Exception as follows.

Transaction transaction =  sessionFactory.getCurrentSession().beginTransaction();

Where xmlData is as follows

public class XMLData {
 private int id;
 private String xml;

 public int getId() {
  return id;

 public void setId(int id) { = id;

 public String getXml() {
  return xml;

 public void setXml(String xml) {
  this.xml = xml;
I got the following Exception.

Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [org.cgi.model.Server]
 at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(
 at org.hibernate.exception.SQLStateConverter.convert(
 at org.hibernate.exception.JDBCExceptionHelper.convert(
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(
 at org.hibernate.persister.entity.AbstractEntityPersister.insert(
 at org.hibernate.action.EntityIdentityInsertAction.execute(
 at org.hibernate.engine.ActionQueue.execute(
 at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(
 at org.hibernate.event.def.AbstractSaveEventListener.performSave(
 at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(
 at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(
 at org.hibernate.event.def.DefaultPersistEventListener.onPersist(
 at org.hibernate.event.def.DefaultPersistEventListener.onPersist(
 at org.hibernate.impl.SessionImpl.firePersist(
 at org.hibernate.impl.SessionImpl.persist(
 at org.hibernate.impl.SessionImpl.persist(
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at java.lang.reflect.Method.invoke(
 at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(
 at $Proxy0.persist(Unknown Source)
 at org.cgi.dao.manager.ServerDAO.persist(
 at org.cgi.dao.manager.test.TestManager.main(
Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1094608 > 177152). You can change this value on the server by setting the max_allowed_packet' variable.
 at com.mysql.jdbc.MysqlIO.send(
 at com.mysql.jdbc.MysqlIO.sendCommand(
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
 at com.mysql.jdbc.ConnectionImpl.execSQL(
 at com.mysql.jdbc.PreparedStatement.executeInternal(
 at com.mysql.jdbc.PreparedStatement.executeUpdate(
 at com.mysql.jdbc.PreparedStatement.executeUpdate(
 at com.mysql.jdbc.PreparedStatement.executeUpdate(
 ... 21 more

As it is mentioned in the Exception we need to increase the max_allowed_packet variable of MySQL server.
By Default MySQL provides the size of 1 MB. you can read the value by the following Command in MySQL Command Prompt.

show variables like '%max_allowed_packet';
Now you can Set the Value by using the following Command.
set global max_allowed_packet=16777216;

For Reference
The maximum allowed packet size is 1GB.

Monday, March 7, 2011

Create Executable JAR File Using Eclipse

We all are aware of the main part of an Executable JAR file is keeping the MANIFEST.MF in META-INF Folder. Also inside the MANIFEST.MF we need to provide the following Details.

Manifest-Version: 1.0
  Main-Class: com.example.Example
  Class-Path: dependent.jar
Classpath will be required in case you have more dependent jars are there, also we create the Same jar using the JAR Command provided by Java as follows.
jar cvfm myFile.jar myManifestFile *.class
I found that some of the executable jars will through ClassNotFound Exception.
Hence i recommend to follow the Eclipse Jar Export Option.

The test Project i am going through have a Sample Java file to open a Window using the Java Swing.
Once I execute the project the Output is as Follows.
Now lets Create the JAR to run the Application.
Step 1: Right Click on the Project and Select Export.
Step 2: Select teh Export Destination as Runnable JAR File, and click Next.
Step 3: Select the Class with main method, which need to be executed initially and teh Destination Folder where the Jar need to be saved. Here i selected "C:\test.jar"
Step 4: Click Finish, Now your Executable Jar is ready @ C:\test.jar.
Hope you enjoyed the work through.