-->
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: Problem with @Where annotations on collections during insert
PostPosted: Mon Apr 22, 2013 6:20 am 
Newbie

Joined: Mon Apr 22, 2013 5:37 am
Posts: 2
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.


Top
 Profile  
 
 Post subject: Re: Problem with @Where annotations on collections during insert
PostPosted: Tue Apr 23, 2013 7:51 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
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.


That's excpected behaviour. The @Where annotations are designed for read-operations only.

Quote:
insert into LINK (link_id, source_thing_id, destination_thing_id)

which fails because the LINK type is mandatory.


How did you map class Thing ?


Top
 Profile  
 
 Post subject: Re: Problem with @Where annotations on collections during insert
PostPosted: Tue Apr 23, 2013 12:02 pm 
Newbie

Joined: Mon Apr 22, 2013 5:37 am
Posts: 2
I did not know the Where annotation was for readonly, I saw the correct "One-shot delete" statement and assumed it was also for write operations.
Do I have to force the filter using @SQLInsert/Update/Delete in that case ?
I tried that and it seems to work but I'd rather let Hibernate handle that automatically.

The mapping for class Link (I assume that was what you're asking) is something like this :
Code:
@Entity
@Table(name="LINK")
public class Link{
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="link_id", unique=true, nullable=false)
   private int linkId;

   @Column(name="source_thing_id", nullable=false)
   private int sourceThingId;

   @Column(name="destination_thing_id", nullable=false)
   private int destinationThingId;

   //bi-directional many-to-one association to Type
   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="type", nullable=false)
   private Type type;


The DB model is kept very simple and basic, with no foreign keys, so this LINK join table is kinda standalone.

Thanks for your input.


Top
 Profile  
 
 Post subject: Re: Problem with @Where annotations on collections during insert
PostPosted: Wed Apr 24, 2013 8:28 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
That what you are trying to reach is a so called 'Ternary association' where your class "Type" act as intermediate class.
They have to be mapped different than you are doing.
To explain it all here is to much effort, see Book "Java Persistence with Hibernate" chapter ternary associations.


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.