Wednesday, July 7, 2010

Deleting childeren in Hibernate


Lets take example of One to many relationship between Author and Book.

If you want to delete book without deleting parent i.e author you could simply delete using

delete(book)

which will cause :
org.springframework.dao.InvalidDataAccessApiUsageException: deleted object would be re-saved by cascade (remove deleted object from associations): [Book#1]; ...  
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:657)  
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)  
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)  
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)  
at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:881)  
at ConsoleScript7.run(ConsoleScript7:3)
Caused by: org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [Book#1] 
The problem is that the books are still in the author's collection, so when the session is flushed, they will be recreated. Solution will be remove the book from the collection itself.
author.getBooks().remove(bookId)
and then save author object.

Friday, June 25, 2010

Reading file from URL into byte array output stream

Here is code which reads file from URL and writes to ByteArrayOutPutStream

try
{
InputStream is = new URL("http://localhost:8080/pdf/test.pdf").openStream();
ByteArrayOutputStream outputDoc = new ByteArrayOutputStream();
byte buf[]=new byte[1024];
int len;
while((len=is.read(buf))>0)
{
outputDoc.write(buf,0, len);
}
outputDoc.close();
}
catch(Exception e) { e.printStackTrace(); }


Reading file from URL and writing to file

File file = new File("D:/test123.pdf"); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream outBuf = new BufferedOutputStream(fos);
InputStream is = new URL("http://localhost:8080/pdf/test.pdf").openStream();
byte buf[]=new byte[1024]; int len; while((len=is.read(buf))>0) { outBuf.write(buf,0, len); } outBuf.close(); fos.close();

Thursday, March 11, 2010

Resolving java.lang.OutOfMemoryError: PermGen space in Weblogic

Hi,

To resolve the java.lang.OutOfMemoryError: PermGen space in Weblogic please add the following line

set USER_MEM_ARGS=-Xms512m -Xmx1024m -XX:MaxPermSize=128m

in :\Oracle\Middleware\user_projects\domains\dgmt_domain\bin\setDomainEnv.cmd

Thanks,


Enable Remote Debugging in Weblogic

Hi,

Remote debugging in Weblogic with Eclipse can be configured as follows:

In startWebLogic.cmd file, can be found at :\Oracle\Middleware\user_projects\domains\dgmt_domain\bin\startWebLogic.cmd,

Replace

set JAVA_OPTIONS =%SAVE_JAVA_OPTIONS%

With

set JAVA_OPTIONS=-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=1044,server=y,suspend=n %SAVE_JAVA_OPTIONS%

While starting the Weblogic you will see “Listening for transport dt_socket at address: 1044” then we are sure that debug is enabled on weblogic.

Thanks,


Tuesday, February 9, 2010

Spring MVC Training

Check out this SlideShare Presentation: