org.hibernate.HibernateException [org.postgresql.u

2019-09-10 02:02发布

问题:

I know that may that question has been asked before, but the difference for my question is that I am using extended PersistenceUnit and also I am not the one who manages the transaction as the server is responsible for managing it.

BTW I am using JPA(2.1) with hibernate(4.3.10) provider, PostgreSQL(9.5) DB and liberty server

This is what I get in the browser

And here are my entities in simple 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;
    }

}

Any Idea how to solve this problem when getting organizations under geoArea by using >> geoArea.organizations in xhtml page?

回答1:

I know the question was asked before one month ago, but i want to share my solution as no one was answered here to my question.

BTW my solution was to use byte[] without @Lob over its getter so this will generate the column in postgre database table to be bytea and instead of oid column any more

Here are my code which i am using now to avoid Large objects may not be used in auto commit mode .... exception which was fired in browser and prevent the page from working as expected

@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