-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Is there a Trigger function in mapping of Hibernate3?
PostPosted: Thu Apr 27, 2006 4:54 am 
Newbie

Joined: Thu Apr 27, 2006 3:46 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hello,

I have two tables(dept2, emp2) as below.

--- from --

create table dept2
( deptno number
, deptname varchar2(10)
, salsum number ) ;

alter table dept2 add constraint dept2_pk primary key(deptno) ;

create table emp2
( empno number
, empname varchar2(10)
, sal number
, deptno number ) ;

alter table emp2 add constraint emp2_pk primary key(empno) ;
alter table emp2 add constraint emp2_fk foreign key(deptno) refrences dept2(deptno);

insert into dept2 values ( 10,'DEPT10', 100) ;
insert into dept2 values ( 20,'DEPT20', 200) ;
insert into dept2 values ( 30,'DEPT30', 300) ;

insert into emp2 values ( 11,'EMP11',100,10) ;
insert into emp2 values ( 21,'EMP21',100,20) ;
insert into emp2 values ( 22,'EMP22',100,20) ;
insert into emp2 values ( 31,'EMP31',100,30) ;
insert into emp2 values ( 32,'EMP32',100,30) ;
insert into emp2 values ( 33,'EMP33',100,30) ;

--- to --

The "SALSUM" column of DEPT2 table have the value,
that is total of the SAL of employee in the same department.

When the EMP2 table is changed using DML,
I would like to try that the SALSUM column automatically is updated,
like method of DB trigger.

Is it possible with mapping of Hibernate3?
Would you like to advice for me.

Thank you very much.

Hibernate version: 3.1.3

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using: Oracle9i

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 1:40 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
This belongs in business logic, not your persistence layer. After all, if you ran code like this, you would be most unhappy if DEPT2 was updated (or at least, many hibernate users, myself included, would be very unhappy):
Code:
Session s = createNewSession();
Employee e = s.get(EmployeeImpl.class, empId);
e.setPhoneNumber(phoneNumber);
s.flush();
So what you need to do is ensure that whenever a salary is updated/inserted/deleted, the corresponding Department is updated.

Two alternatives are to use a real DB trigger, or to drop the SALSUM column entirely and map it as a derived value:
Code:
<class name="Department" ...>
  ...
  <property name="SalarySum" formula="(select sum(e.sal) from emp2 e where e.deptno = deptno"/>
  ...
</class>
The deptno (no table specified) will have the alias for the dept2 table prepended when the department is loaded. So it will always be correct on loading. If you need to check that a department object's salary is up-to-date, you can call Session.refresh on the department.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.