0

I have to attend a "software engineering" exam this Friday, but there's a little thing I can't understand, and my teacher is unavailable for explaining.

I must make a project based on the Boundary-Controller-Entity (BCE) pattern, with persistence (database or sequential file, but I'm going to use SQLite). I should implement entity classes with save/load methods, which put/retrieve data in/from the persistence layer. They use DAO.

For example:

public class someEntity
{
   private int someData;

   public void set_someData (int _someData) {this.someData = _someData;}
   public int  get_someData ()              {return this.someData;}

   public void save ()
   {
      DAO dao = new DAO ();

      dao.openConnection ();
      dao.insert (this.someData);
      dao.closeConnection ();
   }

Thus far, it's all clear.

When I implement the "load" method, I want to "fill" my entity instance with data from the database, using search parameters (primary key in DB). But, if, for example, I want to get all people born before 1980, what can I do?

The only solution in my mind is to implement the following, into controller class

ArrayList <people> pList= new ArrayList ();
people p;

for (int i = 1900 ; i < 1980 ; i++)
{
  p = new people ();

  if (p.load (i) == true); //returns true if has found corresponding entry in db
  {
     pList.add(i);
  }
}

This is totally insane, is it not?

So, I need a "loader" method that returns a collection, so I can implement in the DAO a method that returns all people born before 1980, with a simple SQL query. Can this be done only if the control class talks directly with the DAO, ignoring BCE pattern rules.

How can I resolve the problem, if it is a real problem?

Carmine Ingaldi
  • 856
  • 9
  • 23

2 Answers2

0

Simply create a method in DAO to execute SQL query like:

SELECT * FROM people p WHERE p.born < '1980-01-01';

Don't forget to create index on born column.

Plus, in cafe of getting a single person who born before 1980 and worked in specific company.

You can use limit.

SELECT p.* FROM people p p.company = c.id WHERE p.born < '1980-01-01' and p.company_id = 'thatcompany' LIMIT 1

(suppose p.company_id is a FK Column)

dgregory
  • 1,397
  • 1
  • 12
  • 26
  • thanks for the answer :) .........ok, but when i do this, i get a resultSet with more-than-one entries, when i need only one. if i choos first resultSet entry and i load it into my entity instance, then i could need to repeat the operation: i create entity, call load method, and load method does that query. i would have same resultSet, i would choose second entry, but i just can't do it.....or not?. PS: sorry for my english, i know, sometimes i make horrific errors :D – Carmine Ingaldi Jul 02 '12 at 07:14
  • I'm so sorry but cannot understand what is exactly what you want to do. Yes, you may get multiple rows. Are you try to get a single person who born before 1980 and worked in specific company? – dgregory Jul 05 '12 at 04:14
  • yes because i should have one entity instance per each table entry, it's not? "load" method belongs to an entity class, not collection......but if i can use code below, (that is a search upon the empolyees container, not employees themself) i think the problem has resolved..... – Carmine Ingaldi Jul 05 '12 at 08:47
  • I just add a query to get a single person who born before 1980 and worked in specific company. Please take a look. – dgregory Jul 06 '12 at 02:11
0

I have been thinked about a solution:

i.e. I have got "employee" entity, and an "employee" works in a "company".

this can be represented like:

public class company
{
  private String name;
  private int employee_amount;
  private ArrayList <employee> works;

  //setter, getters and save methods...

  public void load (String _name)
  {
     DAO dao = new DAO ();
     employee e;

     dao.openConnection ();
     works = dao.searchByCompany (_name);    
     dao.closeConnection ();

     this.name = _name;
     this.employee_amount = e.length ();
  }

in DAO, there will be implmented searchByCompany method

public ArrayList <employee> searchByCompany (String name)
{
  ArrayList <employee> to_return = new ArrayList ();

  //stuff....

   rs = stmt.executeQuery ("SELECT * FROM Employees WHERE Company = '" + name + "'");

  //get rs fields and put everything into to_return

  return to_return;
}

So when i will need to search famouses employees born before '80, i put the following code into controller class

Company c = new Company;

c.load ("thatCompany");

for (int i = 0 ; i < c.getEmployee_amount () ; i++)
{
   // get ith employee from list
   // test if was born before '80
   //do something, else do other thing
}

is it right way to work?

Carmine Ingaldi
  • 856
  • 9
  • 23