gpt4 book ai didi

java - hibernate - Blob

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:10 25 4
gpt4 key购买 nike

我尝试更新数据库中的表“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();
}

图像类:

   @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" />

库:

enter image description here

错误:

Hibernate: insert into IMAGE (IMAGE, NAME) values (?, ?) Exception in thread "main" java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy13.save(Unknown Source) at com.journaldev.hibernate.main.ImageTest.main(ImageTest.java:35) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356) ... 2 more Caused by: java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$5$1.doBind(BlobTypeDescriptor.java:151) at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$2$1.doBind(BlobTypeDescriptor.java:107) at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286) at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281) at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:56) at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2843) at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2818) at org.hibernate.persister.entity.AbstractEntityPersister$4.bindValues(AbstractEntityPersister.java:3025) at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558) at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98) at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490) at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195) at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179) at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214) at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324) at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288) at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194) at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209) at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194) at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707) at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702) ... 7 more

最佳答案

以下答案取自 this post

AbstractMethodError 意味着您的 JDBC 驱动程序的PreparedStatements 未实现 setBlob(int, InputStream, long)。

使用较旧的 setBlob(int, Blob) 或更新驱动程序(Connector/J 5.1 实现 Jdbc 4.0,这应该是 setBlob(int, InputStream, long) 所需的)

关于java - hibernate - Blob ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27403692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com