Bick
Bick

Reputation: 18551

manage a sequence in hibernate

I have a DB table with id as primary key.
In the object I have define the colomn getter using @Id
When the user creates a new row it does assign it a new Id.
but it also assign a new id to the old object.

thus, when I iterate a list using

for(){
    session.save(myO);
}

I get new rows in the db for the new elements but also new rows for the old ekements. Any assistance? Thanks.

Upvotes: 0

Views: 116

Answers (2)

Riddhish.Chaudhari
Riddhish.Chaudhari

Reputation: 853

first create sequence in database like this given below

CREATE SEQUENCE SEQ_MFILTERGROUP MINVALUE 1 MAXVALUE 99 INCREMENT BY 1 START WITH 1 CACHE 20;

<class name="example.FilterGroup" table="FILTERGROUP">

<id name="id">

generator class="sequence">

<param name="sequence"> SEQ_MFILTERGROUP </param>

</generator>

</id>

</class>

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692151

The javadoc says:

Persist the given transient instance, first assigning a generated identifier.

You should call save on an existing, detached instance. save is to persist a new entity. Use saveOrUpdate or merge.

Upvotes: 1

Related Questions