[英]Dao and Service of a complex object
所以我有這個復雜的對象,例如:
@Entity
public class House {
private Kitchen kitchen;
private LivingRoom livingRoom;
private MainBedRoom mainBedRoom;
private Toilet toilet;
etc...
}
現在,House類中的每個對象(Kitchen,LivingRoom等)都非常復雜,帶有非原始字段,而沒有。
我有以下服務:
@Service
public HouseService {
}
@Service
public KitchenService{
}
.. 等等..
我的問題是:
如果我在HouseService中創建一個新的House對象,是否需要為該創建方法使用其他服務?
看來我什至根本不需要所有其他服務,因為House是所有對象的根,而我需要的只是一個(顯然很大的)HouseService? 那正確嗎?
即:
HouseService.createNewHouse {
House house = new House(...);
Kitchen kitchen = new Kitchen(...);
...
house.setKitchen(kitchen);
this.dao.save(house);
return house;
}
}
盡管可以,但不要讓生活變得復雜。 我假設您為每個Entity類都有多個DAO。 只需@Inject
(或@Autowired) and use them directly in each
Service`類中@Autowired) and use them directly in each
。
您的@Service
類應該已經被@Transactional
因此通過其他@Service
類不會獲得任何收益(除非有特定的業務邏輯)。
代替
@Service
public class FirstService {
@Autowired
private SecondService secondService;
@Autowired
private FirstDao firstDao;
@Transactional
public void saveFirst(First first) {
secondService.saveSecond(first.getSecond());
firstDao.save(first);
}
}
@Service
public class SecondService {
@Autowired
private SecondDao secondDao ;
@Transactional
public void saveSecond(Second second) {
secondDao .save(second);
}
}
直接去DAO
@Service
public class FirstService {
@Autowired
private SecondDao secondDao;
@Autowired
private FirstDao firstDao;
@Transactional
public void saveFirst(First first) {
secondDao.save(first.getSecond());
firstDao.save(first);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.