Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate ManyToMany question.

 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is Activity defined as one of the entities ?
 
pawan chopra
Ranch Hand
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T Mishra wrote:Is Activity defined as one of the entities ?


Yes, It is an Entity
 
T Mishra
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
jQuery Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you compare the existing entities against those generated by the hbm2java esp Activity ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic