ChatGPT解决这个技术问题 Extra ChatGPT

JPA - Returning an auto generated id after persist()

I'm using JPA (EclipseLink) and Spring. Say I have a simple entity with an auto-generated ID:

@Entity
public class ABC implements Serializable {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     private int id;

     // ...
}

In my DAO class, I have an insert method that calls persist() on this entity. I want the method to return the generated ID for the new entity, but when I test it, it returns 0 instead.

public class ABCDao {
    @PersistenceContext
    EntityManager em;

    @Transactional(readOnly=false)
    public int insertABC(ABC abc) {
         em.persist(abc);
         // I WANT TO RETURN THE AUTO-GENERATED ID OF abc
         // HOW CAN I DO IT?
         return abc.id; // ???
    }
}

I also have a service class that wraps the DAO, if that makes a difference:

public class ABCService {
    @Resource(name="ABCDao")
    ABCDao abcDao;

    public int addNewABC(ABC abc) {
         return abcDao.insertABC(abc);
    }
}
Similar one, can refer stackoverflow.com/q/3328813/366964
Thanks for the answers. And as a tricky solution (not a JPA) we can use another unique id like unix timestamp.
Please refer the below link, looks like the same issue stackoverflow.com/questions/9732453/…

J
JB Nizet

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;
}

NB: this needs to anotate the id field with @GeneratedValue - whatever that entails
Can u please explain the issues in trying to achieve this with composite id stackoverflow.com/questions/31362100/…
Is there a performance penalty (or any other negative effects) of manually flushing after persisting?
Yes, there is: unnecessary roundtrip to the database if the transaction ends up being rollbacked, potential exceptions if the persisted entity (or other flushed entities) is not in a valid state yet. A sequence or uuid generator is simpler and more efficient, and doesn't have these problems because the ID is generated and assigned before the entity is written to the database.
@JBNizet, do you need to return the instance or is the passed reference still valid? I mean, does insertABC create a new object? Or modify the old one?
u
utkal patel
@Entity
public class ABC implements Serializable {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     private int id;   
}

check that @GeneratedValue notation is there in your entity class.This tells JPA about your entity property auto-generated behavior


K
Koray Tugay

This is how I did it:

EntityManager entityManager = getEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(object);
transaction.commit();
long id = object.getId();
entityManager.close();

Its not working giving zero as a Return after persisting the data into table. either refresh is not working at this case .. What should i do for this. please suggest a way... Thank you
@VikrantKashyap Please post a new question with small code and mention me so I can take a look.
J
James

You could also use GenerationType.TABLE instead of IDENTITY which is only available after the insert.


Just a word of caution. When I tried GenerationType.TABLE, it created a separate table named hibernate_sequences and restarted the sequence from 1.
e
emecas

Another option compatible to 4.0:

Before committing the changes, you can recover the new CayenneDataObject object(s) from the collection associated to the context, like this:

CayenneDataObject dataObjectsCollection = (CayenneDataObject)cayenneContext.newObjects();

then access the ObjectId for each one in the collection, like:

ObjectId objectId = dataObject.getObjectId();

Finally you can iterate under the values, where usually the generated-id is going to be the first one of the values (for a single column key) in the Map returned by getIdSnapshot(), it contains also the column name(s) associated to the PK as key(s):

objectId.getIdSnapshot().values()

A
Anh Khoa

This is how I did it. You can try

    public class ABCService {
    @Resource(name="ABCDao")
    ABCDao abcDao;

    public int addNewABC(ABC abc) {
         ABC.setId(0);
         return abcDao.insertABC(abc);
    }
}

So every time you insert something on DB you should set that id? There should be a better way without you setting the ID
Once the record is persisted, the id is set by hibernate
A
Andrey
em.persist(abc);
em.refresh(abc);
return abc;

This method did not work for me. Got this error: javax.persistence.PersistenceException: org.hibernate.HibernateException: this instance does not yet exist as a row in the database]
@rtcarlson, yes this will not work. if you're creating a new object, what you need is em.flush() not em.refresh(abc).

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now