Hi,

I am having an issue with a DAO test. The problem arises when trying to remove an item from an ArrayList annotated as a Hibernate @CollectionOfElements - I am finding that that I am getting a failure on the respective JUnit tests (see final, commented-out assertion in the test methods below). The removal of the item from the list is not persisted. Adding items to the ArrayList appears to work just fine.

My DAO extends GenericDao - but does not override the save() method (which in turn, calls jpaEntityFactory.merge()).

I am wondering, if for @CollectionOfElements, I need to be doing something manually to make it work. Removing an associated object from my @ManyToOne mappings works just fine.

I should add though that I have had issues with using both Hibernate and JPA (Hibernate implementation). @CollectionOfElements is, of course, a Hibernate extension to the JPA spec.

--- The two failing tests:
public class ItemDaoTest extends BaseDaoTestCase
{
   ...

    public void testAddAsset() throws Exception
    {
        NewsItem item = dao.get(-1L);
        assertEquals(2, item.getAssets().size());

        Asset asset = new Asset();
        asset.set...();

        item.addAsset(asset);
        item = dao.save(item);

        item = dao.get(-1L);
        assertEquals("more than 3 assets", 3, item.getAssets().size());

        item.removeAsset(asset);
        //item.getAssets().remove(asset);
        item = dao.save(item);

        item = dao.get(-1L);
        // FIXME -- this is not working --FAILS!
        //assertEquals(2, item.getAssets().size());
    }

    public void testAddContact() throws Exception
    {
        NewsItem item = dao.get(-1L);
        assertEquals(2, item.getContacts().size());

        Contact contact = new Contact();
        contact.set...();
        contact.setEmail("[EMAIL PROTECTED]");
        contact.setOutdated(false);

        item.addContact(contact);
        item = dao.save(item);

        item = dao.get(-1L);
assertEquals("more than 3 contacts", 3, item.getContacts().size());

        item.getContacts().remove(contact);
        //item.removeContact(contact);
         dao.save(item);

        //item = dao.get(-1L);
        // FIXME -- this is not working -- FAILS!!
        //assertEquals(2, item.getContacts().size());
    }
}


An abridged version of the DB schema and the model classes are listed below:

--- The DB (MySQL, InnoDB):

Table: ITEMS
   ID  BIGINT(20) NOT NULL
   ...
Table: ITEM_ASSETS
   ITEM_ID  BIGINT(20) NOT NULL
   POSITION INT(11) NOT NULL
   ...
   PRIMARY KEY  (`ITEM_ID`,`POSITION`),
   KEY `FK4028A102370B2881` (`ITEM_ID`),
   CONSTRAINT `FK4028A102370B2881` FOREIGN KEY (`ITEM_ID`)

Table: ITEM_CONTACTS
   ITEM_ID  BIGINT(20) NOT NULL
   POSITION INT(11) NOT NULL
   ...
   PRIMARY KEY  (`ITEM_ID`,`POSITION`),
   KEY `FKCCF02612370B2881` (`ITEM_ID`),
CONSTRAINT `FKCCF02612370B2881` FOREIGN KEY (`ITEM_ID`) REFERENCES `newsitems` (`ITEM_ID`)

--- The Model:

@Entity
public class Item implements Serializable
{
   private Long id;
   private List<Asset> = new ArrayList<Asset>();
   private List<Contact> = new ArrayList<Contact>();
   private Set<Tag> = new HashSet<Tag>();
   ... misc fields

    @Id @SearchableId
    @Column(name = "ITEM_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId()
    {
        return id;
    }

    @CollectionOfElements
@JoinTable(name = "ITEMS_ASSETS", joinColumns = @JoinColumn(name = "ITEM_ID"))
//    @CollectionId(
//            columns = @Column(name = "ASSET_ID"),
//            type = @Type(type = "long"),
//            generator = "native"
//    )
    @IndexColumn(name = "POSITION", base = 1)
    @SearchableReference(refAlias = "asset")
    public List<Asset> getAssets()
    {
        return assets;
    }

    @CollectionOfElements
@JoinTable(name = "ITEMS_CONTACTS", joinColumns = @JoinColumn(name = "ITEM_ID"))
    @IndexColumn(name = "POSITION", base = 1)
    public List<Contact> getContacts()
    {
        return contacts;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "ITEMS_TAGS",
joinColumns = { @JoinColumn(name = "ITEM_ID") }, inverseJoinColumns = { @JoinColumn(name = "TAG_ID") }
             )
    private Set<Tag> getTags()
    {
        return tags;
    }

    @Transient
    public List<Tag> getTagsList()
    {
        return new ArrayList<Tag>(tags);
    }
   ... misc getters + setters ...

    @SuppressWarnings("unused")
    private void setContacts(List<Contact> contacts)
    {
        this.contacts = contacts;
    }

    public void addContact(Contact newContact)
    {
        if (newContact == null)
            throw new IllegalArgumentException("Null contact");
        this.getContacts().add(newContact);
    }

    public void removeContact(Contact contact)
    {
        this.getContacts().remove(contact);
    }

    public String toString() { .. }
    public int hashCode() { .. }
    public boolean equals() { .. }
}

@Embeddable
public class Asset implements Serializable
{
    ... fields ...
    ... getters + setters ...
    public String toString() { .. }
    public int hashCode() { .. }
    public boolean equals() { .. }
}

@Embeddable
public class Contact implements Serializable
{
    ... fields ...
    ... getters + setters ...

    @org.hibernate.annotations.Parent
    public Item getItem()
    {
        return item;
    }

    public String toString() { .. }
    public int hashCode() { .. }
    public boolean equals() { .. }
}




Thanks for any support you can give.

Alex Coles

Alexander Coles
[EMAIL PROTECTED]
www.alexcolesportfolio.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to