Reputation: 613
I'm currently developing an android application, and because i need to access a database, i've decided to use the dao pattern. Currently, i understand the concepts of this pattern.
In my app, i have my business objects. For example, let's consider the order and the payment objects. Let's assume that an order as a variable of the type payment. In the database, each record of the table orders will have a foreign key for the payments table, that keeps the associacion between the order and it's payment. The problem is, as dao's should be completely independent from each other, the OrderDao won't "know" the PaymentDao, so how can i instantiate a order object, with the corresponding payment instance in it? This is making me really confused... The OrderDao won't be able to return a order instance, because this instance needs a payment instance.. I could do some "tricks", like the OrderDao returns a instance with a payment initialized with only its id, and later in the business logic, grab that id and use the PaymentDao to retrieve the payment instance and set it to the previous order... But this doesn't sound that good..
How should this be done?
Just to make my explanation easy about the classes structure:
public class Order {
private int id;
private Payment payment;
......
}
public class Payment {
private int id;
....
}
The reason i'm not using any framework, is because in my database, i need some table that hold some strings translated.. For example, if we had a categories table, i would have a categories_i18n table, that for each record in the categories table, would have the corresponding translation in some languages..
Thanks in advance.
Upvotes: 0
Views: 2100
Reputation: 1774
I think, You are modelling the Class Relationships wrongly in the Database. As per your requirements Your Order Class is having instance of Payment Class so when you map to the database it should be Payment that will have Order ID as the foreign key.
Once you map it in this way. You can first get the Order from OrderDAO and then you can get Payment from PaymentDAO by using getPaymentByOrderID method. In this way your DAO will be independent and you will achieve the desired functionality.
Upvotes: 1
Reputation: 236
There is nothing wrong with your DAO's returning Object graphs. I use Hibernate for ORM and to take your example, I would have a PaymentDao which has methods for dealing with Payment objects (findById, persist, etc) and an OrderDao for dealing with Order objects.
However, if i retrieve an Order object form the OrderDao, then it will still contain a reference to the relevant PaymentObject (you can lazily load these sub-objects with Hibernate, so no need to worry about the performance overhead of returning huge object graphs - you will need to investigate your chosen persistence framework to help you here).
HTH
PS Example of an Order entity with a Company reference in hibernate:
@Entity
@Table(name = "ORDERS")
@SequenceGenerator(allocationSize = 1, name = "orderId", sequenceName = "ORDERID")
public class Order extends OptimisticLockingNumericIdDto<Order> implements Serializable {
private Long id;
private Company managingCompany;
...
@ManyToOne(targetEntity = Company.class)
@JoinColumn(name = "COMPANYID")
public Company getManagingCompany() {
return managingCompany;
}
public void setManagingCompany(Company managingCompany) {
this.managingCompany = managingCompany;
}
}
Upvotes: 3