-->
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.  [ 4 posts ] 
Author Message
 Post subject: strange autoflush behavior
PostPosted: Tue Jan 13, 2004 2:33 pm 
Newbie

Joined: Tue Jan 13, 2004 1:57 pm
Posts: 2
I've noticed some strange behavior where on some occasions when i would add and then later remove objects from a collection. Sometimes the removed object would still be in the database. After some investigation I came up with a simple test case to show the problem.

<class name="test.ContentType"
table="content_type">

<id name="id" type="string" column="id">
<generator class="assigned" />
</id>

<set name="views" inverse="true" lazy="true"
cascade="all-delete-orphan">
<key column="content_type" />
<one-to-many class="test.View" />
</set>
</class>

<class name="test.View" table="view_master">
<composite-id>
<key-property name="viewName" column="view_name" />
<key-many-to-one name="contentType" column="content_type"
class="test.ContentType" />
</composite-id>

<many-to-one name="contentType" column="content_type"
class="test.ContentType"
cascade="save-update" insert="false" update="false" not-null="true"/>
</class>

String hql1 = "from View v where v.viewName = 'test'";
String hql2 = "from ContentType c where c.id = 'test'";

ContentType contentType = new ContentType();
contentType.setId("YY");
session.save(contentType);

View view = new View();
view.setViewName("test");

contentType.addView(view);
session.find(hql1);
contentType.removeView(view);
session.flush();

After running this code there is no view in the database as expected. However if i replace session.find(hql1) with session.find(hql2) and rerun the code I find a view inside the database.

The reason appears to be the autoflush code. In the first case it decides it needs to flush the session, but in the second case it doesn't. But it leaves the insert laying around for the session.flush().


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 3:36 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
can you show the code in addView and removeView

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 5:20 pm 
Newbie

Joined: Tue Jan 13, 2004 1:57 pm
Posts: 2
emmanuel wrote:
can you show the code in addView and removeView


Sure can

public class ContentType
{
private String id=null;
private Set views=new HashSet();

public void setId(String _id){
id=_id;
}
public String getId() {return id;}

public Set getViews() {
return views;
}

public void setViews(Set views) {
this.views = views;
}

public void addView(View view) {
view.setContentType(this);
views.add(view);
}

public void removeView(View view) {
views.remove(view);
}

public boolean equals(Object o) {
if(o == this) {
return true;
}
if(!(o instanceof ContentType)) {
return false;
}

ContentType p = (ContentType)o;

return p.getId().equals(this.id) ;

}

public int hashCode() {
int result = 17;
result = 37*result + id.hashCode();
return result;
}
}

public class View implements java.io.Serializable {
private String viewName;
private ContentType contentType;

public String getViewName() {
return viewName;
}

public void setViewName(String viewName) {
this.viewName = viewName;
}

public ContentType getContentType() {
return contentType;
}

public void setContentType(ContentType contentType) {
this.contentType = contentType;
}

public boolean equals(Object o) {
if(o == this) {
return true;
}
if(!(o instanceof View)) {
return false;
}

View view = (View)o;

return view.getViewName().equals(this.getViewName()) &&
view.getContentType().equals(this.getContentType());
}

public int hashCode() {
int result = 17;
result = 37*result + viewName.hashCode();
if(contentType != null)
result = 37*result + contentType.hashCode();
return result;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 6:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
public void removeView(View view) {
  views.remove(view);
  view.setContentType(null);
}

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.