Hello
I'm using annotations to use one table for two collection on an entity.
It's working as expected when I read each collection (the @Where filter is applied correctly), but when a collection is modified (then detected as dirty), the hibernate automatic inserts/updates fail, as they don't seem to use the @Where filter.
I use "thing" objects that can be "linked" with different types of link.
Here are my Database objects:
Quote:
- THING (thing_id, name, ...)
- LINK (link_id, source_thing_id, destination_thing_id, type)
Everything is 'NOT NULL'.
Here are my Java declarations:
Code:
@Entity
@Table(name = "THING")
public class Thing{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "thing_id", unique = true, nullable = false)
private int thing_id;
@Column(name = "name", nullable = false)
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "link", joinColumns = { @JoinColumn(name = "source_thing_id") }, inverseJoinColumns = { @JoinColumn(name = "destination_thing_id") })
@WhereJoinTable(clause="type = 1")
private List<Thing> thingsType1;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "link", joinColumns = { @JoinColumn(name = "source_thing_id") }, inverseJoinColumns = { @JoinColumn(name = "destination_thing_id") })
@WhereJoinTable(clause="type = 2")
private List<Thing> thingsType2;
...
}
Now, when I read the collections thingsType1 and thingsType2, I get the correct results (correctly filtered "select * from THING where type = ?").
But if I edit one of the collections, Hibernate will try to insert into the LINK table without using the @Where annotation:
Quote:
insert into LINK (link_id, source_thing_id, destination_thing_id)
which fails because the LINK type is mandatory.
How can I force Hibernate to use the @Where filter on insert/update/delete ?
I see that the generated static SQL is invalid, when loading the entities in the logs too:
Quote:
Static SQL for collection: com.xxxxx.entities.Thing.thingsType1
Row insert: insert into LINK (source_thing_id, destination_thing_id) values (?, ?)
Row update: update LINK set destination_thing_id=? where source_thing_id=? and destination_thing_id=?
Row delete: delete from LINK where source_thing_id=? and destination_thing_id=?
One-shot delete: delete from LINK where source_thing_id=? and ( type = 1)
Static SQL for collection: com.xxxxx.entities.Thing.thingsType2
Row insert: insert into LINK (source_thing_id, destination_thing_id) values (?, ?)
Row update: update LINK set destination_thing_id=? where source_thing_id=? and destination_thing_id=?
Row delete: delete from LINK where source_thing_id=? and destination_thing_id=?
One-shot delete: delete from LINK where source_thing_id=? and ( type = 2)
Static select for collection com.xxxxx.entities.Thing.thingsType1: select [...] from LINK link0_ left outer join thing thing1_ on link0_.destination_thing_id=thing1_.thing_id where ( link0_.type = 1) and link0_.source_thing_id=?
...
As you can see, only the "select" and "One-Shot delete" are using the correct filter.
I'm using Hibernate 3.3.2.GA
Thanks in advance for any help.