简体   繁体   中英

How to initialize @ReadOnly entities in JPA / EclipseLink

I am using JPA2 and EclipseLink. I would like to use an entity with @ReadOnly annotation. So under normal conditions these entities are not supposed to be changed und unique as Java objects over the complete persistence unit.

But how can I initialize them? They need to arrive in the database somehow. Can I bootstrap them under specific circumstances - eg when I know that there are no other transactions and caches in use?

It's true that @ReadOnly JPA entities shouldn't be written, so you can't use the PersistenceManager (via the persist() method) to initialize them. You can however create a utility class that runs when your application starts and contains a statically created set of these entities as well as a bootstrap method that uses native queries to insert them into your database.

Obviously how you cause this class to execute will depend upon your application, but I'd also recommend making the class smart enough to only add the entities that aren't already in the database. This way your class will be useful as your application is maintained.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM