-->
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.  [ 2 posts ] 
Author Message
 Post subject: DELETE, then INSERT statements every time I SELECT
PostPosted: Sun Feb 06, 2011 5:45 pm 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
Hi,

I am having an unexpected behavior when I do a SELECT query.

I have an entity with a collection of embeddable elements. These are the mappings:

Code:
@Entity
@Table(name = "document")
public class Document extends AbstractEntity {

   // Integer id in AbstractEntity
   private Integer typeId;
   private Set<PropertyValue> propertyValues = Sets.newHashSet();
   //...

   @Column(name = "document_type_id")
   public Integer getTypeId() {
      return typeId;
   }

   public void setTypeId(Integer typeId) {
      this.typeId = typeId;
   }   
   
   //...
   
   @ElementCollection(fetch = FetchType.EAGER)
   @CollectionTable(
      name = "property_values",
      joinColumns = {@JoinColumn(name = "document_id")}
   )
   public Set<PropertyValue> getPropertyValues() {
      return propertyValues;
   }
}

@Embeddable
public class PropertyValue {

   private Integer definitionId;
   private String value;
   
   @Column(name = "property_definition_id")
   public Integer getDefinitionId() {
      return definitionId;
   }
   
   public void setDefinitionId(Integer definitionId) {
      this.definitionId = definitionId;
   }
   
   @Column(name = "value")
   public String getValue() {
      return value;
   }
   
   public void setValue(String value) {
      this.value = value;
   }
}


This is the DDL:

Code:
CREATE TABLE document (

   id INT NOT NULL AUTO_INCREMENT,
   document_type_id INT NOT NULL,
   
   PRIMARY KEY (id),
   FOREIGN KEY (document_type_id) REFERENCES document_types (id)
   
) ENGINE = InnoDB;

CREATE TABLE property_values (

   document_id INT NOT NULL,
   property_definition_id INT NOT NULL,
   value VARCHAR(4000),
   
   PRIMARY KEY (document_id, property_definition_id),
   FOREIGN KEY (document_id) REFERENCES document (id),
   FOREIGN KEY (property_definition_id) REFERENCES property_definitions (id)
   
) ENGINE = InnoDB;


When I do a simple query:
Code:
String query = "FROM Document WHERE typeId = ? ORDER BY name";
return getHibernateTemplate().find(query, typeId);

Hibernate executes DELETE, then INSERT statements for every PropertyValue of every Document!
Here is the resulting SQL from the code above:

Hibernate: select document0_.id as id1_, ..., document0_.document_type_id as document8_1_ from document document0_ where document0_.document_type_id=? order by document0_.name
Hibernate: select propertyva0_.document_id as document1_1_0_, propertyva0_.property_definition_id as property2_0_, propertyva0_.value as value0_ from property_values propertyva0_ where propertyva0_.document_id=?
Hibernate: delete from property_values where document_id=? and property_definition_id=? and value=?
Hibernate: delete from property_values where document_id=? and property_definition_id=? and value=?
Hibernate: delete from property_values where document_id=? and property_definition_id=? and value=?
Hibernate: delete from property_values where document_id=? and property_definition_id=? and value=?
Hibernate: insert into property_values (document_id, property_definition_id, value) values (?, ?, ?)
Hibernate: insert into property_values (document_id, property_definition_id, value) values (?, ?, ?)
Hibernate: insert into property_values (document_id, property_definition_id, value) values (?, ?, ?)
Hibernate: insert into property_values (document_id, property_definition_id, value) values (?, ?, ?)


What could be the problem?


Top
 Profile  
 
 Post subject: Re: DELETE, then INSERT statements every time I SELECT
PostPosted: Sun Feb 13, 2011 10:20 am 
Newbie

Joined: Sun Mar 07, 2010 9:05 am
Posts: 13
Any thoughts on the problem?

Am I doing something wrong regarding the collection mapping?


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