2.1.6
<hibernate-mapping>
<class name="lyo.test.bean.Trade" table="tradetest">
<cache usage="read-write"/>
<id name="trade_id" column="trade_id" type="int">
<generator class="native"/>
</id>
<property name="trade_name" column="trade_name"/>
<property name="trade_desc" column="trade_desc"/>
<property name="picture_file">
<column name="picture_file" sql-type="longblob"/>
</property>
</class>
</hibernate-mapping>
Session s=HibernateUtil.currentSession();
Transaction t=s.beginTransaction();
Integer tradeid=(Integer)s.save(trade);
log.info("Save "+tradeid+" trade complete !!! -----------------------------");
t.commit();
HibernateUtil.closeSession();
19:31:07,632 INFO SettingsFactory:129 - cache provider: net.sf.hibernate.cache.
EhCacheProvider
19:31:07,652 INFO SettingsFactory:141 - query cache factory: net.sf.hibernate.c
ache.StandardQueryCacheFactory
19:31:07,673 INFO Configuration:1116 - instantiating and configuring caches
19:31:07,943 WARN EhCache:94 - Could not find configuration for hibernate.test.
lyo.test.bean.Trade. Configuring using the defaultCache settings.
19:31:08,704 INFO SessionFactoryImpl:118 - building session factory
19:31:10,036 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI,
no JNDI name configured
19:31:10,046 INFO UpdateTimestampsCache:35 - starting update timestamps cache a
t region: net.sf.hibernate.cache.UpdateTimestampsCache
19:31:10,046 WARN EhCache:94 - Could not find configuration for net.sf.hibernat
e.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
19:31:10,066 INFO StandardQueryCache:41 - starting query cache at region: net.s
f.hibernate.cache.StandardQueryCache
19:31:10,066 WARN EhCache:94 - Could not find configuration for net.sf.hibernat
e.cache.StandardQueryCache. Configuring using the defaultCache settings.
Hibernate: insert into tradetest (trade_name, trade_desc, picture_file) values (
?, ?, ?)
java.lang.IllegalArgumentException: Packet is larger than max_allowed_packet fro
m server configuration of 1048576 bytes
at org.gjt.mm.mysql.Buffer.ensureCapacity(Buffer.java:398)
at org.gjt.mm.mysql.Buffer.writeBytesNoNull(Buffer.java:386)
at org.gjt.mm.mysql.PreparedStatement.executeUpdate(PreparedStatement.ja
va:352)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.jav
a:528)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.jav
a:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIde
ntityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(Session
Impl.java:775)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at lyo.test.servlet.test.doPost(test.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:20
6)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:688)
at java.lang.Thread.run(Thread.java:536)
before log######################
19:31:10,897 ERROR AssertionFailure:22 - an assertion failure occured (this may
indicate a bug in Hibernate, but is more likely due to unsafe use of the session
)
net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session aft
er an exception occurs)
at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2641)
at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2465)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2458
)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:22
60)
at net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.jav
a:1805)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1568)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1533)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1521)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1513)
at lyo.test.service.TradeDAOImpl.getTrades(TradeDAOImpl.java:30)
at lyo.test.servlet.test.doPost(test.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:20
6)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:688)
at java.lang.Thread.run(Thread.java:536)
net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session aft
er an exception occurs)
at net.sf.hibernate.impl.SessionImpl.checkId(SessionImpl.java:2641)
at net.sf.hibernate.impl.SessionImpl.flushEntity(SessionImpl.java:2465)
at net.sf.hibernate.impl.SessionImpl.flushEntities(SessionImpl.java:2458
)
at net.sf.hibernate.impl.SessionImpl.flushEverything(SessionImpl.java:22
60)
at net.sf.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.jav
a:1805)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1568)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1533)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1521)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1513)
at lyo.test.service.TradeDAOImpl.getTrades(TradeDAOImpl.java:30)
at lyo.test.servlet.test.doPost(test.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:20
6)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:688)
at java.lang.Thread.run(Thread.java:536)
Hibernate: insert into tradetest (trade_name, trade_desc, picture_file) values (
?, ?, ?)
Hibernate: select last_insert_id()
Hibernate: select trade0_.trade_id as trade_id, trade0_.trade_name as trade_name
, trade0_.trade_desc as trade_desc, trade0_.picture_file as picture_4_ from trad
etest trade0_
java.lang.UnsupportedOperationException: Blobs are not cacheable
at net.sf.hibernate.type.BlobType.disassemble(BlobType.java:60)
at net.sf.hibernate.impl.CacheEntry.disassemble(CacheEntry.java:38)
at net.sf.hibernate.impl.CacheEntry.<init>(CacheEntry.java:29)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2
214)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loade
r.java:315)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:305)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections
(Loader.java:133)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1521)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1513)
at lyo.test.service.TradeDAOImpl.getTrades(TradeDAOImpl.java:30)
at lyo.test.servlet.test.doPost(test.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:20
6)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:688)
at java.lang.Thread.run(Thread.java:536)
2004-9-4 19:39:02 org.apache.catalina.core.StandardContext reload
信息: Reloading this Context has started
MySQL 4.0.1 alpha
INFO
I want to save a picture using hibernate and jakarta FileUpload.
The picture is saved to DataBase successfully although I get these Exception! My code is:
Code:
boolean ismul=FileUpload.isMultipartContent(req);
List list=new ArrayList();
Trade trade=new Trade();
try{
if(ismul){
DiskFileUpload df=new DiskFileUpload();
List items=df.parseRequest(req);
Iterator iter=items.iterator();
while(iter.hasNext()){
FileItem item=(FileItem)iter.next();
if(item.isFormField()){
log.debug("Is normal field ===========");
String name=item.getString();
trade.setTrade_name(name);
}else{
log.debug("Is a file or picture!!!!! ");
byte[] b=item.get();
trade.setPicture_file(Hibernate.createBlob(b));
}
}
}else{
System.out.println("Not multipart! don't process... ...");
}
}catch(Exception e){
e.printStackTrace();
}
try{
Session s=HibernateUtil.currentSession();
Transaction t=s.beginTransaction();
Integer tradeid=(Integer)s.save(trade);
log.info("Save "+tradeid+" trade complete !!! -----------------------------");
t.commit();
HibernateUtil.closeSession();
I think the problem maybe is here :
Code:
byte[] b=item.get();
trade.setPicture_file(Hibernate.createBlob(b));
Because the item.get() method can return byte[]
, I have to convert the byte[] to Blob. So I use the
Code:
Hibernate.createBlob(byte[])
method. It correct?
If I change the code to:
Code:
InputStream in=item.getInputStream();
trade.setPicture_file(Hibernate.createBlob(in));
The Exception will only be
Code:
java.lang.UnsupportedOperationException: Blobs are not cacheable
Who can tell me why Hibernate give so Exception if I use Hibernate.createBlob(byte[])?