aspose file tools*
The moose likes Object Relational Mapping and the fly likes hibernate and DB triggers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate and DB triggers" Watch "hibernate and DB triggers" New topic
Author

hibernate and DB triggers

Shubhra Jyotsana
Greenhorn

Joined: May 03, 2011
Posts: 6
Hello,
I am using hibernate and pre-insert triggers in database. Let me explain the scenario.
I have two tables say A and B. In both the tables Primary keys are inserted through pre-insert triggers. Primary key from A is foreign key in B. So when I insert into these tables the foreign key column in B should get populated with triggered value of A (the primary key value). But its not happening as I am expecting. Primary keys in both tables are inserted properly but the foreign key column keeps getting value 0 instead of the triggered value which it should actually get.
Table have one-to-many relationship.

The two tables are like -
class Employee {
private int RECORDID;
@OneToMany(cascade=cascadeType.ALL)
@JoinColumn(name="MASTERRECORDID" , referencedColumnName="RECORDID")
private Collection<EmployeeDetails> employeeDetails = new ArrayList<EmployeeDetails>();
}

class EmployeeDetails{
private int RECORDID;
private int MASTERRECORDID;
}

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Why are you using insert triggers to do this?

Can you post your insert code?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Shubhra Jyotsana
Greenhorn

Joined: May 03, 2011
Posts: 6
Thanks for quick response

Actually the project I am working on was previously using JDBC and now we are shifting to hibernate. Tables use pre-insert triggers for generating primary keys values and now i need to manage everything in hibernate plus i am not supposed to make any changes in database .

For insert i am using this method :

public void persist(E entity) {
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(entity);
tx.commit();
}

Shubhra Jyotsana
Greenhorn

Joined: May 03, 2011
Posts: 6
Please people..I need an urgent solution. I am stuck badly I would really appreciate any help
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

So your code will persist the entity in whatever state it is in. Have to populated the association?
Shubhra Jyotsana
Greenhorn

Joined: May 03, 2011
Posts: 6
Yes, but when I try to persist the association this happens:
The value is correctly triggered , but mapped column is not getting this value.
I think the problem is that hibernate does not refer to DB table for inserting mapped column value rather it it consulting the Entity object which obviously has 0 value.

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes - but why should Hibernate read the database for an insert? An insert means there is no data there, so nothing to read.

The easiest fix sounds like dropping the triggers to me. If you really can't do that you could map separate entities specifically for inserting and mark the FK property as read only. Then Hibernate should never update it. Not nice though.
Shubhra Jyotsana
Greenhorn

Joined: May 03, 2011
Posts: 6
Thanks for the feedback. I think it will be best to drop triggers as mapping entities separately will not solve my purpose.
Shubhra Jyotsana
Greenhorn

Joined: May 03, 2011
Posts: 6
Finally I found solution for dealing with triggers in hibernate. It worked for me, hope it will help somebody else.

Here is the link.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate and DB triggers