org.hibernate.HibernateException [org.postgresql.util.PSQLException:大对象可能无法在自动提交模式下使用。]

问题描述 投票:0回答:1

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

BTW我正在使用JPA(2.1)与hibernate(4.3.10)提供程序,PostgreSQL(9.5)数据库和自由服务器

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

以下是我在简单视图中的实体

@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下获取组织来解决这个问题?

java postgresql hibernate jpa jpa-2.1
1个回答
0
投票

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

BTW我的解决方案是在其getter上使用不带@Lob的byte []因此这将生成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;
     }

}

注意>>如果在使用@Lob之前已经生成oid列,如果在persistence.xml中使用hibernate.hbm2ddl.auto = update,则必须手动删除oid列,因为它无法将列从oid更新为类型bytea并且它会考虑oid很好但是确定你可以使用hibernate.hbm2ddl.auto = create-drop来删除并再次创建表,这将生成bytea类型的列

© www.soinside.com 2019 - 2024. All rights reserved.