New Features
Enhanced Features
O/R Mapping
Tips and Tricks
UML Diagrams
VP Suite

One foreign key point to multiple primary key

Visual Paradigm ORM code generation now support one foreign key associating to multiple primary keys.

Entity Relationship Diagram


From the above ERD, college_id of Student point to another two entities college_id

Generating Class Diagram from Entity Relationship Diagram

Then generate it to Class Diagram by following ways:

  • Right Click on the DiagramSynchronize to Class Diagram

  • or hold down the right-mouse button, move the mouse to form the right-left gesture indicated by the blue path on the Entity Relationship Diagram.

Specifying which association support setter method

Since the Student table have more than one foreign key referenced, during the synchronization from ERD to Class Diagram, a Conflit dialog box will be shown

  • Department = > Student
    if choosing this one, Department information can be set and retrieve from Student. And College for Student becomes readonly.
  • College = > Student
    if choosing this one, College information can now be set and retrieve from Student, and the Department for Student can be readonly.


In this example, we will select Department = > Student as our choice
Animated Demo

Generating code from Class Diagram

  1. Select from the menu bar ToolsObject-Relation Mapping(ORM)Wizards...

  2. Choose Generate Code and Database from Class Diagram then click on Next Button
  3. Select Classes needed for generating Database table
  4. View table details and change code style if necessary
  5. setup Database Configuration, we are using MySQL Database as an example
  6. To Test the successful of Database configuration, click on the Test Connection Button. If connected to Database successfully, the below dialog will be shown.
  7. Select the outputpath and the application type for the usage of generated code
  8. Click on Close button when code and database generated successfully.

Using ORM generated code

  • We should create data for the tables first
    You may notice there is only setDepartment(...) for Student only.
//Create a College First
College college = CollegeFactory.createCollege();
college.setName("College A");

//Then create two department
Department department1 = DepartmentFactory.createDepartment();
Department1.setDept_id(1);
department1.setName("Department A");
department1.setCollege(college);

Department department2 = DepartmentFactory.createDepartment();
	department2.setDept_id(2);
department2.setName("Department B");
department2.setCollege(college);

//And add few student to each department
Student student1 = StudentFactory.createStudent();
student1.setAge(21);
student1.setGender('M');
student1.setName("Student A");
student1.setDepartment(department1);

Student student2 = StudentFactory.createStudent();
student2.setAge(21);
student2.setGender('F');
student2.setName("Student B");
student2.setDepartment(department2);

college.save();
  • Then we can load College and Department from Student
// Load a student from Database
Student student = StudentFactory.loadStudentByQuery("name='Student A'",null);
College college = student.getCollege1();
Department department = student.getDepartment();
	
System.out.println(college.getName());
System.out.println(department.getName());
  • And the result is...
College A
Department A

Downloads

 
 
Last modified: 2006/08/12 15:06
 
 
Home | Recent Topics | Highlights | UML Diagrams | Tips and Tricks | Object-Relational Mapping
visual-paradigm.com Home | Training Center | UML Center | VP Gallery | Discussion Forum | UML Open Directory