-->
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.  [ 2 posts ] 
Author Message
 Post subject: Question about ManyToMany and deletes
PostPosted: Mon Jun 08, 2009 1:04 pm 
Newbie

Joined: Fri Apr 10, 2009 3:53 pm
Posts: 6
Hello;

I have an Entity called Audit with a member called Materials which is a collection of Material Entities:

Code:
   @ManyToMany(mappedBy="audits")
   private List<Material> materials = new ArrayList<Material>();


I put the mappedBy here because I would like Material to be the owner of this relationship (I hope I am understanding this correctly).

In Material I have:
Code:
   @ManyToMany(cascade={CascadeType.ALL})
   private List<Audit> audits = new ArrayList<Audit>();


The table that is created is:
Code:
mysql> desc material_audit;
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| materials_id | int(11) | NO   | MUL | NULL    |       |
| audits_id    | int(11) | NO   | MUL | NULL    |       |
+--------------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)


This what I would expect. However the issue I have is I can't delete an Audit without getting a 'Cannot delete or update a parent row: a foreign key constraint fails' exception on this table.

I have been reading through the Annotations reference guide and I am still not sure how to map this so I can delete Audits and have the Materials persist but the material_audit mappings removed.

Any tips would be great.

Thanks,
Luke


Top
 Profile  
 
 Post subject: Re: Question about ManyToMany and deletes
PostPosted: Tue Jun 09, 2009 12:53 am 
Newbie

Joined: Fri Apr 10, 2009 3:53 pm
Posts: 6
I tried making the following modification.

I gave Audit this:
Code:
@ManyToMany(cascade={CascadeType.ALL})
   private List<Material> materials = new ArrayList<Material>();


And took out all mention of Audit in Material. I don't really need to ever get a list of Audit's by Materials. But my goal is still to be able to associate Audits with Materials and delete an Audit without deleting the Material.

My problem now is I am not sure how to persist Materials to an Audit.

The following resulted in an Audit being persisted by no Materials in the audit_material table (I am running this in a TestCase):
Code:
// materials
      Material material1 = new Material();
      material1.setName(TEST_MATERIAL_1);
      Material material2 = new Material();
      material2.setName(TEST_MATERIAL_2);
      materialDao.save(material1);
      materialDao.save(material2);
      materialDao.getHibernateTemplate().flush();

//build a list of materials
      List<Material> materials = new ArrayList<Material>();
      Material m1 = materialDao.findByName(TEST_MATERIAL_1);
      Assert.notNull(m1);
      materials.add(m1);
      Material m2 = materialDao.findByName(TEST_MATERIAL_2);
      Assert.notNull(m2);
      materials.add(m2);

      //build an audit
      Audit audit = new Audit();
      audit.setCompany(cLoad);
      audit.setAreaName("Test Area");
      audit.setCategoryName("Test Category Name");
      audit.setAuditClass("Test Audit Class Name");
      audit.setAuditDate(new Date());
      audit.setMaterials(materials);
      auditDao.save(audit);
      auditDao.getHibernateTemplate().flush();


I did some searching around and found a few people with similar issues to mine who's posting had not been answered. Either this is so trivial it is left alone or this is a tricky thing to do (which I find hard to believe).

Any tips would be appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.