简体   繁体   English

我可以动态修改Hibernate / JPA属性吗?

[英]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. 我有一个实体,我需要为特定配置或会话工厂制作Transient。 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. 基本上,该列永远不应该在一个特定配置的update或insert语句中发送,并且在检索数据时将由拦截器设置。 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. 注释是一种语法元数据形式,可以添加到Java源代码中,因此您无法在运行时更改它。

However, if its necessary, then java annotations, can be accessible to the programmer at the runtime through reflection api. 但是,如果必要的话,那么java注释可以在运行时通过反射api访问程序员。 An example on how to add annotations using Javassist is here . 如何添加使用Javassist进行注释的一个例子是在这里

My recommendation, as Dragan suggest, is that you can set it to null wherever its not required. 正如Dragan建议的那样,我的建议是,您可以在不需要的地方将其设置为null。

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. 我已经遇到过这样的麻烦,我认为打开会话后无法更改映射配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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