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;
}