File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate ManyToMany question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate ManyToMany question." Watch "Hibernate ManyToMany question." New topic
Author

Hibernate ManyToMany question.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

Hi All,

I have below situation:

I have two tables ActivityType and ActivitySubType with ManyToMany relationship with each other. So I have a another table Activity_Main_Sub_Type which has two columns

(!) ActivityTypeId (2) ActivitySubTypeId

There is another table Activity which has reference to both ActivityTypeId and ActivitySubTypeId as foreign keys. Now I had generated these tables using Hibernate schema update. Now I want to delete Activity_Main_Sub_Type table and populate it again. I am not able to do that. I get foreign key constraint error on Activity table. When I looked in to the table I saw that there are two foreign key reference from table Activity_Main_Sub_Type. I am not sure how did I get this? Can any body suggest me if I am missing anything. Is there any way I can update my table and add new data.


Pawan Chopra
SCJP - DuMmIeS mInD
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

Is Activity defined as one of the entities ?


Thanks,
Tushar (SCJP 1.5)
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

T Mishra wrote:Is Activity defined as one of the entities ?


Yes, It is an Entity
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

You should have only two entities defined ActivityType and ActivitySubType (make sure the mappedBy and join columns are correct.). Since you are generating schema from entity definition, hbmtoddl will create the mapping table Activity_Type_Activity_Sub_Type. This will have the foreign keys to ActivityType and ActivitySubType. Third entity Activity is not needed here.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

But Activity is another class which is using ActivityType and subtype. For example in my application user can create an activity of particular type and under that sub type. So I need to save that in database as a record. But I am not sure why Activity table has reference to mapping table Activity_Type_Activity_Sub_Type.
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

Does the entity Activity, has any other attributes other than ActivityTypeId and ActivitySubTypeId. If no, the entity Activity is similar to the auto generated mapping table Activity_Type_Activity_Sub_Type.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 411

T Mishra wrote:Does the entity Activity, has any other attributes other than ActivityTypeId and ActivitySubTypeId. If no, the entity Activity is similar to the auto generated mapping table Activity_Type_Activity_Sub_Type.


Yes, It has other attributes as well. Also some other references.
T Mishra
Ranch Hand

Joined: Apr 04, 2006
Posts: 108

Can you compare the existing entities against those generated by the hbm2java esp Activity ?
 
jQuery in Action, 2nd edition
 
subject: Hibernate ManyToMany question.