Sisäänkirjautuminen| Uusi tunnus
FrontPage| Projects| Forums| Search|
^ DaoGen Forums: › Why? › PHP5? › Using ToString() with PHP › Next DaoGen version almost ready! › Create (insert) and Save (update) PHP examples › Support for numeric object types almost ready! › DaoGen 2.1.0 released! › Compliments and minor suggestion › variables within a class › Foreign key constraint in create() › Security issue!!! › auto-generated columns and create method › Oracle BLOB › DAO for cross-table queries › Please help me.. › Please use this forum for DaoGen related stuff › Call-time Pass-By-Reference › DaoGen 2.4.0 has been released! » How can I show relationships? › DaoGen & PHP 5 › PHP4 vs PHP5 and other considerations. › singleQuery example in php › Why are save and delete not synchronized? › Why are save and delete not synchronized? › DTD for generated XML › DaoGen 2.2.2 has been released! › DaoGen 2.0.2 is available. › For a thesis ?? › Extremely good idea ?? how can I ???? › Usability suggestions for DaoGen › Performance of DaoGen with Mapping tools › Performance with List › Some usage statistics › DaoGen 2.2.1 has been released! › Programming Examples are back! › DaoGen 2.0.1 is available. › Version 2.0.0 generates buggy PHP code! New version soon available! › Examples of DaoGen customizations › Code Examples
Sisäänkirjautuminen
» Käyttäjätunnus:
» Salasana:
» Muista kirjautuminen:
Suosituimmat sivut: › DaoGen 2.1.0 released! › Using ToString() with PHP › DaoGen Forums › Programming Examples › DaoGen Manual › DaoGen FAQ › Search › Projects › Forums › Version 2.0.0 generates buggy PHP code! New version soon available!
FrontPage » Forums » DaoGen Forums » How can I show relationships?

How can I show relationships?

Posted by anonymous 16.04.2004 05:33

hi,

I would liked to know what the best practice to represent master/details relationships with DAO.


Re: How can I show relationships?

Posted by Luigi 16.04.2004 10:38

> anonymous 16.04.2004 05:33
> I would liked to know what the best practice to represent master/details relationships with DAO.

This is an interesting question. I have done lot's of projects that had master/detail structures in the database design and usually I did use surrogare keys to handle the relationship. It is quite easy to implement with DAO components and in the Database.

So for COMPANY table, you have a column, say COMPANY_ID which is integer. Then you have a EMPLOYEE table which has EMPLOYEE_COMPANY column which contains the COMPANY_ID for the employee.

Now for your DAO components, generate the code both tables just like they were independent. Then handle the relationship in extending DAO classes or in the application logic. It is actually really easy - if you want to get a Company-object and the list of all employees for that company, you would do like this:

-----8<-------8<-------8<-------8<-------8<-------8<-------8<--------

// Create the DAO components:
CompanyDao companyHandler = new CompanyDao();
EmployeeDao employeeHandler = new EmployeeDao();

// get the database connection.
Connection conn = getConnection();

int myCompanyId = 1234;

// The COMPANY_ID is the primary key for COMPANY_TABLE:
Company myCompany = new Company(myCompanyId);
List employeeList = null;

try {
// Load the company object:
companyHandler.load(conn, myCompany);

// Prepare the search the employees:
Employee searchTemplate = new Employee();
searchTemplate.setCompanyId(myCompany.getCompanyId());

// Run the search, assuming that COMPANY_ID is *not* primary key for EMPLOYEE table.
employeeList = employeeHandler.searchMatching(conn, searchTemplate);

} catch (NotFoundException notFount) {
System.out.println("Company not found!");
} catch (SQLException error) {
System.out.println("Database error!");
}

// Now you have the all information about the Company in myCompany
// object and the List of all company empoyees in employeeList object.

-----8<-------8<-------8<-------8<-------8<-------8<-------8<--------

You can do it this way if you use DaoGen. If you use other DAO implementation, the procedure might be little different.

// Luigi.


Re: Re: How can I show relationships?

Posted by anonymous 22.04.2004 01:04

> Luigi 16.04.2004 10:38
> > anonymous 16.04.2004 05:33
> > I would liked to know what the best practice to represent master/details relationships with DAO.
>
> This is an interesting question. I have done lot's of projects that had master/detail structures in the database design and usually I did use surrogare keys to handle the relationship. It is quite easy to implement with DAO components and in the Database.
>
> So for COMPANY table, you have a column, say COMPANY_ID which is integer. Then you have a EMPLOYEE table which has EMPLOYEE_COMPANY column which contains the COMPANY_ID for the employee.
>
> Now for your DAO components, generate the code both tables just like they were independent. Then handle the relationship in extending DAO classes or in the application logic. It is actually really easy - if you want to get a Company-object and the list of all employees for that company, you would do like this:
>
> -----8<-------8<-------8<-------8<-------8<-------8<-------8<--------
>
> // Create the DAO components:
> CompanyDao companyHandler = new CompanyDao();
> EmployeeDao employeeHandler = new EmployeeDao();
>
> // get the database connection.
> Connection conn = getConnection();
>
> int myCompanyId = 1234;
>
> // The COMPANY_ID is the primary key for COMPANY_TABLE:
> Company myCompany = new Company(myCompanyId);
> List employeeList = null;
>
> try {
> // Load the company object:
> companyHandler.load(conn, myCompany);
>
> // Prepare the search the employees:
> Employee searchTemplate = new Employee();
> searchTemplate.setCompanyId(myCompany.getCompanyId());
>
> // Run the search, assuming that COMPANY_ID is *not* primary key for EMPLOYEE table.
> employeeList = employeeHandler.searchMatching(conn, searchTemplate);
>
> } catch (NotFoundException notFount) {
> System.out.println("Company not found!");
> } catch (SQLException error) {
> System.out.println("Database error!");
> }
>
> // Now you have the all information about the Company in myCompany
> // object and the List of all company empoyees in employeeList object.
>
> -----8<-------8<-------8<-------8<-------8<-------8<-------8<--------
>
> You can do it this way if you use DaoGen. If you use other DAO implementation, the procedure might be little different.
>
> // Luigi.
>

But, if my Company bean has the attribute employeeList ? Or manager?Can I modify the "singleQuery" method?


Re: How can I show relationships?

Posted by Luigi 22.04.2004 13:06

> anonymous 22.04.2004 01:04
>
> But, if my Company bean has the attribute employeeList ? Or manager?Can I modify the "singleQuery" method?

I would recommend that you do not modify the query-methods. Instead, you should extend the generated companyDao class and include reference to empoyeeDao. (Also, extend the company value object to include runtime-variable "List employees") Then implement new load-method which will first load the company and then load the employees into it like in the example. You can do all this with a few lines of code and if you just extend the Dao, you can later change the definition of the table(s) and generate new base classes and have it all work out of the box without touching your extensions.

BTW: This was really good question, since now that I had to explain the way to do this, I decided that the next DaoGen version will support the "List" attribute as a runtime variable and generate proper code for that.

// Luigi.