0

after calling the persist method of the entity manager to persist my entity, I can't seem to retrieve the data that is generated by the database such as create_date and some autonumbers.

the merge method does throw back the result object after calling the EntityManager.merge() but it still don't have the additional data generated by the database.

the merge method should do the synchronization right? But it seems not to do it

any suggestions would be appreciated

chip
  • 3,039
  • 5
  • 35
  • 59

2 Answers2

1

It's probably not flushing to the db at that point. Our system (using hibernate) only flushes at the end of the transaction. You can probably google around this now...! HTH

davidfrancis
  • 3,734
  • 2
  • 24
  • 21
  • hi, I think I am only using jta. tried to flush it just like what you said but I still get the same message – chip Dec 15 '11 at 11:19
  • 1
    Are you using Hibernate? What message are you talking about? I don't think you'll get any joy here unless you commit the values to the database at this point. Is that what you're trying to do? This doesn't appear to be standard practice with JPA/Hibernate - google around it e.g. http://stackoverflow.com/questions/197045/setting-default-values-for-columns-in-jpa – davidfrancis Dec 15 '11 at 15:33
  • PS When I say "commit the values to the database at this point" you'd have to commit the transaction and start another one at that point - not a nice idea – davidfrancis Dec 15 '11 at 15:33
  • hi @davidfrancis, I'm sorry I am no that much familiar with this. I I seem to persist the data of the entity just fine, `java.lang.IllegalArgumentException: Can not refresh not managed object:` Is the message that it gives when I try to refresh the entity with the values on the data store. I also do think that I am not doing any stuff regarding about the transaction because I think the container is already working that out for me. – chip Dec 16 '11 at 00:23
  • Hi, I think you've got distracted by the refresh thing. I would forget about this for, as there is no db state to refresh. Have a read of these two links: http://docs.oracle.com/javaee/5/tutorial/doc/bnbqw.html -> Read section: Persisting Entity Instances. This tells you this: "This means the entity’s data is stored to the database when the transaction associated with the persist operation is completed". Also, it would be good if you could modify your question and post some code. Have a look at the code examples in that link. – davidfrancis Dec 16 '11 at 11:36
  • thanks for the suggestions. I was able to take care of it. We just took the autogenerated key manually from a database procedure and assigned it manaully to our entity before persisting so we don't have to depend on jpa to retrieve back the entity after persisting. thanks anyway – chip Dec 17 '11 at 08:58
1

JPA providers aren't requeired to fetch actual state of the entity from the database after persisting. If you need to do it, call flush() and refresh() explicitly.

If your JPA provider is Hibernate, you can use @Generated annotation to mark fields that should be refereshed automatically.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • hi, I tried to do the refresh thing and it gave me a lovely message. it said it cannot refresh because the entity that I am trying to refresh is not managed, but I just persisted it. reading the definition from oracle's entity manager it says that 'Make an instance managed and persistent.' that is for the persist method. weird – chip Dec 15 '11 at 11:04
  • 1
    @simon: Try to `flush()` before `refresh()`. – axtavt Dec 15 '11 at 11:10
  • hi, thanks for responding. yep I did what you suggested, same message, it says it can't refresh an unmanaged object. – chip Dec 15 '11 at 11:18