I add entity to my database and it works fine. But when i retrieve the List, i get the old entity, the new entities i add are not shown until i undeploy the application and redeploy it again. This means are my entities cached by default? But, I haven't made any settings for caching entities in my persistence.xml or any such file.
I have even tried calling flush(), refresh() and merge(). But still it shows the old entities only. Am i missing something? Please help me.
JPA has 2 levels of caching. The first level of caching is the persistence context. The JPA Entity Manager maintains a set of Managed Entities in the Persistence Context.
As illustrated in the figure 2, JPA entities are cached at the persistence context level and guarantees that there will be one object instance per persistence context for a specific row of a database table. Concurrent transactions affecting the same row are managed by applying an appropriate locking mechanism in JPA.
Using JPA, you can designate any POJO class as a JPA entity–a Java object whose nontransient fields should be persisted to a relational database using the services of an entity manager obtained from a JPA persistence provider (either within a Java EE EJB container or outside of an EJB container in a Java SE application ...
Entity cache is provided by EclipseLink ORM framework. It stores recently read or written entity instance in memory, which minimizes database access and improves the application performance. Entity cache is used only when you retrieve entities by ID, so queries by other attributes still run on the database.
Welcome to JPA. If you use it, it means you will have huge problems if you update the database outside of JPA unless you know what you're doing and are very careful. This means you have to figure out how to flush any cached entities so they can be reloaded.
Basically, don't update entities outside JPA if you can at all help it and if you do you will probably have to get into the workings of the caching model used by your particular JPA provider. If you need to update outside JPA a lot then JPA probably isn't the right choice for you.
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):
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With