Une relation plusieurs-à-plusieurs.Le mappage plusieurs-à-plusieurs ne fonctionne pas, un côté est configuré pour inverser
Les tables:
Product (productId, ...)
Category (categoryId, ...)
Product_Category(productId, categoryId)
-je configurer la relation si toutes les mises à jour seront effectuées par l'entité produit.
produit Entité:
private Set<Category> categories = new HashSet<Category>();
public void AddCategory(Category category)
{
if(!this.categories.contains(category))
this.categories.add(category);
if(!category.getProducts().contains(this))
category.getProducts().add(this);
}
public void RemoveCategory(AMCategory category)
{
if(categories.contains(category))
categories.remove(category);
if(category.getProducts().contains(this))
category.getProducts().remove(this);
}
Product.hbm.xml
<set name="categories" table="product_category" cascade="save-update" lazy="true">
<key column="productId"/>
<many-to-many column="categoryId" class="Category"/>
</set>
L'entité Catégorie:
private Set<Product> products = new HashSet<Product>();
/**
* @return the products
*/
public Set<Product> getProducts() {
return products;
}
Category.hbm.xml:
<set name="Products" table="product_category" cascade="none" inverse="true" lazy="true">
<key column="categoryId"/>
<many-to-many column="productId" class="Product"/>
</set>
Maintenant, j'ai une boucle dans laquelle je crée et sauvegarde des entités Product, et j'associe également une seule catégorie à chacune.
La table product_category est vide, donc je suppose que j'ai un problème avec mes mappages.
forloop
{
Product p = new Product();
// set all properties
Category c = DAO.GetCategory(someId);
p.AddCategory(c);
session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
}
J'ai aussi essayé d'abord sauver le nouveau produit, puis le chargement du produit nouvellement inséré, puis en ajoutant la catégorie et l'épargne, mais même résultat, the table product_category is empty.
Le tableau produit a inséré les nouveaux produits.
Qu'ai-je fait de mal? Il doit y avoir des lignes dans les tables product_category et product, mais la catégorie product_category est vide.
Mise à jour
classe Mon HibernateUtil:
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
return configuration.buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Fixez vos mappings dans votre publication - Product.hbm.xml montre class = "Product" qui devrait être class = "Category" dans le many-to-many; et aucun ne montre inverse. Ensuite, nous pourrions voir ce qui ne va pas. –
hmm, peut-être que c'est le problème, je changeais certaines choses pour voir si cela fonctionnerait, laissez-moi vérifier merci. – mrblah
ok je l'ai corrigé, si j'ai cascade = aucun ai-je vraiment besoin d'inverse = vrai encore? – mrblah