我尝试更新表格" image"在我的数据库中插入blob类型。
public static void main (String args[]) throws FileNotFoundException{
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
File image = new File("c://img//button.png");
System.out.println("file esistente ? "+image.exists());
InputStream in = new FileInputStream(image);
Blob blob = Hibernate.getLobCreator(session)
.createBlob(in, image.length());
Image imageClass = new Image();
imageClass.setImage(blob);
imageClass.setName("blobTry");
session.save(imageClass);
session.getTransaction().commit();
sessionFactory.close();
}
的imageCLASS:
@Entity
@Table(name = "IMAGE")
public class Image {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private int id;
@Column(name = "NAME")
private String name;
@Column(name = "IMAGE")
private Blob image;
/* ... auto generated getters / setters */
}
Hibernate用户映射:
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.journaldev.hibernate.model.Image" />
LIB:
错误:
Hibernate:插入IMAGE(IMAGE,NAME)值(?,?)异常 线程&#34;主要&#34; java.lang.reflect.UndeclaredThrowableException at com.sun.proxy。$ Proxy13.save(未知来源)at com.journaldev.hibernate.main.ImageTest.main(ImageTest.java:35)引起 by:java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at java.lang.reflect.Method.invoke(未知来源)at org.hibernate.context.internal.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356) ... 2更多引起:java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava / IO / InputStream的;Ĵ)V 在 org.hibernate.type.descriptor.sql.BlobTypeDescriptor $ 5 $ 1.doBind(BlobTypeDescriptor.java:151) 在 org.hibernate.type.descriptor.sql.BlobTypeDescriptor $ 2 $ 1.doBind(BlobTypeDescriptor.java:107) 在 org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90) 在 org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286) 在 org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281) 在 org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56) 在 org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843) 在 org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2818) 在 org.hibernate.persister.entity.AbstractEntityPersister $ 4.bindValues(AbstractEntityPersister.java:3025) 在 org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032) 在 org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558) 在 org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98) 在org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490) 在 org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195) 在 org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179) 在 org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214) 在 org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324) 在 org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288) 在 org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) 在 org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) 在 org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209) 在 org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) 在 org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194) 在 org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) 在 org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 在org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715) 在org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)... 7 更
答案 0 :(得分:1)
AbstractMethodError意味着你的JDBC驱动程序的PreparedStatements没有实现setBlob(int,InputStream,long)。
使用旧的setBlob(int,Blob)或更新驱动程序(Connector / J 5.1实现Jdbc 4.0,这应该是setBlob所需的内容(int,InputStream,long))
答案 1 :(得分:0)
我将数据文件的类型更改为bean&#34; image&#34; Blob到byte []现在可以正常工作