• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate + Primary Key Violation

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rafael Fagundes
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic