-->
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: @Many-to-Many mapping update problem and sources problem
PostPosted: Fri Mar 09, 2007 4:48 am 
Newbie

Joined: Fri Mar 09, 2007 4:13 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.0 GA

Mapping documents:

First

Code:
@Entity
@Table(name = "CATALOG")
public class CatalogDBO {

    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToMany(cascade = CascadeType.ALL)
    private Set<ItemDBO> items = new HashSet<ItemDBO>();


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    public Set<ItemDBO> getItems() {
        return items;
    }

    public void setItems(Set<ItemDBO> items) {
        this.items = items;
    }
}


Second
Code:
@Entity
@Table(name = "ITEM")
public class ItemDBO {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long Id;

    private String name;

    @ManyToMany(mappedBy = "items")
    private Set<CatalogDBO> catalogDBOs = new HashSet<CatalogDBO>();


    public Long getId() {
        return Id;
    }

    public void setId(Long id) {
        Id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<CatalogDBO> getCatalogDBOs() {
        return catalogDBOs;
    }

    public void setCatalogDBOs(Set<CatalogDBO> catalogDBOs) {
        this.catalogDBOs = catalogDBOs;
    }
}



Code between sessionFactory.openSession() and session.close():

Code:
        CatalogDBO catalogDBO = getEntityManager().find(CatalogDBO.class, catId);
        ItemDBO itemDBO = getEntityManager().find(ItemDBO.class, itemId);
        catalogDBO.getItems().add(itemDBO);
        getEntityManager().merge(itemDBO);
        getEntityManager().merge(catalogDBO);


Database: Oracle 9i

Hibernate creates assocation table CATALOG_ITEM with FK's CATALOGDBOS_ID and ITEMS_ID.

When i'm adding item to catalog and then merge it, Hibernate didn't insert link between objecrs in the assiciation table. But if I create new Catalog, add Items to it and then persist it via the EntityManager, link will be created. I cant't understand it. I have searched answer in this forum, in "Java persistence with Hibernate", but I didn't find it.

Can someone explain where problem is?

P.S. I've tried different cascades. It, doesn't work...

P.P.S. I've search in hibernate sources and I have one question.
For what reason in org.hibernate.type.CollectionType.replace PersistentSet with dirty=true first is copied to simple HashSet (dirty missed) and then copied back to PersistentSet? Because of this copying "dirty" flag in colletcion is missed!
Here is the code of second copying:
Code:
      if (original==target) {
         //get the elements back into the target
         //TODO: this is a little inefficient, don't need to do a whole
         //      deep replaceElements() call
         replaceElements( result, target, owner, copyCache, session );
         result = target;
      }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 8:34 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Skipper, i have the following questions:

- Why you didn't specify link table in your annotations? Like that:

@JoinTable(
name = "CATALOG_ITEM",
joinColumns = {@JoinColumn(name = "CATALOGDBOS_ID")},
inverseJoinColumns = {@JoinColumn(name = "ITEMS_ID")}
)
private Set<ItemDBO> items = new HashSet<ItemDBO>();


- When you updating bidirectional association (wich has two representations in memory), you must update two sides of that association. In your case, Catalogs collection in Item entity is inverse side of this association (as specified mappedBy attribute) As i see, you've updated only one side in this line:

catalogDBO.getItems().add(itemDBO);

What you must do:

catalogDBO.getItems().add(itemDBO);
itemDBO.getCatalogDBOs().add(catalogDBO);


I hope this helps!

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 09, 2007 8:42 am 
Newbie

Joined: Fri Mar 09, 2007 4:13 am
Posts: 2
lester wrote:
Skipper, i have the following questions:

- Why you didn't specify link table in your annotations? Like that:

@JoinTable(
name = "CATALOG_ITEM",
joinColumns = {@JoinColumn(name = "CATALOGDBOS_ID")},
inverseJoinColumns = {@JoinColumn(name = "ITEMS_ID")}
)


- When you updating bidirectional association (wich has two representations in memory), you must update two sides of that association. As i see, you've updated only one side in this line:

catalogDBO.getItems().add(itemDBO);

What you must do:

catalogDBO.getItems().add(itemDBO);
itemDBO.getCatalogDBOs().add(catalogDBO);


I hope this helps!


Thanks for reply. I've tried link via joinTable, but it didn't help too. How the book said, there is no need to link visa JoinTable. This relation make everything by default.

The answer is simple. Update to hibernate 3.2.2 GA. There was a bug №HHH-2292.
bug HHH-2292

Thanks for help :)


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.