如何使用hibernate在postgres数据库中存储文件?

时间:2016-01-22 05:22:07

标签: java database hibernate postgresql bytea

我是Hibernate的新手。 我需要将文件存储在postgres中,列的数据类型为bytea。 我在互联网上四处寻找这个案例的工作演示,但我无法找到任何。

Hibernate映射:

<property name="fileData" type="binary">
   <column name="fileData" not-null="true" />
</property>

POJO:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [fileData] in table [nmondata]; found [bytea (Types#BINARY)], but expecting [binary(255) (Types#VARBINARY)]
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateColumnType(SchemaValidatorImpl.java:105)
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:92)
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:473)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at com.psl.perf.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:34)
    at com.psl.perf.DAO.NmonReportFileDaoImpl.save(NmonReportFileDaoImpl.java:15)
    at com.psl.perf.service.DataService.saveNmonCSV(DataService.java:249)
    at com.psl.perf.main.Main2.main(Main2.java:8)
Jan 22, 2016 12:26:18 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:postgresql://x.x.x.x:5432/perf]
Exception in thread "main" java.lang.NullPointerException
    at com.psl.perf.DAO.NmonReportFileDaoImpl.save(NmonReportFileDaoImpl.java:17)
    at com.psl.perf.service.DataService.saveNmonCSV(DataService.java:249)
    at com.psl.perf.main.Main2.main(Main2.java:8)

表:



    private byte[] fileData;
        public byte[] getFileData() {
            return fileData;
        }
        public void setFileData(byte[] fileData) {
            this.fileData = fileData;
        }

错误:


    create table nmonData(id int, buildNumber int, path text, fileName text, fileData bytea);

有人可以共享工作演示或链接吗?

  

这对我有用   评论<property name="hbm2ddl.auto">validate</property>   hibernate配置文件为我做了伎俩,但我现在很困惑   关于什么应该是列和成员变量的数据类型   用于存储文件的java。

1 个答案:

答案 0 :(得分:2)

我假设您需要将文件内容存储到PG bytea列中。

  

PostgreSQL DDL

CREATE TABLE data_object
(
  obj_id bigserial NOT NULL,
  obj_name character(30),
  obj_file bytea,
  CONSTRAINT data_object_pkey PRIMARY KEY (obj_id)
)
  

Java对象

@Entity
@Table (name = "data_object")
public class DataObject {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    @Column(name = "obj_id")
    private Long id;

    @Column(name = "obj_name")
    private String name;

    @Column(name = "obj_file")
    private byte[] file;

    // getters, setters, hasCode, equals, toString
  

驱动程序代码

byte[] data = new byte[FILE_SIZE];
// read file contents into data
DataObject obj = new DataObject();
obj.setFile(data);
// save obj using your hibernate session

我使用的是Hibernate 4.3.7和PG 9.4.1