Oct 13, 2010

f Comment

Hibernate Self Join - Test Java Code

MenuHow to Do Self Join Many-To-Many Mapping In Hibernate
Hibernate Self Join - Create DB Tables
Hibernate Self Join - Create Java Class
Hibernate Self Join - Create Hibernate Configuration
Hibernate Self Join - Test Java Code
Amazon So we've created a database table to embody the concept of a Keyword, a table to join a Keyword to itself, the actual Java class that embodies the Keyword, the corresponding Hibernate configuration. Now you should be able to test our code.

Suppose manager is an instance of a class whose job is to do CRUD (create, read, update, delete) operations on a Hibernate aware entity. Suppose we already have Keywords with keyword_id 243, 250, 260. Here's how we add a couple of keywords to another keyword as its children:
Keyword keyword1 = (Keyword)manager.getKeywordById(243);
Keyword keyword2 = (Keyword)manager.getKeywordById(250);
Keyword keyword3 = (Keyword)manager.getKeywordById(260);
Set children=new HashSet();
children.add(keyword2);
children.add(keyword3);
keyword1.setChildren(children);
manager.createOrUpdate(keyword1);
With any luck you'll run the code without an error and see that the corresponding rows created in the database correctly. Congratulations! Questions? Let me know! Otherwise enjoying Java and Hibernate!

◀ Create Hibernate Configuration
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael