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?