[EJB 3.0] - merge one-to-one strategy
by lpiccoli
hi all,
I am having troubles understanding the EJB3 updating strategy. I am familar with the traditional mechanism of dealing with just FK when updating a one-to-one relationship. However with EJB3 the FK is now a object.
>From the EJB3trail there is a example that does the following for a persist.
It loads both foreign key objects before setting, then persisting.
| public double calculate (int fundId, int investorId, double saving) {
| 26
| 27 Investor investor =
| 28 em.find(Investor.class,
| 29 Integer.valueOf(investorId));
| 30 Fund fund =
| 31 em.find(Fund.class,
| 32 Integer.valueOf(fundId));
| 33
| 34 int start = investor.getStartAge();
| 35 int end = investor.getEndAge();
| 36 double growthrate = fund.getGrowthrate();
| 37
| 38 double tmp = Math.pow(1. + growthrate / 12., 12. * (end - start) + 1);
| 39 double result = saving * 12. * (tmp - 1) / growthrate;
| 40 Timestamp ts = new Timestamp (System.currentTimeMillis());
| 41
| 42 TimedRecord rec =
| 43 new TimedRecord (fund, investor, saving, result, ts);
| 44 em.persist (rec);
| 45
| 46 return result;
| 47 }
|
Is the same mechanism required when updating?
Unfortunetly the EJB3trail does not have a one-to-one merge example.
Is something like the following performant or are there better mechanisms?
The major issue is executing the load of the FK relationship object. Which seems expensive when i already have the FK id.
|
| strategy 1
|
| public double update (int recId, int fundId, int investorId, double saving) {
|
| Investor investor =
| em.find(Investor.class,
| Integer.valueOf(investorId));
| Fund fund =
| em.find(Fund.class,
| Integer.valueOf(fundId));
| TimeRecord rec =
| em.find(Fund.class,
| Integer.valueOf(fundId));
|
| rec.setSaving( saving);
| rec.setFund( fund );
| rec.setInvestor( investor );
| em.merge (rec);
|
| return result;
| }
|
I have tried the following with little success as it complains about transient Objects
|
| strategy 2 not working.
|
| public double update2 (int recId, int fundId, int investorId, double saving) {
|
| Investor investor =new Investor( investorId);
| Fund fund = new Fund( fundId);
| TimeRecord rec =
| em.find(Fund.class,
| Integer.valueOf(fundId));
|
| rec.setSaving( saving);
| rec.setFund( fund );
| rec.setInvestor( investor );
| em.merge (rec);
|
| return result;
| }
|
|
So my question is does the merge on one-to-one relationship require fullly populated detached objects or can it be done with just the id for the FK?
many thanks
-lp
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3982272#3982272
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3982272
18Â years, 1Â month
[JBoss jBPM] - Associating variables to task instances
by john.rojas
Hi,
I am having difficulty associating variables with task instances. I am using hypersonic to test my code and when I check the JBPM_VARIABLEINSTANCE table, the TASKINSTANCE_ column is null.
My process definition loops over the same task node multiple times. Each time the token reaches the task node, a new task instance is created, but I cannot find the variable instance "var1" for each task instance.
For now, I am concatinating the task instance id to the variable name, but from reading the documentation, I thought that taskInstance.setVariable( "var name", object ) would assign "var name" to that task instance, and assigning "var name" to another task instance would not change the value of the 1st assignment.
Thanks in advance for your help.
Regards,
John
The task action handler is shown below:
| public static class TaskNodeAction implements ActionHandler
| {
| private static final long serialVersionUID = 1L;
| String value1;
| public void execute(ExecutionContext context) throws Exception
| {
| TaskInstance taskInstance = context.getTaskInstance();
| if( taskInstance == null )
| logger.error( "Got null taskInstance in node=" + context.getNode().getFullyQualifiedName() );
| else
| {
| // load custom variables and start it.
| taskInstance.setVariable( "var1", value1 );
| taskInstance.start();
| }
| }
| }
|
The task node out of the process definition:
| <task-node name="Looping Task">
| <task name="Looping Task">
| <event type='task-create'>
| <action class='TaskNodeAction'>
| <value1>1234</value1>
| </action>
| </event>
| </task>
| <transition name="" to="next transition"></transition>
| </task-node>
|
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3982268#3982268
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3982268
18Â years, 1Â month