我知道以前可能会问过这个问题,但我的问题的不同之处在于我使用扩展的PersistenceUnit 而且我不是管理事务的人,因为服务器负责管理它。
BTW我正在使用JPA(2.1)和hibernate(4.3.10)提供程序,PostgreSQL(9.5)DB和自由服务器
以下是我在简单视图中的实体
@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;
}
}
任何想法如何通过使用&gt;&gt;获取geoArea下的组织来解决此问题xhtml页面中的geoArea.organizations?
答案 0 :(得分:0)
我知道这个问题是在一个月前提出来的,但我想分享我的解决方案,因为我的问题没有人回答。
顺便说一句,我的解决方案是使用 byte [] 而不使用 @Lob 来获取getter,这样就会生成postgre数据库表中的列为 bytea 而不是 oid 列
以下是我现在使用的代码,以避免大型对象可能无法在自动提交模式中使用.... 在浏览器中触发的异常并阻止页面按预期工作< / p>
@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;
}
}
注意&gt;&gt;如果你在使用 @Lob 之前已经生成 oid 列,那么你必须手动删除 oid 列,如果你在你的 hibernate.hbm2ddl.auto = update 中使用了persistence.xml因为它无法帮助将 oid 中的列更新为 bytea ,并且它会认为 oid 很好但是确定你可以使用 hibernate.hbm2ddl.auto = create-drop 再次删除并创建表格,这将生成 bytea 类型
列