Mar 15

This is one problem which ate around 2 hours of last night. Here are the possible solutions

1) There is a genuine case of duplication of the id column. Try querying the max (IDCOLUMN) from the table.

2) If the message says Duplicate entry ‘127′ or ‘32767′ or any of the max values in the following table

Type Bytes Minimum Value Maximum Value
    (Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127
    0 255
SMALLINT 2 -32768 32767
    0 65535
MEDIUMINT 3 -8388608 8388607
    0 16777215
INT 4 -2147483648 2147483647
    0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807
    0 18446744073709551615

then you need to change your datatype. Your next increment value isn’t getting accommodated within the datatype.

3) If you have an increment id in your Hibernate, please check whether your datatype is varchar in mysql. If it is varchar in mysql and if your hibernate mapping file maps to an Integer property in your Value Object, this problem is sure to occur after your 9th record.

Tags: , , ,

Mar 01

I found this very interesting and thought I could post this here.

Let me jump directly to the code.

On your jsp, you have

<input id=”productimage” name=”productimage” type=”file”>

Your ActionForm will have

private FormFile productimage;

public FormFile getProductimage() {
return productimage;
}
public void setProductimage(FormFile productimage) {
this.productimage = productimage;
}

My product.hbm.xml has

<property
name=”productimage”
type=”blob”
column=”productimage”
not-null=”true”
length=”250″
/>


My ActionClass has

Product product=new Product();
product.setProductimg(

product.toByteArrayImpl(adminForm.getProductimage()));
model.addProduct(factory, product);

And my model.addProduct() has


public boolean addProduct(SessionFactory factory, Product product) {
Session session =null;
try {
session = factory.openSession();
session.save(product);
} catch (Exception e) {
e.printStackTrace();
return false;
}
finally{
session.flush();
session.close();

}

return true;

}

and my ProductVO has

private byte[] productimg;
private Blob productimage;

/** Don’t invoke this. Used by Hibernate only. */
public void setProductimage(Blob productimage) {
this.productimg = this.toByteArray(productimage);
}

/** Don’t invoke this. Used by Hibernate only. */
public Blob getProductimage() {
return Hibernate.createBlob(this.productimg);
}


public byte[] getProductimg() {
return productimg;
}
public void setProductimg(byte[] productimg) {
this.productimg = productimg;
}

public byte[] toByteArray(Blob fromBlob) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
return toByteArrayImpl(fromBlob, baos);
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (baos != null) {
try {
baos.close();
} catch (IOException ex) {
}
}
}
}

public byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)
throws SQLException, IOException {
byte[] buf = new byte[4000];
InputStream is = fromBlob.getBinaryStream();
try {
for (;;) {
int dataSize = is.read(buf);

if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
}
}
}
return baos.toByteArray();
}

public byte[] toByteArrayImpl(FormFile formfile)
throws SQLException, IOException {
byte[] buf = new byte[4000];
InputStream is = formfile.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
for (;;) {
int dataSize = is.read(buf);

if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ex) {
}
}
}
return baos.toByteArray();
}

 

Tags: , , , ,

Feb 03

Recently, I was trying to integrate Struts with Hibernate 3.2 under Weblogic. And i had this big pestering exception…

javax.servlet.ServletException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken [from com.g2h.biz.vo.Category category]
at weblogic.servlet.internal.ServletStubImpl.createServlet(ServletStubImpl.java:909)
at weblogic.servlet.internal.ServletStubImpl.createInstances(ServletStubImpl.java:873)
at weblogic.servlet.internal.ServletStubImpl.prepareServlet(ServletStubImpl.java:812)
at weblogic.servlet.internal.WebAppServletContext.preloadServlet(WebAppServletContext.java:3281)
at weblogic.servlet.internal.WebAppServletContext.preloadServlets(WebAppServletContext.java:3226)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:3207)
at weblogic.servlet.internal.HttpServer.preloadResources(HttpServer.java:694)
at weblogic.servlet.internal.WebService.preloadResources(WebService.java:483)
at weblogic.servlet.internal.ServletInitService.resume(ServletInitService.java:30)
at weblogic.t3.srvr.SubsystemManager.resume(SubsystemManager.java:131)
at weblogic.t3.srvr.T3Srvr.resume(T3Srvr.java:966)
at weblogic.t3.srvr.T3Srvr.run(T3Srvr.java:361)
at weblogic.Server.main(Server.java:32)

The reason for this problem is this. Both Weblogic and Hibernate was using “antrl.jar”. Weblogic 8.1, that i was using, had an “antlr” jar bundled inside its weblogic.jar. Since server libraries had preference over the jars in our application, hibernate wasnt using the latest antlr jar in my web application’s lib folder.

Solution that i used :

Put the following lines in your “.cfg.xml” inside your session-factory tag

classicquery
There is a biggggg discussion in the hibernate forum on this issue. It also had another solution for weblogic. To add the following tag inside weblogic.xml

weblogic.xml

Tags: ,

Give your best to the world.