aspose file tools*
The moose likes Object Relational Mapping and the fly likes adding more than one entry 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 "adding more than one entry" Watch "adding more than one entry" New topic
Author

adding more than one entry

Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
this is my config file

this is my employee class


this is my main class


this is the query i used to make table


this code is working .
But my problem is when i try to reun this main file it gives error.



i need to run this file and enter data more than one time.what are the changes i have to done?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

Samanthi perera wrote:

this code is working .
But my problem is when i try to reun this main file it gives error.
i need to run this file and enter data more than one time.what are the changes i have to done?

Because you are using the same id value for each run. You should change the id each time you run the application.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
I found we have to use generator element.
can you tell me how to chang this class with generator element?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

You may use something like,

Check the JPA specification/documentation of the provider for more information and relevant annotations.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
where to put this value?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

You should annotate the Id field of your bean with this and some annotations for sequences etc.. I suggest you read the JPA Specification for more details.
ishan dave
Ranch Hand

Joined: Apr 16, 2007
Posts: 39
hi Samanthi,

the problem is at
# SEVERE: Duplicate entry '5' for key 1
# Jan 30, 2010 10:09:16 AM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
# SEVERE: Could not synchronize database state with session
# org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

for resolving this you need to change the id of you entity as you said. for that you need to use
@GeneratedValue annotation for generating your id every time you persist them.

it should be like this
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
Integer id;

this way your ids will be generated automatically, there some other strategies also available for Id generation like
Identity, Sequence and Table
here what I think is some are database dependent so Table strategy is the best one and most suitable to my requirement.

if you want know more about other attributes then please try some googling like Id generation in JPA or refer the docs or post reply and I will get back with more details

thank you,
ishaan


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Once the question is answered it's not really necessary to answer it again.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
i change class


but if i comment @GeneratedValue(strategy = GenerationType.AUTO) line it is working .
 
 
subject: adding more than one entry