1

Account class:

@Entity

@Table(name = "Account")

public class Account {

...

private String accountId;
...

@Id
@GeneratedValue
@Column(name = "accountId")
public String getAccountId() {
    return accountId;
}

public void setAccountId(String accountId) {
    this.accountId = accountId;
}

...

if i try to do that:

temp = new Account();

temp.setAccountId(tempId); //
session.save(temp);
System.out.println("accountid...." + temp.getAccountId());

it prints an accountId which is not equal to tempId, i think its because it is a auto-increment field in the table

on the other hand if i do that:

temp = new Account();

session.save(temp);
temp.setAccountId(tempId);
session.merge(temp);

i get exception:

org.hibernate.HibernateException: identifier of an instance 
of ... was altered from 1244 to 1221

how can i override the id?

  • Here is something [http://stackoverflow.com/questions/89439/bypass-generatedvalue-in-hibernate] – subodh Feb 07 '13 at 12:57

3 Answers3

0

You don't have to set the id, hibernate will automatically assign the id (according to the strategy defined with the generated value annotation) when you persist your entity.

Not sure, but you are using a String value for your idea, how can it be incremented ? Don't you want to use a Long instead ?

Mickael
  • 5,711
  • 2
  • 26
  • 22
  • i need to save a specific id, so the assigned "auto increment" id is not wanted in this part of code (it is used for else where) –  Feb 07 '13 at 12:00
  • the db defines the id as big int, but i used string in the code, i don't think it's what causing the problem, because when i use "getAccountId()" it works well –  Feb 07 '13 at 12:03
0

First, You must not assign values to a generated field. It will be replaced by generated one. Its an expected behavior.

Second, you must not change id. All entities mapped to database by their ids. If you change an id, its a new entity. You cannot update it, you must save it.

I do not think any of JPA implementation has overriding id mechanism. If you want to set id by yourself its OK, then remove GeneratedValue annotation and do the same thing for your persistance store. Then you can save your values as IDs.

bhdrkn
  • 6,244
  • 5
  • 35
  • 42
0

If you want to work with a specific id and there is already an entity with that id on database, you must get the entity from session first:

Account a = session.get(Account.class, id);

Then make changes and merge.

Can Yegane
  • 514
  • 4
  • 11
  • 1
    the main issue is that the entity is no longer in the DB (it was deleted somehow) and i want to recreate it... –  Feb 07 '13 at 13:54
  • 1
    If you are sure that the id that you want the entity to have is unique, then you can use a jdbc statement to insert a record with that id manually, but as far as I know you can't do that using hibernate as @GeneratedValue will simply create a new value for you if you do an insert operation. – Can Yegane Feb 07 '13 at 15:09
  • Also `@Id` annotation will prevent you from updating the inserted entity's accountId column after insertion. – Can Yegane Feb 07 '13 at 15:16