These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Could not use lazy loading with Oracle 10g
PostPosted: Tue Jan 27, 2009 6:03 pm 
Newbie

Joined: Thu Aug 07, 2008 3:50 pm
Posts: 4
Hibernate version:3.3.1

Hi
I'm using hibernate 3.3.1 with Oracle 10g and Weblogic 10, and the driver oracle.jdbc.driver.OracleDriver in ojdbc14.jar.

For some reasons, my application has passed all the test, when I was using SQLServer DB. But I start receiving the error "Stream has already been closed", when I switched the DB to Oracle 10g Express.
After investigating the code, I believe that the problem is the lazy loading, for some reason, that I could not understand, the driver could not load the content of a document (LONG RAW) lazily, because the connection was closed.

As I said in the beginning, this issue did not appear with SQLServer, also when I change the mapping, and I removed the lazy loading, the tests were successful.

Can anyone help me by explaining to me , or by pointing me to some documents that explain in details how lazy loading works, or how proxy works. I looked already in the books "Hibernate in Action" and "Java Persistence Hibernate", but could not find satisfying answers.

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2009 7:15 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
This might help: http://www.orafaq.com/usenet/comp.datab ... 7/0593.htm

or this: http://www.jroller.com/agrebnev/entry/h ... ips_tricks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 4:24 pm 
Newbie

Joined: Thu Aug 07, 2008 3:50 pm
Posts: 4
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 5:01 pm 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
It would help if you could post the code how you access the objects and what SQL results.

one-to-one is awkward for me because i have seen it being eager loaded even though it shouldnt be, you might check the order of your sqls for that.

To point you to some more reading, 13.1.6 Lazy loading with interception of Java Persistence with Hibernate states that you need bytecode instruction via a separate ant target for lazy property loading to work.

If you want to try something out (no guarantees):
remove the lazy=true from UserContentBinary.content and the class itself
remove the one-to-one association on both sides
at the User mapping add:
Code:
<many-to-one name="content" class="com.my.test.dao.entity.UserContentBinary" cascade="all" column="ID" insert="false" update="false">

this should make sure that the whole UserContentBinary object loaded lazy including the content

hope it works


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 6:35 pm 
Newbie

Joined: Thu Aug 07, 2008 3:50 pm
Posts: 4
Hi
I don't understand why the first mapping did not work with Oracle, but the one that you have suggested solved my problem.

thank you very much.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 1:48 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
You're welcome

As it helped, please rate the answer(s)

thank you too


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.