ORM: Keep entities coherent in a cache or retrieve entities each time?

We have a tricky question in a project using Java & Hibernate, with a model containing bi-directional relationships.

As it’s a small project with few users, few entities, and few rows involved in relationships (not more than 100 in 1-n relationships), we don’t really need a 2nd level cache.

Operations done with entities are simple: inserting, removing and performing some computations with entities involving relationships loading.

Let say we have two Java entities: Room and Furniture. An item of furniture is in one room and a room can have many items of furniture. So Furniture has a reference to Room, and Room has a list of Furniture.

For the Delete Furniture action, the easiest thing to do is to retrieve the right Furniture, and tell the EntityManager to remove the Furniture. But, to ensure the Java instances remain coherent, we should also remove the Furniture instance in the list of Furnitures in the associated Room. Unfortunately, if the Room instance is not loaded, a request will be done to the database for nothing if we don’t have a cache.

So, what would be a better choice between:

A) Keeping it simple

  • In general, no preoccupation about bilateral relationships in java instances coherence, as we don’t have 2nd level cache
  • Well document the fact that there is a cache per-action, so if specific computations must be done requering bilateral relationships coherence within an action, this action must take care of the coherence
  • No boilerplate code for bilateral relationships coherence management needed
  • No overhead of loading non-necessary entites
  • If a 2nd level cache must be added, then a big effort in refactoring has to be done

B) Keeping it coherent

  • Always maintain the bilateral relationship’s coherence
  • Enable 2nd level cache

1

Personally I’d do the simplest thing that could possibly work: Don’t auto-fix the bilateral relations.

I’ve previously developed and maintained a system that (among many other things) did exactly this: ensure that the relations between business objects where always in sync with what the DB would look like (i.e. if you remove one item, the other is updated accordingly). It was quite a complex piece of code and it was pretty hard to keep all the corner cases in mind (it didn’t help that this also supported nested transactions).

But if your business transactions are well defined and work pretty much independently from each other and each one is run in its own transaction, then all that complexity brings very little advantage.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật