Question: I'm new to Hibernate and I've a basic question. I have two entity classes User and Comments with the relationship defined as One toMany from user to comments and manytoOne from comments to user. Now, if my UI sends me back some comments with comment\_text and user\_id what is the best way to save it? By defining @ManyToOne
 Private User user; 
I need to use the whole user object to store this information however, all I need is to store comment with user\_id in the table and query user information when I need it.

I'm new to Hibernate so it may be really just basics but can someone please help me?

Answer: 
The relationship between users and comments is many-to-many, so you can try to create a table with the following fields:

* key
* commenter
* commented
* comment-text
You question touches 2 very big points.

Having a fully connected database model is a recipe for low performance and quite some entanglement.

From a design point of view, you probably don't want to change a user when a comment is created, updated or deleted. So from Hibernate, probably you can skip the relationship between `Comment` and `User` and get the user information in another way when you load the comments (as I assume that you want to show a user name next to the comment). So when you receive a piece of text and a user id, you can store that straight away, but do remember to check that the user id is actually the one posting the comment, and not someone sending fabricated data.

Now thinking from the user point of view. Do you always need the comment information when you load a user? (Hibernate will load the comment data lazily, as all \*toMany relationships are lazy by default). From a design point of view, the more connected your entities are the more chances you have something will break when you do a small change.

Something that helps me in this situations is to find out what data is changed together or has interdependent constrains. If there are none, then those objects can be separated. And in the case you need to improve the performance, you could have some read-only entities that map all of the data you need for some of your views.

I'll suggest you to read the book [Implementing Domain-Driven Design](http://www.informit.com/store/implementing-domain-driven-design-9780321834577) as it touches on these topics and it's a fantastic source of good advice about design and happens to use Java+Hibernate in the examples!