1

I have an Entity where one of attributes is insert_time. Insert time is not filled within application but via database trigger.

CREATE OR REPLACE TRIGGER ENTITY_NAME_CT
BEFORE INSERT ON ENTITY_NAME
FOR EACH ROW
BEGIN
:new.CREATE_TIME := CURRENT_TIMESTAMP;
END;

how can i fetch insert time in my application? i know can call entity repository from my service to search for entity with given id, but this will begin new transaction and basicly fetch whole entity again, which seems like a bad solution. Can the insert time be fetch within repository's create method? entity managers refresh() won't do the trick since its still same transaction

My create method looks more or less like this:

@Transactional
public Comment createEntity(Entity entity) {
    entityManager.persist(entity);
    return entity
}
Akka Jaworek
  • 1,970
  • 4
  • 21
  • 47

2 Answers2

1

You can try flushing followed by refreshing:

@Transactional
public Comment createEntity(Entity entity) {
    entityManager.persist(comment);
    entityManager.flush();
    entityManager.refresh(comment);    

    return entity
}

Just make sure the flush mode is set to FlushModeType.AUTO not COMMIT for that connection / transaction.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
0

There is a merge command that can help you achieve the result.

@Transactional
public Entity createEntity(Entity entity) {
    return entityManager.merge(entity);
}

This question can provide you more information on Merge.

Tu.Ma.
  • 1,325
  • 10
  • 27