0

In hibernate we can map entity relationships with one to one, one to many, etc. I am little bit skeptical about using the relationship annotations and I prefer to use individual find methods to retrieve child records. Example,

Lets consider I have two tables, User and Roles. Each user can have one role. So the entities are,

class User {
   @Column
   private String name;

   @OneToOne(mappedBy="role_id")
   private Role role;
   .... getter/setter....
}

class Role {
  ...
}

Either we have to make eager fetch or it will lead to lazy initialisation exception if the role is accessed outside of the current session.

Instead of this, shall we have the mapping like this?

class User {
   @Column
   private String name;

   @Column
   private Long roleId;
   ....
}

This way, whenever we need the role details, we can get the role_id from the User object and query the role table? Is this a right approach? Yes, I know the benefit of loading object graph, but I think this approach will avoid the unnecessary eager fetches and will run seamlessly if we do database partitions.

(I always consider databases as just datastore and use use individual queries to retrieve data instead of using joins to avoid load on the DB).

Please let me know your thoughts.

6
  • For sure, it's possible. It might be a solution in case you've serious performance issue. Unfortunately you lose the relation between the objects: looking at the DB it might be clear that Users and Roles are related (if there's a foreign key, for example), but if one of the developers looks at the code only, he could miss that relation. Oct 9, 2015 at 8:13
  • I think you're way off the mark here, especially in the end when you say "instead of using joins to avoid load on the DB". It means you haven't made any serious database work, since joins are not only necessary, they're a beautiful thing. Considering that you're not a database expert, I would be very careful when you come up with a "hey, why not do it this way?". You're very likely to be wrong. Note that I'm not insulting you, I'm just stating the facts.
    – Kayaman
    Oct 9, 2015 at 8:21
  • why not relation? I m not understanding the problem? are they eagerly fetched, than make them lazy and fetch all the values using HQL with fetch. problem? Oct 9, 2015 at 8:52
  • @Kayaman yes, I am not a database expert. But I have seen DBs get severe performance hit by more complex joins when on the heavy load. I may be wrong, just wanted to understand others thoughts on this. My assumption is, there is a performance difference between querying a table with the primary key (simple query) vs query with joins. Please correct me if I am wrong.
    – Raj
    Oct 9, 2015 at 9:01
  • @OAD its not a problem, sorry if it sounded like one. This question is more about understanding others experiences/thoughts.
    – Raj
    Oct 9, 2015 at 9:03

1 Answer 1

2

I suggest using

class User {
   @Column
   private String name;

   @OneToOne(mappedBy="role_id")
   private Role role;
   .... getter/setter....
}

class Role {
  ...
}

Don't see point in calling database select twice. Database can handel joins good and do select very fast so I don't see point to manually do it. Also when using this approach you can easy save/update/delete objects.

To avoid lazy initialisation exception you can use Hibernate.initialize(Object obj) as explained here How Hibernate.initialize() works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.