Thank you
I have read the two links, but they don't really answer my concerns, that's why I created a small test case to show the problem.
I have two entities User and UserContentBinary, below is the java code and hibernate mapping:
Quote:
//------------------------------
public class User {
private Integer id;
private String name;
private Integer version;
private Integer parentId;
private UserContentBinary content;
public User(Integer id, String name){
this.id = id;
this.name = name;
}
public User(String name){
this.name = name;
}
public User(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public UserContentBinary getContent() {
return content;
}
public void setContent(UserContentBinary content) {
this.content = content;
}
}
//------------------------------
public class UserContentBinary {
private Integer id;
private byte[] content;
private Integer version;
private User user;
public UserContentBinary(){
}
public UserContentBinary(byte[] ct){
this.content = ct;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
//------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.test.dao.entity.User" table="TEST_USER" lazy="false">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="foreign">
<param name="property">content</param>
</generator>
</id>
<version name="version" type="java.lang.Integer">
<column name="VERSION" not-null="true" />
</version>
<property name="name" type="java.lang.String">
<column name="name" not-null="false" length="300"/>
</property>
<property name="parentId" type="java.lang.Integer">
<column name="parent_id" not-null="false" />
</property>
<one-to-one name="content" class="com.my.test.dao.entity.UserContentBinary" cascade="all" constrained="true"/>
</class>
</hibernate-mapping>
//--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.my.test.dao.entity.UserContentBinary" table="TEST_USER_CONTENT" lazy="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<version name="version" type="java.lang.Integer">
<column name="VERSION" not-null="true" />
</version>
<one-to-one name="user" class="com.my.test.dao.entity.User"/>
<property name="content" type="binary" lazy="true">
<column name="CONTENT"/>
</property>
</class>
</hibernate-mapping>
Now if the userContentBinary is lazy loading, and I try to load a user, and then get the userContentBinary, I will get " java.sql.SQLException: Stream has already been closed". But if I set it to false, it works fine, and I need to keep it lazy.
Can any one help me to solve this issue
Thank you