-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: lazily loading a column (blob) from database
PostPosted: Mon Mar 27, 2006 11:26 am 
Newbie

Joined: Mon Nov 15, 2004 2:11 am
Posts: 2
Hi All,
we want to lazily load a column from database. As that column is a blob object,we don't want to load that heavy blob object in particular usecases.

We are using Oracle 10g ,Hibernate 3.1 and Spring 1.2.6.


I have attached code for mapping blob object for AttachmentVO.


package com.fedbid.model.vo;

import java.io.Serializable;
import java.util.Date;


/**@author Hibernate CodeGenerator
* @hibernate.class table="ATTACHMENT"
*
* */
public class AttachmentVO implements Serializable{

/** identifier field */
private Integer attachmentId;

/** identifier field */
private Integer auctionId;


/** persistent field */
private byte[] attachment;

/** default constructor */
public AttachmentVO() {
}

/**
* @return Returns the attachment.
* @hibernate.property column="ATTACHMENT" type="com.fedbid.model.BinaryBlobType"
*/
public byte[] getAttachment() {
return attachment;
}

/**
* @param attachment
* The attachment to set.
*/
public void setAttachment(byte[] attachment) {
this.attachment = attachment;
}
/**
* @return Returns the attachmentId.
* @hibernate.id column="ATTACHMENT_ID"generator-class="sequence"
* @hibernate.generator-param name="sequence" value="ATTACHMENT_seq"
*/
public Integer getAttachmentId() {
return attachmentId;
}

/**
* @param attachmentId
* The attachmentId to set.
*/
public void setAttachmentId(Integer attachmentId) {
this.attachmentId = attachmentId;
}

/**
* @return Returns the auctionId.
* @hibernate.property column="AUCTION_ID"
*/
public Integer getAuctionId() {
return auctionId;
}


}



BinaryBlobUserType Class:

/**
*
*/
package com.fedbid.model;

import java.io.Serializable;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

/**
*
* Utility method for storing and retrieving blob data using hiberante. This class is used in
* AttachmentVO : @hibernate.property column="ATTACHMENT" type="com.fedbid.model.BinaryBlobType"
* for byte[] getAttachment() method.
*
*/
public class BinaryBlobType implements UserType {

public int[] sqlTypes() {
return new int[] { Types.BLOB };
}

public Class returnedClass() {
return byte[].class;
}

public boolean equals(Object x, Object y) {
return (x == y)
|| (x != null && y != null && java.util.Arrays.equals(
(byte[]) x, (byte[]) y));
}

public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
Blob blob = rs.getBlob(names[0]);
return blob.getBytes(1, (int) blob.length());
}

public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
st.setBytes(index, (byte[]) value);
}

public Object deepCopy(Object value) {
if (value == null)
return null;

byte[] bytes = (byte[]) value;
byte[] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);

return result;
}

public boolean isMutable() {
return true;
}

public int hashCode(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return 0;
}

public Serializable disassemble(Object arg0) throws HibernateException {
// TODO Auto-generated method stub
return null;
}

public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}

public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
// TODO Auto-generated method stub
return null;
}

}


I really appreciate for any suggestions or help.

Thanks,
Santhosh.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 2:49 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
did you try the lazy flag on the BLOB property?
it requires: bytecode instrumentation - shown how to setup on pg 178 of the reference.pdf.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 4:00 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
the other thing you may consider is writing a named query that executes a stored procedure to get (and another that would update) nothing other than the blob, and DON'T map the blob in your class. This way you could call the function that gets the blob from your DAO class at your leisure.

check out <sql-query in the reference.pdf or you can view some of my postings that have discussed this with "Search" (for "procedure" author: jt_1000).

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject: blob lazy loading
PostPosted: Mon Mar 27, 2006 4:33 pm 
Newbie

Joined: Mon Nov 15, 2004 2:11 am
Posts: 2
Hi JT,
Could you please give me example for instrumentation. I used ant to generate instrumentation class code.

I get the following error when I try to run junit test for lazy loading property fields:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.fedbid.model.dao.AttachmentDAOTest' defined in null: Unsatisfied dependency expressed through bean property 'interceptFieldCallback': set this property value or disable dependency checking for this bean
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:962)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:823)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:200)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:187)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



Thanks a lot for your quick reply,

santhosh.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 4:46 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
I have not done bytecode instrumentation - that is why I referred you to the docs. You might consider the stored-proc (or a straight query) way...

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.