View Single Post
  #1 (permalink)  
Old 06-06-2007, 06:00 AM
Albert Albert is offline
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
Mapping of class Association with hibernate
Hello people, I need to do map to a class of association

Code:
public class ClassA { private Integer id_A = new Integer(-1); //collection B } public class ClassB{ private Integer id_B = new Integer(-1); // collection A } public class ClassAB { // many-to-many with extra attributes private A a; // attribute of the type of the A class or could be of the type of id_A, would be PK and FK private B b;// attribute of the type of class B or could be of the type of id_B, would be PK and FK private String extraAttribute; private Date date; }
The problem that appears to me is that the mapping alternatives, that offer hibernate for relations many to many, when the intermediate class has additional attributes to the primary keys (pk own and fk other people's), also happen to be part of the key, and I did not find as it would have to be the method to insert a new object to this collection, and to be able to assign to him to values to the additional attributes (ExtraAttribute and date), in addition in my model these attributes they would not be primary key and they could later be, therefore null or to be loaded if outside necessary (thing q the mapping offered by hibernate conditions to being it, PK everything).

The q I made to solve it momentarily is to create this intermediate class, and in the mapping (ClassAB.hbm.xml) to make use of composite-you go with key-many-to-one for the traditional FK and property for the remaining attributes. Next to this, a method that does saveORupdate of the instance of ClassAB, also methods that give back collection to me of A and B (in this case would use ArrayList) to add to the same ones, but the problem of this is that he is everything very artisan.

Albert
Reply With Quote
Sponsored Links