mardi 8 mai 2018

Id based Entities or Object(mapping) based with Spring Data, JPA?

Which is the better approach? Let's say I have these two entities:

Approach A :

class Survey {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    int id;

    @Column(name = "parent_id")
    int parentId;
    ...
}

Approach B :

class Survey {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    int id;

    @JoinColumn(name = "parent_id", referencedColumnName = "id")
    @ManyToOne(optional = true)
    Survey parent;
    ...
}

I am going to have lots of these types of relationships. Even with LAZY fetch strategy, B has following disadvantages :

  1. Caching of entities is not straightforward. Lazy loaded data (will/should it be cached or not?)
  2. If I ever need to serialize/deserialize these entities with jackson, this won't be straightforward.

I am inclined towards approach A, couldn't find any guidelines/comparisons of these two approaches. Any comparison or tips over which approach is preferable? Any major drawbacks/performance impact of approach A over B.

P.S. : This might be a stupid question but still I had to ask.

Aucun commentaire:

Enregistrer un commentaire