Back Forum Reply New

Issue with JpaTemplate deleting all records in Table

I have a User Object that I am trying to delete from my database. I have a Unit Test and DBUnit case that seeds three (3) records into my database.

When I do a findAll, I get 3 records.

I then delete(T) but all 3 records are deleted, not just the one I specified.

I have another domain Object (Store), that works just fine, but for some reason my User does not.
NOTE: Create and update on my User works fine as per my Unit Tests.

Here is my User:

Code:
package com.baselogic.domain;

[omitted]

@Entity
@Table(name = quot;usersquot;)
@EntityListeners({UserDetailsObjectListener.class})
public class User    extends StringIdentifiedObjectlt;Stringgt; {
   private static final long serialVersionUID = 3832626162173359488L;   private String username;   private String password;   private String firstName;   private String middleName;   private String lastName;   private Address address;   private String email;   private int enabled;   private Listlt;Authoritygt; authorities = new LinkedListlt;Authoritygt;();          public User() {       this.enabled = 1;   }
   public User(String username, String password, int enabled) {       setId(username);       this.username = username;       this.password = password;       this.enabled = enabled;   }
   @Column(name = quot;usernamequot;, nullable = true, length = 50)   public String getUsername() {       return username;   }
   public void setUsername(String username) {       this.username = username;   }
   public String getPassword() {       return password;   }
   public void setPassword(String password) {       this.password = password;   }
   @Column(       name = quot;first_namequot;,       nullable = false,       length = 50   )   public String getFirstName() {       return firstName;   }
   public void setFirstName(String firstName) {       this.firstName = firstName;   }
   @Column(       name = quot;middle_namequot;,       nullable = true,       length = 50   )   public String getMiddleName() {       return middleName;   }
   public void setMiddleName(String middleName) {       this.middleName = middleName;   }
   @Column(       name = quot;last_namequot;,       nullable = false,       length = 50   )   public String getLastName() {       return lastName;   }
   public void setLastName(String lastName) {       this.lastName = lastName;   }
   @Column(       name = quot;emailquot;,       nullable = true,       length = 50   )   public String getEmail() {       return email;   }
   public void setEmail(String email) {       this.email = email;   }
   @JoinColumn(       name = quot;address_fkquot;,       nullable = true   )   @OneToOne(       fetch = FetchType.EAGER,       cascade =CascadeType.ALL   )   public Address getAddress() {       return address;   }
   public void setAddress(Address address) {       this.address = address;   }
   public int getEnabled() {       return enabled;   }
   public void setEnabled(int enabled) {       this.enabled = enabled;   }
   @JoinTable(name = quot;users_authoritiesquot;,joinColumns = {@JoinColumn(name = quot;users_idquot;) },inverseJoinColumns = {@JoinColumn(name = quot;authorities_idquot;) }       )   @ManyToMany(cascade = {CascadeType.ALL },fetch = FetchType.EAGER   )   public Listlt;Authoritygt; getAuthorities() {       return authorities;   }
   public void setAuthorities(Listlt;Authoritygt; authorities) {       this.authorities = authorities;   }
   public void setAuthority(Authority authority) {       this.authorities.add(authority);   }

}
My Address:

Code:
package com.baselogic.domain;

[omitted]

@Entity
@Table(name = quot;addressquot;)
@EntityListeners( { IdentifiedObject.class })
public class Address extends IdentifiedObjectlt;Longgt; {   private static final long serialVersionUID = 3832626162173359499L;   private String address;   private String address2;   private String city;   private String state;   private String zipcode;   private String country;
   public String getAddress() {       return address;   }
   public void setAddress(String address) {       this.address = address;   }
   public String getAddress2() {       return address2;   }
   public void setAddress2(String address2) {       this.address2 = address2;   }
   public String getCity() {       return city;   }
   public void setCity(String city) {       this.city = city;   }
   public String getState() {       return state;   }
   public void setState(String state) {       this.state = state;   }
   public String getZipcode() {       return zipcode;   }
   public void setZipcode(String zipcode) {       this.zipcode = zipcode;   }
   public String getCountry() {       return country;   }
   public void setCountry(String country) {       this.country = country;   }
} // The End...
My Authority:

Code:
package com.baselogic.domain.security;

[omitted]

