DimaSan
DimaSan

Reputation: 12684

How to pass object field in DAO?

I created DAOs for my entities. And now I have a problem: I need to pass another object's field in some methods. For example:

@Override
public void updateById(PriceTrack priceTrack, int id) throws DBException {
    PreparedStatement ps = null;
    try {
        conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        conn.setAutoCommit(false);
        ps = conn.prepareStatement(UPDATE_BY_ID);
        ps.setInt(1, priceTrack.getOldPrice());
        ps.setInt(2, priceTrack.getNewPrice());
        ps.setDate(3, new java.sql.Date(priceTrack.getLastUpdatedPrice().getTime()));
        // todo - pass Appartment id
        ps.setObject(4, new Apartment());
        ps.executeUpdate();
        conn.commit();
    } catch (SQLException e) {
        JdbcUtils.rollbackQuitely(conn);
        throw new DBException("Cannot execute SQL = '" + UPDATE_BY_ID + "'", e);
    } finally {
        JdbcUtils.closeQuitely(ps);
        JdbcUtils.closeQuitely(conn);
    }
}

Here I need to pass the corresponding Aparment_id value. Creating new Apartment() is wrong.

Content of UPDATE_BY_ID:

    public static final String UPDATE_BY_ID = "UPDATE price_track SET old_price=?, new_price=?, lastupdated_price=?, apartment_id=?";

What is the best way to do it?

Here is also the diagram, that shows connection. enter image description here

Upvotes: 0

Views: 823

Answers (3)

xrcwrn
xrcwrn

Reputation: 5325

Better way is to create object, initialize it and pass it to DAO. For that use POJO class:

class PriceTrack{
           int id;
           doulbe price;
          //and relation ship between price and apartment 1:1 or 1:n
            Set apartment=new HashSet(0);
         //getter and setter
     }

class Apartment{
           int id;
           int room;
           String description;
         //and relation ship between apartment and price 1:1 or 1:n
      }

public static void main(String s[])
{
   PriceTrack priceTrack=new PriceTrack();
  priceTrack.setId(23);
   priceTrack.setPrice(2389);
  Apartment a=new Apartment();
  a.setRoom(3);
  a.setDescription("good rooms");
  Set s=new HashSet();
  s.add(a);
  priceTrack.setApartment(s);
  //now send It to the DAO method
  DAO.updateById(priceTrack);
}

Upvotes: 1

Mudassar
Mudassar

Reputation: 3205

If you have a concern with the growing number of arguments there are possible solutions:

  1. To pass a map in which you can pass the number of arguments you want in the form of key-value pair as required
  2. To pass a object when the arguments being passed are many and basically a part of the form which correlate to fields of a class and supplied to you in the form of an populated object

Which case you go for depends on the way you are getting data and what is anticipation of getting more fields in future.

Upvotes: 1

Ihor Patsian
Ihor Patsian

Reputation: 1298

If you just want to update old_price, new_price or lastupdated_price it is not necessarily update aparment_id because it is foreign key. It is logical to assume that it won't change. You can remove aparment_id from your query:

UPDATE price_track SET old_price=?, new_price=?, lastupdated_price=?

And appropriately remove ps.setObject(4, new Apartment()); from your code.

JDBC requires manual configuration and it does nothing automatically.

Upvotes: 1

Related Questions