I'm persisting an Entity class to the database through the following code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(user); // user is the Entity class object
em.getTransaction().commit();
Now through JPA, how do I obtain the autogenerated keys generated by the query above?
Assuming you have mapped your @Id in your entity with @GeneratedValue(strategy=IDENTITY) and created your table with AUTO_INCREMENT on the primary key column, then the id should be assigned to your object on commit.
So you can access the id from your object after the commit call. If you need it before the commit, you can call flush() to have the id assigned.
see also, http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Identity_sequencing