views:

954

answers:

3

Hello,

Having been googling for hours, I realize that users can use either xml file(orm.xml, I suppose?) or annotations in JPA, or both of them at the same time. I'm i correct? So, My project use the second-level cache, which is not in the JPA specification. And I use annotations like: @org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE )

for each entities. However, I guess that the annotation above doesn't belongs to be JPA annotation, therefore, I don't know if there is anything equivalent in orm.xml? I have had a look at the xml schema of orm.xml, and there is not such hibernate element permitted in the schema.

If I can't do the second-level cache config in the orm.xml, is there any way other than in the annotation? The reason that I don't want to do it the annotation is because I have two applications, one use READ_WRITE concurrency strategy and another use READ_ONLY concurrency strategy.

Thanks in advance!!!

Kewei

+2  A: 

you can use the hibernate.cfg.xml, hibernate.properties or *.hbm.xml files to turn on caching on specific objects with different caching strategies

Salandur
Thank you Salandur!I just had a look at the <Java Persistance with Hibernate> Book.It is said that "On the other hand, you can’t override annotations with Hibernate XML mapping files; you have to define a complete entity class mapping in XML."So I guess maybe I can't use annotation and Hibernate mapping files at the same time? right?
keweishang
In the <Java Persistance with Hibernate> Book, it is also said that:"You also need to consider vendor extensions when you make a decision for an XML metadata format. The Hibernate XML format supports all possible Hibernate mappings, so if something can’t be mapped in JPA/Hibernate annotations, it can be mapped with native Hibernate XML files. The same isn’t true with JPA XML descriptors—they only provide convenient externalized metadata that covers the specification."
keweishang
@keweishang: i don't know that, here we are stil using weblogic 8.1, so we can't use annotations. but i thing u are right and in this case use the hibernate mapping files. with spring u can define the caching outside the mapping file, may u can have a look on that, examlple: <property name="entityCacheStrategies"> <props> <prop key="HibernateObject">read-write</prop>
Salandur
Thank you very much, Salandur:)
keweishang
+1  A: 

If you don't want to use annotations, then check the documentation of your hibernate second level cache implementation (a.k.a "cache provider", for example, ehcache) how to configure it.

Note that Hibernate allows to use different cache providers, so there is no general answer to your question.

Aaron Digulla
Thank you very much! Aaron Digulla.
keweishang
A: 

So Therefore, I think the current solution for my problem is to replace all the annotations of each entities with Hibernate mapping files, so that for different deployment (application as well), we could use different Hibernate mapping files.

keweishang