This means are my entities cached by default?
JPA 1.0 does not define a L2 cache ("shared cache"), JPA 1.0 only defines an L1 cache ("transactional cache") but JPA providers can support a shared object cache, and most do. This is the case of TopLink Essentials which supports L1 and L2 cache through JPA Extensions for Caching (per JVM).
Now, as explained in the great article Understanding the cache of TopLink Essentials(GlassFish JPA):
- All EntityManagers from same persistence unit shares the session cache (that's how TopLink calls the 2nd-level cache).
- Session cache is turned on by default.
- If there is modifications/deletions of entities in the persistence context they are synchronized to session cache after a transaction committed, so the state of session cache is updated (or such a cache wouldn't be usable at all).
So there must be something else wrong with your setup. You can try do disable the shared session cache for testing purpose (and only for testing purpose) by adding the following property:
<property name="toplink.cache.shared.default" value="false"/>
But I would be surprised if this changes anything. As I said, I think there is another problem somewhere.
PS: This doesn't answer the question but, if you are using GlassFish v3, why don't you use EclipseLink?
Update: answering a comment of the OP
So if i persist employee record, then it is seen in database but not in collection of employees in department until i explicitly add it to the collection of employees. Is this necessary step?
Well, if you don't create the link between entities at the Java level, JPA won't be able to create it at the database (JPA only does what you tell him to do). So, yes, you need to create the link and, in case of a bidirectional association, you even need to set both sides of the link (for example add the employee
to the collection of employees on the Department
and set the department
of an Employee
).