@Entity
@Table(name = quot;authoritiesquot;)
public class Authority    extends IdentifiedObjectlt;Longgt;   implements Serializable {   private static final long serialVersionUID = 3832626162173359499L;
   private Listlt;Usergt; users = new LinkedListlt;Usergt;();
   private String authority;
   public Authority() {       this(quot;ROLE_VIEWERquot;);   }
   public Authority(String authority) {       this.authority = authority;   }      @JoinTable(name = quot;users_authoritiesquot;,joinColumns = {@JoinColumn(name = quot;authorities_idquot;) },inverseJoinColumns = {@JoinColumn(name = quot;users_idquot;) }       )   @ManyToMany(cascade = {CascadeType.ALL },fetch = FetchType.EAGER   )   public Listlt;Usergt; getUsers() {       return users;   }
   public void setUsers(Listlt;Usergt; users) {       this.users = users;   }
   public String getAuthority() {       return authority;   }
   public void setAuthority(String authority) {       this.authority = authority;   }
}quot;          date_created=quot;1980-07-03quot;          date_updated=quot;1980-07-03quot;/gt;
   lt;users id=quot;minimickknutsonquot;          username=quot;minimickknutsonquot;          address_fk=quot;2quot;          enabled=quot;0quot;          password=quot;1234quot;          first_name=quot;MiniMickquot;          last_name=quot;Knutsonquot;          email=quot;mickknutson@nomail.comquot;          date_created=quot;1980-07-03quot;          date_updated=quot;1980-07-03quot;/gt;
   lt;users id=quot;yesnotherusernamequot;          username=quot;yesnotherusernamequot;          enabled=quot;1quot;          password=quot;1234quot;          first_name=quot;yesnotherusernamequot;          last_name=quot;yesnotherusernamequot;          email=quot;yesnotherusername@nomail.comquot;          date_created=quot;1980-07-03quot;          date_updated=quot;1980-07-03quot;/gt;
   lt;users_authorities users_id=quot;mickknutsonquot; authorities_id=quot;1quot;/gt;   lt;users_authorities users_id=quot;minimickknutsonquot; authorities_id=quot;1quot;/gt;   lt;users_authorities users_id=quot;yesnotherusernamequot; authorities_id=quot;1quot;/gt;      lt;users_authorities users_id=quot;mickknutsonquot; authorities_id=quot;2quot;/gt;   lt;users_authorities users_id=quot;mickknutsonquot; authorities_id=quot;3quot;/gt;   lt;users_authorities users_id=quot;mickknutsonquot; authorities_id=quot;4quot;/gt;lt;/datasetgt;
public interface BaseDaolt;T, ID extends Serializablegt; delete method:Code:   @Transactional(readOnly = false)   void delete(T entity);
class BaseDaoJpaImpllt;T, ID extends Serializablegt; extends JpaDaoSupport   implements BaseDaolt;T, IDgt; delete method:

Code:   @Transactional    public void delete(final T entity) {       getJpaTemplate().execute(new JpaCallback() {    public Object doInJpa(EntityManager entityManager)        throws PersistenceException {        T managedEntity = entity;       if (!entityManager.contains(managedEntity)) {            managedEntity = entityManager.merge(entity);        }       entityManager.remove(managedEntity);       return null;    }});   }
My bean:

Code:
lt;bean id=quot;userDaoquot; class=quot;com.baselogic.dao.jpa.BaseDaoJpaImplquot;         scope=quot;prototypequot;gt;        lt;constructor-arg value=quot;com.baselogic.domain.Userquot; /gt;       lt;property name=quot;jpaTemplatequot; ref=quot;jpaTemplatequot;/gt;   lt;/beangt;
UserTest:

