java - org.hibernate.HibernateException [org.postgresql.util.PSQLException : Large Objects may not be used in auto-commit mode.]

标签 java postgresql hibernate jpa jpa-2.1

我知道这个问题可能之前已经被问过,但我的问题的不同之处在于我正在使用扩展的PersistenceUnit,而且我也不是管理事务的人,因为服务器负责用于管理它。

顺便说一句,我正在将 JPA(2.1) 与 hibernate(4.3.10) 提供程序、PostgreSQL(9.5) DB 和 liberty 服务器一起使用

这是我在浏览器中得到的 enter image description here

这是我的简单 View 中的实体

@Entity
public class GeoArea{
      private Integer id;//Auto Generated
      private String name;

      private Set<TourismOrganization> organizations;

      //getter and setter methods

      @ManyToMany(mappedBy = "geoAreas")
      public Set<TourismOrganization> getOrganizations() {
          return organizations;
      }

       public void setOrganizations(Set<TourismOrganization> organizations) {
           this.organizations = organizations;
       }
}
<小时/>
@Entity
public class TourismOrganization{
      private Integer id;//Auto Generated
      private String name;

      private BinaryContent logo;
      private Set<TourismGeoArea> geoAreas;

      //other getter and setter methods

      @ManyToMany
      public Set<TourismGeoArea> getGeoAreas() {
          return geoAreas;
      }

      public void setGeoAreas(Set<TourismGeoArea> geoAreas) {
         this.geoAreas = geoAreas;
      }

      @OneToOne(fetch = FetchType.EAGER, optional = true, cascade = { CascadeType.REMOVE }, orphanRemoval = true)
      public BinaryContent getLogo() {
          return logo;
      }

      public void setLogo(BinaryContent logo) {
          this.logo = logo;
      }
}
<小时/>
@Entity
public class BinaryContent{
    private Integer id;//Auto Generated
    private String contentType;

    private byte[] data;

    //other getter and setter methods

    @Lob
    @Column(length = 16000000) // This should generate a medium blob
    @Basic(fetch = FetchType.LAZY) // I've read this is default, but anyway...
    public byte[] getData() {
        return data;
    }

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

}

在 xhtml 页面中使用 >> geoArea.organizations 获取 geoArea 下的组织时,您知道如何解决此问题吗?

最佳答案

我知道这个问题是在一个月前提出的,但我想分享我的解决方案,因为这里没有人回答我的问题。

顺便说一句,我的解决方案是在其 getter 上使用 byte[] 而没有 @Lob ,这样这将在 postgre 数据库表中生成列 bytea 不再是 oid

这是我现在使用的代码,以避免在浏览器中触发的大对象可能无法在自动提交模式下使用......异常并阻止页面按预期工作

@Entity
public class BinaryContent{
     private Integer id;//Auto Generated
     private String contentType;

     private byte[] data;

     //other getter and setter methods

     //@Lob >> remember that i am not using it anymore to avoid the exception on the browser
     @Column(length = 16000000) // This should generate a medium blob
     @Basic(fetch = FetchType.LAZY) // I've read this is default, but anyway...
     public byte[] getData() {
          return data;
     }

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

}

Note >> u must delete oid column by hand if it was already generated before using @Lob if you are using hibernate.hbm2ddl.auto=update in your persistence.xml as it will not help to update the column from oid to be type bytea and it will consider oid is fine but sure you can use hibernate.hbm2ddl.auto=create-drop to drop and create the tables again and this will generate the column in bytea type

关于java - org.hibernate.HibernateException [org.postgresql.util.PSQLException : Large Objects may not be used in auto-commit mode.],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38511205/

相关文章:

java - 文件开头的附加 "2000"字符串([32 30 30 30] 字节)

java - 无法在java中将大文件(>1gb)转换为字节数组

api - 如何向 API 客户端提供 1,000,000 个数据库结果?

java - 如何通过 hibernate 访问 pl/sql 过程中的输出参数

java - 是否应该更改 hibernate 生成的代码以满足需要?

可以通过拆分(分块)上传超大文件的 Javascript/flash 库

java - 如何添加带有触发器的作业以运行 Quartz 调度程序实例而无需重新启动服务器

postgresql - 如何通过添加下划线前缀来重命名 PostgreSQL 表?

postgresql - 创建高可用且高可用的数据库

java - 配置 Hibernate 以使用 Oracle 的 SYS_GUID() 作为主键