• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate and DB triggers

 
Shubhra Jyotsana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using insert triggers to do this?

Can you post your insert code?
 
Shubhra Jyotsana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please people..I need an urgent solution. I am stuck badly I would really appreciate any help
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your code will persist the entity in whatever state it is in. Have to populated the association?
 
Shubhra Jyotsana
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally I found solution for dealing with triggers in hibernate. It worked for me, hope it will help somebody else.

Here is the link.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic