views:

41

answers:

2

I have a table of NewsFeed.
NewsFeed
+ id
+ fromUserId
+ targetId
+ type

so the news is like this: Tom Wakefield commented on Peter Smith's profile.

So either I add two more fields into my NewsFeed call fromUserName and targetUserName

OR

For every row I display in my dataTable I would query from the Entity.

<p:dataTable value="#{myBean.news}" var="item">
    <p:column>
         <h:outputText value="#{myBean.getName(item.fromUserId)} " />
         <h:outputText value="commented on " />
         <h:outputText value="#{myBean.getName(item.targetId)}" />
    </p:column>
</p:dataTable>

then inside myBean.java

myBean.java

public String getName(Long userId){
    User u = mySessionBean.findUserById(userId);
    return u.getFName() + " " + u.getLName();
}

Which way is better? Does making query at every row really hurting me a lot in term of performance

Note: the database expect to have lot of users. News are display very often.

+1  A: 

"normalize until it hurts, denormalize until it works"

IOW: record only the IDs and use JOINs to get the whole data.

Javier
thank you for your input +1
Harry Pham
+1  A: 

Does making query at every row really hurting me a lot in term of performance

No.

Duplicating data causes more problems than it solves.

Duplicated relationships make updates nearly impossible to process correctly.

Object-Relational Managers can -- and often do -- generate proper SQL joins to optimize the fetches. Also ORM's have cache. And the database has cache.

The 2nd and 3rd normal forms amount to the following rule: Do Not Duplicate Relationships.

In rare cases you can "denormalize" and improve performance without creating complex update logic. In most other cases, the duplicated data leads to updates and inserts that are either either complex or slow or both.

S.Lott
Thank you for your input. I will take your advice. +1
Harry Pham