0

I have two tables;

Street has

 "Street_id", "StreetName", "description"

and Path has

 "path_id", "pathDescription", "path_name"

and there is one table more,

Street-path has

  "path_id", "Street_id"

Now I want to create a relationship between these tables such that when ever record is deleted from Street, record from Street-path will also be deleted, but they can't have direct relation ship. Only Street and Path can have direct relationship, and record in Path should keep intact.

How to do it in hibernate?

PS: I am using xml, not annotations for hibernate mapping.

Just_another_developer
  • 5,737
  • 12
  • 50
  • 83
  • Do you have any idea how this can be done? – Vitaly Apr 18 '13 at 10:49
  • I tried, and created direct relation ship but couldn't figure out how to do the exact thing that is mentioned that is why i asked here. – Just_another_developer Apr 18 '13 at 10:53
  • I think you should try this http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-many-to-many-1.html and get back with your code (mappings, access, etc) if it's not working. – Vitaly Apr 18 '13 at 11:02

2 Answers2

0

You can use Criteria api. You actually do not need any relationship, you just need street_id and you have it already. You can try this;

public void removeStreet(Street street) //this reference is an instance 
{                                       //of Street entity that you want to remove
   session.clear();
   session.delete(street);
   session.flush();

   StreetPath streetPath = new StreetPath(); //create new instance of StreetPath entity
   streetPath.setStreetId(street.getId());   //with id of deleted row of Street table  

   session.clear();
   session.delete(streetPath);
   session.flush();
}

I hope that helps.

  • how, if you could please give example? – Just_another_developer Apr 18 '13 at 10:57
  • @Sobia I tried to give an example. You must create a session first, I guess you have one already. BTW you don't need to use Path entity for this process. –  Apr 18 '13 at 13:56
  • this method that u mentioned i already know ... but I want to do exact what I posted in my query "when ever record is deleted from Street, record from Street-path will also be deleted 'automatically',and record in Path should keep intact" ... and to do so I will not need to query StreetPath seperatly. – Just_another_developer Apr 19 '13 at 04:03
  • @Sobia Maybe this link helps. http://stackoverflow.com/questions/4486413/how-to-imlement-triggers-in-hibernate –  Apr 19 '13 at 06:53
0

you can use this tutorial: Hibernate mapping

Navand
  • 446
  • 4
  • 11
  • This is not really an answer. – Vitaly Apr 18 '13 at 11:04
  • For when ever record is deleted from Street, record from Street-path will also be deleted, you should use `cascade` attribute in XML – Navand Apr 18 '13 at 11:07
  • I have not tried, but everywhere in hibernate tutorial write this, you can try it and it `SQL` for your problem, use `cascade` too. – Navand Apr 18 '13 at 11:25
  • 1
    I do not need to try this actually, you advised using 1:m while the relationship is obviously m:n and then suggested something that you heard about somewhere.. – Vitaly Apr 18 '13 at 11:28