Code:   @Test   public void testRemoveSingleUser() {              // verify there are several Users       Listlt;Usergt; users = dao.findAll();       assertNotNull(users);       assertEquals(3, users.size());
       // Get single user       User user = dao.find(quot;mickknutsonquot;);       assertNotNull(user);       assertEquals(user.getId(), quot;mickknutsonquot;);       assertEquals(user.getFirstName(), quot;Mickquot;);
       // Remove single User       dao.delete(user);       assertFalse(dao.exists(quot;mickknutsonquot;));
       // Verify there is 1 less user now.       users = dao.findAll();       assertEquals(2, users.size());// lt;-- Assertion Error users.size() = 0   }
This has stumped me for 3 days now, and I would really appreciate help.

I have also taken the log ouput for that test:Code:
DEBUG (BaseDaoJpaImpl.delete: 1390) |
DEBUG (BaseDaoJpaImpl.delete: 1397) | delete(final T entity)
DEBUG (SQL.logStatement: 111) | select user0_.id as id2_2_, user0_.date_created as date2_2_2_, user0_.date_updated as date3_2_2_, user0_.address_fk as address11_2_2_, user0_.email as email2_2_, user0_.enabled as enabled2_2_, user0_.first_name as first6_2_2_, user0_.last_name as last7_2_2_, user0_.middle_name as middle8_2_2_, user0_.password as password2_2_, user0_.username as username2_2_, address1_.id as id0_0_, address1_.date_created as date2_0_0_, address1_.date_updated as date3_0_0_, address1_.address as address0_0_, address1_.address2 as address5_0_0_, address1_.city as city0_0_, address1_.country as country0_0_, address1_.state as state0_0_, address1_.zipcode as zipcode0_0_, authoritie2_.users_id as users1_4_, authority3_.id as authorit2_4_, authority3_.id as id1_1_, authority3_.date_created as date2_1_1_, authority3_.date_updated as date3_1_1_, authority3_.authority as authority1_1_ from users user0_ left outer join address address1_ on user0_.address_fk=address1_.id left outer join users_authorities authoritie2_ on user0_.id=authoritie2_.users_id left outer join authorities authority3_ on authoritie2_.authorities_id=authority3_.id where user0_.id=?

DEBUG (SQL.logStatement: 111) | select users0_.authorities_id as authorit2_2_, users0_.users_id as users1_2_, user1_.id as id2_0_, user1_.date_created as date2_2_0_, user1_.date_updated as date3_2_0_, user1_.address_fk as address11_2_0_, user1_.email as email2_0_, user1_.enabled as enabled2_0_, user1_.first_name as first6_2_0_, user1_.last_name as last7_2_0_, user1_.middle_name as middle8_2_0_, user1_.password as password2_0_, user1_.username as username2_0_, address2_.id as id0_1_, address2_.date_created as date2_0_1_, address2_.date_updated as date3_0_1_, address2_.address as address0_1_, address2_.address2 as address5_0_1_, address2_.city as city0_1_, address2_.country as country0_1_, address2_.state as state0_1_, address2_.zipcode as zipcode0_1_ from users_authorities users0_ left outer join users user1_ on users0_.users_id=user1_.id left outer join address address2_ on user1_.address_fk=address2_.id where users0_.authorities_id=?

DEBUG (SQL.logStatement: 111) | select users0_.authorities_id as authorit2_2_, users0_.users_id as users1_2_, user1_.id as id2_0_, user1_.date_created as date2_2_0_, user1_.date_updated as date3_2_0_, user1_.address_fk as address11_2_0_, user1_.email as email2_0_, user1_.enabled as enabled2_0_, user1_.first_name as first6_2_0_, user1_.last_name as last7_2_0_, user1_.middle_name as middle8_2_0_, user1_.password as password2_0_, user1_.username as username2_0_, address2_.id as id0_1_, address2_.date_created as date2_0_1_, address2_.date_updated as date3_0_1_, address2_.address as address0_1_, address2_.address2 as address5_0_1_, address2_.city as city0_1_, address2_.country as country0_1_, address2_.state as state0_1_, address2_.zipcode as zipcode0_1_ from users_authorities users0_ left outer join users user1_ on users0_.users_id=user1_.id left outer join address address2_ on user1_.address_fk=address2_.id where users0_.authorities_id=?

DEBUG (SQL.logStatement: 111) | select users0_.authorities_id as authorit2_2_, users0_.users_id as users1_2_, user1_.id as id2_0_, user1_.date_created as date2_2_0_, user1_.date_updated as date3_2_0_, user1_.address_fk as address11_2_0_, user1_.email as email2_0_, user1_.enabled as enabled2_0_, user1_.first_name as first6_2_0_, user1_.last_name as last7_2_0_, user1_.middle_name as middle8_2_0_, user1_.password as password2_0_, user1_.username as username2_0_, address2_.id as id0_1_, address2_.date_created as date2_0_1_, address2_.date_updated as date3_0_1_, address2_.address as address0_1_, address2_.address2 as address5_0_1_, address2_.city as city0_1_, address2_.country as country0_1_, address2_.state as state0_1_, address2_.zipcode as zipcode0_1_ from users_authorities users0_ left outer join users user1_ on users0_.users_id=user1_.id left outer join address address2_ on user1_.address_fk=address2_.id where users0_.authorities_id=?

DEBUG (SQL.logStatement: 111) | select users0_.authorities_id as authorit2_2_, users0_.users_id as users1_2_, user1_.id as id2_0_, user1_.date_created as date2_2_0_, user1_.date_updated as date3_2_0_, user1_.address_fk as address11_2_0_, user1_.email as email2_0_, user1_.enabled as enabled2_0_, user1_.first_name as first6_2_0_, user1_.last_name as last7_2_0_, user1_.middle_name as middle8_2_0_, user1_.password as password2_0_, user1_.username as username2_0_, address2_.id as id0_1_, address2_.date_created as date2_0_1_, address2_.date_updated as date3_0_1_, address2_.address as address0_1_, address2_.address2 as address5_0_1_, address2_.city as city0_1_, address2_.country as country0_1_, address2_.state as state0_1_, address2_.zipcode as zipcode0_1_ from users_authorities users0_ left outer join users user1_ on users0_.users_id=user1_.id left outer join address address2_ on user1_.address_fk=address2_.id where users0_.authorities_id=?

DEBUG (SQL.logStatement: 111) | select authoritie0_.users_id as users1_1_, authoritie0_.authorities_id as authorit2_1_, authority1_.id as id1_0_, authority1_.date_created as date2_1_0_, authority1_.date_updated as date3_1_0_, authority1_.authority as authority1_0_ from users_authorities authoritie0_ left outer join authorities authority1_ on authoritie0_.authorities_id=authority1_.id where authoritie0_.users_id=?

DEBUG (SQL.logStatement: 111) | select authoritie0_.users_id as users1_1_, authoritie0_.authorities_id as authorit2_1_, authority1_.id as id1_0_, authority1_.date_created as date2_1_0_, authority1_.date_updated as date3_1_0_, authority1_.authority as authority1_0_ from users_authorities authoritie0_ left outer join authorities authority1_ on authoritie0_.authorities_id=authority1_.id where authoritie0_.users_id=?

DEBUG (JpaTransactionManager.processCommit: 730) | Initiating transaction commit

DEBUG (JpaTransactionManager.doCommit: 451) | Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@5782b9b5]

DEBUG (SQL.logStatement: 111) | delete from users_authorities where authorities_id=?
DEBUG (SQL.logStatement: 111) | delete from users_authorities where authorities_id=?
DEBUG (SQL.logStatement: 111) | delete from users_authorities where authorities_id=?
DEBUG (SQL.logStatement: 111) | delete from users_authorities where authorities_id=?
DEBUG (SQL.logStatement: 111) | delete from users_authorities where users_id=?
DEBUG (SQL.logStatement: 111) | delete from users_authorities where users_id=?
DEBUG (SQL.logStatement: 111) | delete from users_authorities where users_id=?
DEBUG (SQL.logStatement: 111) | delete from users where id=?
DEBUG (SQL.logStatement: 111) | delete from address where id=?
DEBUG (SQL.logStatement: 111) | delete from users where id=?
DEBUG (SQL.logStatement: 111) | delete from authorities where id=?
DEBUG (SQL.logStatement: 111) | delete from authorities where id=?
DEBUG (SQL.logStatement: 111) | delete from authorities where id=?
DEBUG (SQL.logStatement: 111) | delete from authorities where id=?
DEBUG (SQL.logStatement: 111) | delete from users where id=?
DEBUG (SQL.logStatement: 111) | delete from address where id=?
DEBUG (JpaTransactionManager.doCleanupAfterCompletion: 534) | Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@5782b9b5] after transaction
DEBUG (EntityManagerFactoryUtils.closeEntityManager: 313) | Closing JPA EntityManager
DEBUG (AnnotationTransactionAttributeSource.getTransactionAttribute: 107) | Adding transactional method [exists] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly]

...I changed the User.Authorities annotations to:

Code:   @ManyToMany(targetEntity = Authority.class,     fetch = FetchType.LAZY   )   @Cascade( {org.hibernate.annotations.CascadeType.ALL} )   public Listlt;Authoritygt; getAuthorities() {       return authorities;   }
and the Autority.Users annotations to:

Code:   @ManyToMany(fetch = FetchType.EAGER   )   //@Cascade( {org.hibernate.annotations.CascadeType.ALL} )
This has resolved my issue.
¥
Back Forum Reply New