3

Entity hold data which mapped to data from persistent storage. Usually this is DB.

You can hold ids:

class Book {
    private String title;
    public String getTitleId() { return title; }

    private Long publisherId;   // <==  **it is**!
    public Long getPublisherId() { return publisherId; }

    private List<Long> authorIds;
    public List<Long> getAuthorIds() { return authorIds; }
}

or resolved entities:

class Book {
    private String title;
    public String getTitleId() { return title; }

    private Publisher publisher;   // <==  **it is**!
    public Publisher getPublisher() { return publisher; }

    private List<Author> authors;
    public List<Author> getAuthors() { return authors; }
}

If you use ORM like Hibernate working with resolved entities is simple as ORM do *join*s with related entities for you for free.

But if you concern on performance or want to use DB features to move logic from Java to SQL with manual SQL queries then entities with ids win (they faster and it is annoying to join each related entities manually)!

So I decide to maintain two type of entities: with ids and with resolved related entities to hold this knowledge in data type (in type name).

So I start googling to get best suffixes for class names but can't find appropriated keywords. Please tell me how to name of methodology/tecnichue to hold ids vs related entities in entity!

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • 1
    This is a good question, and i don't know the answer. It would be useful to have a way of talking about this design decision. It could also be extended to the decision about whether to store references or inlined objects in XML documents, noSQL databases, etc. – Tom Anderson May 21 '13 at 13:45
  • 1
    As an aside, i would advise you not to have two types of entities. Rather, use the ability Hibernate gives you to work with IDs. If you mark all your relationships as lazy, then you should be able to get the ID of an object without requiring it to be loaded. – Tom Anderson May 21 '13 at 14:54

1 Answers1

3

I think the terms you are looking for are

Eager versus lazy loading and Fetching Strategies.

See

EDIT:

The lazy-loaded entity can be referred to as a proxy, a not-yet-initialized collection as a wrapper.

In reaction to the comments: See Hibernate one-to-one: getId() without fetching entire object. Hibernate can produce the ID values for lazy loaded entities.

From the Hibernate FAQ:

How can I retrieve the identifier of an associated object, without fetching the association?

Just do it. The following code does not result in any SELECT statement, even if the item association is lazy.

Long itemId = bid.getItem().getId();

This works if getItem() returns a proxy and if you mapped the identifier property with regular accessor methods. If you enabled direct field access for the id of an Item, the Item proxy will be initialized if you call getId(). This method is then treated like any other business method of the proxy, initialization is required if it is called.

Community
  • 1
  • 1
flup
  • 26,937
  • 7
  • 52
  • 74
  • Sorry, this is near but not exactly. I talk about holding **id** vs **entity** referenced by this id. So in SQL you have **field value** vs **join on another table**... In any case thanks for interest! – gavenkoa May 21 '13 at 13:40
  • 2
    @gavenkoa Also, might want to read the [Identify Field Pattern](http://martinfowler.com/eaaCatalog/identityField.html) and the [Proxy Pattern](http://www.martinfowler.com/eaaCatalog/lazyLoad.html) by Martin Fowler. – Sotirios Delimanolis May 21 '13 at 13:40
  • @SotiriosDelimanolis That what I am talking about! Thanks. +1 – gavenkoa May 21 '13 at 13:43
  • @gavenkoa That is what hibernate means by Lazy Loading. It only retrieves the id of the Object until its other fields need to be accessed. So you might want to give the answer to flup. – Sotirios Delimanolis May 21 '13 at 13:45
  • @SotiriosDelimanolis Mostly but not exactly same. Hibernate hide logic about ids. As I know you can't restrict Hibernate from loading of entirely object when you access it's id field... – gavenkoa May 21 '13 at 13:48
  • 1
    @gavenkoa I have updated the answer, Hibernate does allow you to query the IDs of not-yet-loaded entities. – flup May 21 '13 at 15:10