aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate + Primary Key Violation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate + Primary Key Violation" Watch "Hibernate + Primary Key Violation" New topic
Author

Hibernate + Primary Key Violation

Rafael Fagundes
Ranch Hand

Joined: Sep 23, 2004
Posts: 52
Hello all!

I�m getting the following error:

org.hibernate.exception.GenericJDBCException: could not insert: [Table4VO]

And this is the reason:

SEVERE: [Atinav][JDBC SQL Server Driver:Ver C3.0C] Violation of PRIMARY KEY constraint 'PK'. Cannot insert duplicate key in object 'Table4'.

// ---------------------

MY CENARIO IS:

#1. My rellational mapping it�s like:

Table1VO (Major table whith many to one relationship for Table2VO where
cascade="save-update")

Table2VO (Table whith many to one relationship for Table3VO where
cascade="save-update")

Table3VO (Table whith one to one relationship (bi-direcional association)
for Table4VO where cascade="save-update")

Table4VO (Table whith one to one relationship for Table3VO where
cascade="none" and constrained="true"; the primary key
is a FK: PrimaryKey from Table3)

#2. For updating operation, all the objects instances for the rellational mapping above are duly instantiated. For example:

1 Table1VO has 1 Table2VO that has 1 Table3VO with 1 Table4VO (like in database....... that�s mean, a perfect update)

// ----------

WHAT I WANT it�s to include a new Table3VO object that will generate an insert not only on Table3 but in Table4 also (because the rellationship mentioned above).

// ----------

PROBLEM:

For the collection of Table3VO objects in Table2VO (many-to-one) object, the one that already exists on the database should be updated and the new instance should be inserted. THE SAME THING SHOULD HAPPEN FOR THE Table4!!! But the framework it�s trying to do an insert with the object that already exists on Table4 and that�s is throwing the exception off violation off primary key (even with the right instance of the Table4VO in the respective Table3VO obj.).

Does somebody know what is haaping? Or the reasons???

Thank�s for your attention.

PS: sorry for my bad english!!!


Thanks,
Rafa
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

This is usually related to your inverse mapping. You need to make sure it is there and on the right side. So you have a relationship between A and B, and it is bi-directional. So you have links form A to B and from B to A, but you didn't map one side as inverse. you have to do this for bi-directional mapping because those two sides mapping, Hibernate does not know that they are mapping the same relationship, hence the need for the inverse mapping.

What is you mapping between the two classes to the two tables?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Rafael Fagundes
Ranch Hand

Joined: Sep 23, 2004
Posts: 52
Originally posted by Mark Spritzler:
This is usually related to your inverse mapping. You need to make sure it is there and on the right side. So you have a relationship between A and B, and it is bi-directional. So you have links form A to B and from B to A, but you didn't map one side as inverse. you have to do this for bi-directional mapping because those two sides mapping, Hibernate does not know that they are mapping the same relationship, hence the need for the inverse mapping.

What is you mapping between the two classes to the two tables?

Mark


Thank�s for your attention Mark. The mapping I�ve done it was right!!!
I�ve found the solution as follows:

Table3VO {

...

public Table4VO getTable4VO() {

if(this.table4VO == null)
this.table4VO = new Table4VO();

return table4VO;
}

public void setTable4VO (Table4VO table4VO) {

if(table4VO!= null && this != table4VO.getTable3VO())
table4VO.setTable3VO(this); // It�s needed to assure the relation
this.table4VO = table4VO; // define int the mapping
}

}
}

Table4VO {

...

public Table3VO getTable3VO() {

if(this.table3VO == null)
this.table3VO = new Table3VO();

return table3VO;
}

public void setTable3VO (Table3VO table3VO) {

if(table3VO!= null && this != table3VO.getTable4VO())
table3VO.setTable4VO(this); // It�s needed to assure the relation
this.table3VO = table3VO; // define int the mapping
}

}
}

I�m sorry if it was a little bit confused my example!!!

Cheers.
 
 
subject: Hibernate + Primary Key Violation