-->
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.  [ 2 posts ] 
Author Message
 Post subject: JPA read is forcing item into collection a second time.
PostPosted: Fri Aug 14, 2009 7:41 pm 
Newbie

Joined: Mon Mar 30, 2009 5:15 pm
Posts: 6
I'm doing some ETL work, moving users and groups from an app-specific db to a share-able user/group/login facility.

Unfortunately some user's emails are duplicated in the source. I'm keeping a HashSet of all emails and user names. With each new incoming record I check the caches for the name or email (adding if not found). If I have a cache hit, since it could be a user already in the db prior to the transfer I'm reading, JPA-style the user by name or email (both are enforced to be unique).

However when I hit the second occurance of an email in the source, on which I have previously called persist(), the read from the db throws
    javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not insert collection: [c.a.s.e.c.Group.users#101060]
    java.sql.BatchUpdateException: Batch entry 13 insert into group_user_join (group_id, user_id) values (101060, 101699) was aborted.
as though hibernate is adding the read-in user into the group to which the user was added earlier.

User has
Code:
    @ManyToMany(mappedBy="users", fetch = FetchType.LAZY)
    private Collection<Group> groups;
and Group has
Code:
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name="group_user_join",
               joinColumns={@JoinColumn(name="group_id")},
               inverseJoinColumns={@JoinColumn(name="user_id")})
    private Collection<User> users;

The lookup is
Code:
          Query ueLookup = em.createQuery("select u from User u where userName = :uname or emailAddress = :mail");
          ueLookup.setParameter("uname", username);
          ueLookup.setParameter("mail", email);
          try {
             foundUser = (User)ueLookup.getSingleResult();
          }

Does the setup look correct?
Am I diagnosing the exception correctly?
If so, any thoughts on how to get round the problem?
(I am catching the none-found and too-many exceptions)

Thanks.


Top
 Profile  
 
 Post subject: Re: JPA read is forcing item into collection a second time.
PostPosted: Mon Aug 17, 2009 5:24 pm 
Newbie

Joined: Mon Mar 30, 2009 5:15 pm
Posts: 6
I should have mentioned im my original post
    Hibernate-3.2, java-1.6, postgres-8.3.
    Using batch size of 1000. Statement.setMaxRows(1000) and hibernate.jdbc.batch_size=1000
    The groups were all read in or created in previous transactions
    There could be separate transactions between each occurrence of a given email.


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