简体   繁体   中英

Can I Modify Hibernate/JPA Attribute Dynamically?

I have an Entity that has a column that I need to make Transient only for a specific configuration or session factory. Basically, the column should never be sent in an update or insert statement for one particular configuration, and will be set by an interceptor when retrieving data. For any other configuration, the column should be persisted in the database. Is there any way to configure a column at runtime/dynamically, based on the session factory or configuration? I am able to make the column transient and set it correctly, but I need it persisted for some configurations.

EDIT I initially put "filter" but meant "interceptor"

在此输入图像描述

An annotation is a form of syntactic metadata that can be added to Java source code so you cannot change in it at run-time.

However, if its necessary, then java annotations, can be accessible to the programmer at the runtime through reflection api. An example on how to add annotations using Javassist is here .

My recommendation, as Dragan suggest, is that you can set it to null wherever its not required.

You can create two entities, as mapping of one table. In first one you column would be transient in another will not. I've already had some trouble like this, and i think that it is not possible to change mapping config after you have opened the session.

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