[jboss-user] [JBoss Seam] - Dependent Fields in 2 Classes Best Practice?

gzoller do-not-reply at jboss.com
Fri Mar 16 17:24:10 EDT 2007


Hello,

For the purpose of this question assume I have 2 objects I'll show here extremely abbreviated and w/o all the annotations, but both are @Entity and all the persistence mappings work fine:


  | public class Project {
  |      private List<Expense> expenses;
  |      private int spentAmt;
  | 
  |      public List<Expense> getExpenses() { return expenses; }
  |      public void setExpenses( List<Expense> expenses ) { this.expenses=expenses;}
  | 
  |      public int getSpentAmt() { return spentAmt; }
  |      public void setSpentAmt( int spentAmt ) { this.spentAmt = spentAmt; }
  | }
  | 
  | public class Expense {
  |      private Project proj;
  |      private int amt;
  | }
  | 

So I have a Project with its list of associated Expenses.  Every time I add/change an expense I want to keep a running total in Project.  I could just say spentAmt is @Transient and use a query to tally up all the expenses, but for the sake of conversation assume this would be an expensive process (e.g. lots of expenses and frequent access to spentAmt property) so I'd rather maintain a pre-computed sum.

Are there any recommended ways keep this relationship between Project.spentAmt and Expense in sync, i.e. to know when an expense is truly initially created and edited (vs. just having its setters called upon marshalling the object from the db) so I can properly update Project.spentAmt?  

Here's what I have so far:

* I can catch original creation by overriding persist() in ExpenseHome.
* In Expense.setAmt() if I check whether proj is non-null (is Expense "wired"), it seems that it is not wired for marshalling, which I want to ignore, and is wired for edits, when I want to recompute Project.spentAmt.  

Are these observations safe assumptions or is there a better way?

Thanks for any advice.
Greg

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4028954#4028954

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4028954



More information about the jboss-user mailing list