TitanicLinux.Net :: 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.



viimeisin päivitys: 07.07.2009 00:28 Author: TitanicLinux.Net
Copyright TitanicLinux.Net 2003-2010