使用 hibernate 将 blob 保存到数据库

发布于 2024-08-18 06:27:14 字数 3091 浏览 3 评论 0原文

我尝试使用 blob 和 hibernate3 将文件保存到 MySQL。但我一直都有 java.lang.UnsupportedOperationException:无法通过创建会话来操作 Blob org.hibernate.lob.BlobImpl.excep(BlobImpl.java:127)

这里有一些代码。

package com.uni.domain;

public class File extends Identifier {

    private byte[] data;
    private String contentType;

    public byte[] getData() {
        return data;
    }

    public File() {}

    public void setData(byte[] photo) {
        this.data = photo;
    }

    public boolean isNew() {
        return true;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

package com.uni.domain;

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

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.sql.*;
import java.util.Arrays;

public class PhotoType implements UserType {

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

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

    public boolean equals(Object o, Object o1) throws HibernateException {
        return Arrays.equals((byte[]) o, (byte[]) o1);
    }

    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();
    }

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

    public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {
        st.setBlob(index, Hibernate.createBlob((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 Serializable disassemble(Object o) throws HibernateException {
        return null;  .
    }

    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return null;  .
    }

    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return null;  .
    }

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.uni.domain">
    <class name="com.uni.domain.File">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="data" type="com.uni.domain.FleType"/>
        <property name="contentType"/>
    </class>
</hibernate-mapping>

请帮助我。我哪里错了?

I tried save file to MySQL using blob with hibernate3. But I always have
java.lang.UnsupportedOperationException: Blob may not be manipulated from creating session
org.hibernate.lob.BlobImpl.excep(BlobImpl.java:127)

Here some code.

package com.uni.domain;

public class File extends Identifier {

    private byte[] data;
    private String contentType;

    public byte[] getData() {
        return data;
    }

    public File() {}

    public void setData(byte[] photo) {
        this.data = photo;
    }

    public boolean isNew() {
        return true;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

package com.uni.domain;

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

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.sql.*;
import java.util.Arrays;

public class PhotoType implements UserType {

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

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

    public boolean equals(Object o, Object o1) throws HibernateException {
        return Arrays.equals((byte[]) o, (byte[]) o1);
    }

    public int hashCode(Object o) throws HibernateException {
        return o.hashCode();
    }

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

    public void nullSafeSet(PreparedStatement st, Object value, int index)
            throws HibernateException, SQLException {
        st.setBlob(index, Hibernate.createBlob((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 Serializable disassemble(Object o) throws HibernateException {
        return null;  .
    }

    public Object assemble(Serializable serializable, Object o) throws HibernateException {
        return null;  .
    }

    public Object replace(Object o, Object o1, Object o2) throws HibernateException {
        return null;  .
    }

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.uni.domain">
    <class name="com.uni.domain.File">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="data" type="com.uni.domain.FleType"/>
        <property name="contentType"/>
    </class>
</hibernate-mapping>

Help me please. Where I’m wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2024-08-25 06:27:14

如果你查看BlobImpl的hibernate源代码,你会发现getBytes方法没有实现,并且只转发到异常:

  69    /**
   70        * @see java.sql.Blob#getBytes(long, int)
   71        */
   72       public byte[] getBytes(long pos, int len) throws SQLException {
   73           excep(); return null;
   74       }

 126    private static void excep() {
  127           throw new UnsupportedOperationException("Blob may not be manipulated from creating session");
  128       }
  129   

而不是 getBytes() 你应该使用 getBinaryStream()

示例用法:

    InputStream inputStream = getContent().getBinaryStream();
    int expected = inputStream.available();
    byte[] contents = new byte[expected];
    int length = inputStream.read(contents);
    if(length != expected)
            throw new IOException("Could not read full input");

If you have a look at the hibernate source code for BlobImpl, you'll find that the method getBytes is not implemented, and only forward to exception :

  69    /**
   70        * @see java.sql.Blob#getBytes(long, int)
   71        */
   72       public byte[] getBytes(long pos, int len) throws SQLException {
   73           excep(); return null;
   74       }

 126    private static void excep() {
  127           throw new UnsupportedOperationException("Blob may not be manipulated from creating session");
  128       }
  129   

instead of getBytes() you should use getBinaryStream()

Example usage:

    InputStream inputStream = getContent().getBinaryStream();
    int expected = inputStream.available();
    byte[] contents = new byte[expected];
    int length = inputStream.read(contents);
    if(length != expected)
            throw new IOException("Could not read full input");
小嗷兮 2024-08-25 06:27:14

如果您将其与 Spring 一起使用,则 Spring 对其有内置支持。 DefaultLobHandler< /a> 这是让它工作的配置问题。 示例代码供参考,与旧版本相比没有太大变化春天。

If you are using it with Spring, there is in-built support in Spring for it. DefaultLobHandler It's a matter of config to get it working. Sample Code to refer, not much has changed from the older versions of Spring.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文