JPA - Returning an Auto Generated Id After Persist()

JPA - Returning an auto generated id after persist()

The ID is only guaranteed to be generated at flush time. Persisting an entity only makes it "attached" to the persistence context. So, either flush the entity manager explicitely:

em.persist(abc);
em.flush();
return abc.getId();

or return the entity itself rather than its ID. When the transaction ends, the flush will happen, and users of the entity outside of the transaction will thus see the generated ID in the entity.

@Override
public ABC addNewABC(ABC abc) {
abcDao.insertABC(abc);
return abc;
}

How to get auto generated Id after persist into the mysqlDB Using JPA

This is a long shot, but I'll suggest it anyway.

You have declared the field private long tradetransactionid; which is a primitive type. This means it does not allow nulls and is implicitly 0 on instantiation. When you persist the object in the database, you try to insert an item/object with id equal to 0.
You could try private Long tradetransactionid;, which is a reference type. This way it is initially null and can be set by auto-generation.

Get database generated ID after persist in Hibernate

Here you have a solution:

ENTITY:

@Entity
@Table(name = "TableName")
public class TableNameClass {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", insertable = false, nullable = false, unique = true, updatable = false)
private int ID;

...
}

CREATE FUNCTION

TableNameClass tnc = new TableNameClass();
tnc.setName("Solution");
Session s = HibernateUtil.getSessionFactory().openSession();
Transaction t = s.beginTransaction();
Integer myID = (Integer)s.save(tnc);
t.commit();
s.close();
System.out.println(myID);

As you can see, I've changed the @Generated annotation for @GeneratedValue(strategy = GenerationType.IDENTITY). I don't know if both of them represent the same, but it works.

Trying to persist a new (unmanaged) entity with auto generated ID causes error

Use identity for generating ID by DB:

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

Do not add optional=false, nullable=false. Primary key will be never null in RDBS.

How to get auto generated id From jpa repo after saving data?

if entity is properly configured to generate id, you can take object returned by save method.

tg=targetGroupRepository.save(tg);

How to get Id of last persisted entity using JPA

persist is not guaranteed to generate the ID. The ID is guaranteed to be generated at flush time only. So if you really need the ID before the transaction ends (and the entity manager is thus flushed), call flush() explicitely to get the ID:

MyEntity en = new MyEntity();
en.setName("My name");
em.persist(en);
em.flush();
System.out.println(en.getId());

Why JPA persist() does not generated auto-increment primary ID?

We are also using SQL Server 2008 and it never worked for me so I always execute separate query "SELECT @@IDENTY" to get the inserted id.

The reason I found on the net was that auto id (IDENTITY) is managed by database and never fetched in Entity until unless you commit the row or manually retrieve the info from database.



Related Topics



Leave a reply



Submit