我运行的这段程序出错了:(spring+hibernate) public void save() { Session s=null; Transaction tx=null; try { s=getHibernateTemplate().getSessionFactory().openSession(); } catch (HibernateException e1) { e1.printStackTrace(); } try { tx = s.beginTransaction(); } catch (HibernateException e2) { e2.printStackTrace(); } byte[] buffer = new byte[1]; buffer[0] = 1; try { Email c = new Email(); c.setTitle("Hello Jacky"); c.setAnnex(Hibernate.createBlob(buffer)); s.save(c); s.refresh(c, LockMode.UPGRADE); Blob blob = (Blob) c.getAnnex(); /**robbin给的例子有一句:OutputStream out = blob.getBinaryOutputStream(); *但Blob没有这个方法只有:blob.setBinaryStream(long),返回的是OutputStream,所以我把它改成: *OutputStream out = blob.setBinaryStream(1024*1024); */ OutputStream out = blob.setBinaryStream(1024*1024); String fileName = "e:\\proxxool.xml"; File f = new File(fileName); FileInputStream in=new FileInputStream(f); int count = -1, total = 0; byte[] data = new byte[(int) in.available()]; in.read(data); out.write(data); in.close(); out.close(); s.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); } }出错信息如下:Hibernate: insert into OA_Email (title, content, save_type, create_time, create_personal, annex) values (?, ?, ?, ?, ?, ?) Hibernate: select email_id, title, content, save_type, create_time as create_t5_, create_personal as create_p6_, annex from OA_Email where email_id =? java.sql.SQLException: The start position is beyond the end of the data. at net.sourceforge.jtds.jdbc.BlobImpl.setBinaryStream(BlobImpl.java:245) at cn.com.hnkj.common.hibernate.HibernateDAOImpl.save(HibernateDAOImpl.java:189) at cn.com.hnkj.common.spring.CommonServiceImpl.save(CommonServiceImpl.java:311) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:59) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:149) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:118) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:191) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148) at $Proxy0.save(Unknown Source) at cn.com.hnkj.test.AllTests.suite(AllTests.java:92) at cn.com.hnkj.test.AllTests.main(AllTests.java:73)
to 0xCafeBabe(CafeBabe) :你不知道就不要瞎说啊,问题解决了,将OutputStream out = blob.setBinaryStream(1024*1024); 改为: OutputStream out = blob.setBinaryStream(1);就可以了
public void save() {
Session s=null;
Transaction tx=null;
try {
s=getHibernateTemplate().getSessionFactory().openSession();
} catch (HibernateException e1) {
e1.printStackTrace();
}
try {
tx = s.beginTransaction();
} catch (HibernateException e2) {
e2.printStackTrace();
}
byte[] buffer = new byte[1];
buffer[0] = 1;
try {
Email c = new Email();
c.setTitle("Hello Jacky");
c.setAnnex(Hibernate.createBlob(buffer));
s.save(c);
s.refresh(c, LockMode.UPGRADE);
Blob blob = (Blob) c.getAnnex();
/**robbin给的例子有一句:OutputStream out = blob.getBinaryOutputStream();
*但Blob没有这个方法只有:blob.setBinaryStream(long),返回的是OutputStream,所以我把它改成:
*OutputStream out = blob.setBinaryStream(1024*1024);
*/
OutputStream out = blob.setBinaryStream(1024*1024);
String fileName = "e:\\proxxool.xml";
File f = new File(fileName);
FileInputStream in=new FileInputStream(f);
int count = -1, total = 0;
byte[] data = new byte[(int) in.available()];
in.read(data);
out.write(data);
in.close();
out.close();
s.flush();
tx.commit();
} catch (Exception e) {
e.printStackTrace();
}
}出错信息如下:Hibernate: insert into OA_Email (title, content, save_type, create_time, create_personal, annex) values (?, ?, ?, ?, ?, ?)
Hibernate: select email_id, title, content, save_type, create_time as create_t5_, create_personal as create_p6_, annex from OA_Email where email_id =?
java.sql.SQLException: The start position is beyond the end of the data.
at net.sourceforge.jtds.jdbc.BlobImpl.setBinaryStream(BlobImpl.java:245)
at cn.com.hnkj.common.hibernate.HibernateDAOImpl.save(HibernateDAOImpl.java:189)
at cn.com.hnkj.common.spring.CommonServiceImpl.save(CommonServiceImpl.java:311)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:149)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:118)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:191)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:148)
at $Proxy0.save(Unknown Source)
at cn.com.hnkj.test.AllTests.suite(AllTests.java:92)
at cn.com.hnkj.test.AllTests.main(AllTests.java:73)
改为: OutputStream out = blob.setBinaryStream(1);就可以了