This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes JPA primary key doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA primary key doubt" Watch "JPA primary key doubt" New topic
Author

JPA primary key doubt

Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

Hi,

I have just started to learn JPA and tried a simple program which inserts data into the table. one of the columns in the table has a primary key. I assume, JPA doesn't allow duplicate values and throws exception. ( My undersatnding might be wrong ). But I find the exception coming from DB, which means it reaches out to the DB and throws the error. Is this what actually happens ?


Ashwin Sridhar
SCJP | SCWCD | OCA
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Try using @Id for the primary key field


Programming is about thinking, NOT coding
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

If one of the columns is a primary key the database will not allow duplicates, it will result in an exception being thrown. I cannot comment on what JPA will do, because you have not provided us with enough information. Please read TellTheDetails. Without knowing anything about how things are mapped we cannot give you any answers.


[How To Ask Questions][Read before you PM me]
Ashwin Sridhar
Ranch Hand

Joined: Jul 09, 2011
Posts: 272

I have used @Id for one of the elements in the model.

Please read TellTheDetails. Without knowing anything about how things are mapped we cannot give you any answers.


I done have any exception or error. I just want to clarify my understanding here. so I haven't posted any of my codes.

I thought JPA stops up front when there is a duplicate record. Or probably it stops upfront when there is a duplicate record within the batch that is persisted at that moment. I would try this which struck me now, and post back.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

The @Id on the object uniquely identify the object within the persistence context (it is in essence the primary key). Only one object with a given id can exist within the persistence context at a time. Now typically this will match the primary key and unique constraints on the table as well but this is not enforced.


The developer still needs to be smart about things.

Given the following scenario:

Table has a primary key column named Id.
Entity mapped to that table has an @Id field mapped to the column id in the table.

if you try to call entityManager.persist() on an entity with a particular id and that id already is present in the database, JPA will attempt to insert it and an exception will be thrown. There are other operations like merge for updating entities.

Does that answer your question?

 
Consider Paul's rocket mass heater.
 
subject: JPA primary key doubt