Hello!
Seems like I have subj. Please not the following in my mappings:
there is a non-hibernate managed many-to-one relation between Node2 and Data2 entities using dataRef property (I have a number of reasons to for it). The query is intended to delete orphaned Data2 entities. Note that both Node2 and Data2 have an id property mapped to "ID" column. This causes a conflict in generated SQL: ...where DATA_REF=ID) Actually seems like my PostgreSQL takes TREE_NODE.ID here and this leads to deletion of all Data2 records.
The solution: if I change the column name of one of the id properies to something other than "ID", no conflict happens and the produced SQL works fine.
Is it a bug or am I missing something here?
Thanks.
Hibernate version:
3.1.3
Mapping documents:
@Entity
@Table(name = "TREE_NODE")
public class Node2 {
private Long id;
private List<String> tokens = new LinkedList<String>();
private Long dataRef;
...
@CollectionOfElements(fetch = FetchType.EAGER)
@JoinTable(name = "TOKENS", joinColumns = @JoinColumn(name = "NODE_FK"))
@Column(name = "TOKEN")
@IndexColumn(name = "POSITION")
public List<String> getTokens() {
return tokens;
}
public void setTokens(List<String> tokens) {
this.tokens = tokens;
}
@Id
@GeneratedValue
@Column(name = "ID")
@Index(name = "NODE_ID_INDEX", columnNames = { "ID" })
public Long getId() {
return id;
}
protected void setId(Long id) {
this.id = id;
}
@Transient
public int getDepth() {
return getTokens().size();
}
@Transient
public String getName() {
int depth = getDepth();
if (depth == 0)
return null;
return getTokens().get(depth - 1);
}
@Basic
@Column(name = "DATA_REF")
@Index(name = "NODE_DATA_REF_INDEX", columnNames = { "DATA_REF" })
public Long getDataRef() {
return dataRef;
}
public void setDataRef(Long dataRef) {
this.dataRef = dataRef;
}
}
@Entity
@Table(name = "DATA_TABLE")
public class Data2 {
private Long id;
private byte[] data;
@Basic
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
@Id
@GeneratedValue
@Column(name = "ID")
@Index(name = "DATA_TABLE_ID_INDEX", columnNames = { "ID" })
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
Code between sessionFactory.openSession() and session.close():
The query:
"delete Data2 d where 0=(select count(*) from Node2 n where n.dataRef = d.id)"
The generated SQL (show_sql=true):
delete from DATA_TABLE where 0=(select count(*) from TREE_NODE node2x1_ where DATA_REF=ID)
Name and version of the database you are using:
PostgreSQL 8.0
|