-->
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.  [ 3 posts ] 
Author Message
 Post subject: SELECT then DELETE then INSERT with @CollectionOfElements
PostPosted: Mon Mar 09, 2009 11:37 am 
Newbie

Joined: Wed Jul 09, 2008 7:08 am
Posts: 7
Location: Sweden
Hi

I have entity with a @CollectionOfElements-field.
When I run a query (using a regular query.getResultList()), hibernate first does a SELECT on both tables, then DELETEs all the rows in the JoinTable and then INSERTS the same rows!

What's going on here? Any ideas....?
thanks!

Entity:
Code:
@Entity
@NamedQuery(
       name="findUserByEmail",
       query="FROM PortalUser p WHERE p.email = :email"
   )
@SequenceGenerator(name = "KPANV_SEQUENCE", sequenceName = "KP_ANVANDARID_SQ", allocationSize = 1)
@Table(name = "LGAB_KPANVANDARE")
public class PortalUser {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "KPANV_SEQUENCE")
@Column(name = "ANVANDARID")
private long id;

...

@CollectionOfElements
@JoinTable(name = "LGAB_KPBEVAKADSTATUS", joinColumns = { @JoinColumn(name = "ANVANDARID") })
private Set<SubscribedStatus> statuses;



Generated SQL:

Code:
16:34:12,240 INFO  [STDOUT] Hibernate:
    select
        portaluser0_.ANVANDARID as ANVANDARID95_,
        portaluser0_.EMAIL as EMAIL95_,
        portaluser0_.REGDATBEVAKN as REGDATBE3_95_,
        portaluser0_.SMS as SMS95_,
        portaluser0_.BEVAKNVIASMSJN as BEVAKNVI5_95_,
        portaluser0_.REGDATRPTPRENUM as REGDATRP6_95_
    from
        LGAB_KPANVANDARE portaluser0_
    where
        portaluser0_.EMAIL=?
16:34:12,271 INFO  [STDOUT] Hibernate:
    select
        statuses0_.ANVANDARID as ANVANDARID0_,
        statuses0_.KPSTATUS as KPSTATUS0_
    from
        LGAB_KPBEVAKADSTATUS statuses0_
    where
        statuses0_.ANVANDARID=?
16:34:12,287 INFO  [STDOUT] Hibernate:
    delete
    from
        LGAB_KPBEVAKADSTATUS
    where
        ANVANDARID=?
        and KPSTATUS=?
16:34:12,287 INFO  [STDOUT] Hibernate:
    delete
    from
        LGAB_KPBEVAKADSTATUS
    where
        ANVANDARID=?
        and KPSTATUS=?
16:34:12,302 INFO  [STDOUT] Hibernate:
    insert
    into
        LGAB_KPBEVAKADSTATUS
        (ANVANDARID, KPSTATUS)
    values
        (?, ?)
16:34:12,302 INFO  [STDOUT] Hibernate:
    insert
    into
        LGAB_KPBEVAKADSTATUS
        (ANVANDARID, KPSTATUS)
    values
        (?, ?)       


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 12:18 pm 
Newbie

Joined: Wed Jul 09, 2008 7:08 am
Posts: 7
Location: Sweden
Found this which gives a bit more information:
Quote:
The process of erasing the entire collection and reinsertion is quite a performance penalty, we can overcome this penalty by adding an index column to the collection. The index column identifies the position of an element within the collection. The position of the element in a collection can be used by Hibernate to match a database row and a java object. Adding an index column is done using the @IndexColumn annotation.
link

So does this mean I actually need to add a column to the LGAB_KPBEVAKADSTATUS table in the database (plus a sequence to increment this IndexColumn, using Oracle)?

Neither is a very good option so I might have to resort to native sql here.
Or am I missing something?

/Janne


Top
 Profile  
 
 Post subject: Re: SELECT then DELETE then INSERT with @CollectionOfElements
PostPosted: Wed Jun 17, 2009 4:53 pm 
Newbie

Joined: Wed Jun 17, 2009 4:49 pm
Posts: 1
I just had the same problem in my development.
I just changed it to a bag implementation and successfully avoid that.
like

Code:
Collection<SubscribedStatus> statuses = new ArrayList<SubscribedStatus>();


This will lose the uniqueness of a set implementation. but it can be compensated through other business logic.


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