我可以动态修改Hibernate/JPA属性吗? [英] Can I Modify Hibernate/JPA Attribute Dynamically?

查看:168
本文介绍了我可以动态修改Hibernate/JPA属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体,该实体的一列仅需要针对特定​​配置或会话工厂进行瞬态处理.基本上,对于某个特定配置,该列永远不应该在更新或插入语句中发送,并且在检索数据时将由拦截器设置该列.对于任何其他配置,该列应保留在数据库中.是否有任何方法可以根据会话工厂或配置在运行时/动态配置列?我可以使该列成为瞬态并正确设置它,但对于某些配置,我需要将其保留下来.

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 我最初放置过滤器",但意思是拦截器"

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

推荐答案

注释是一种语法元数据,可以添加到Java源代码中,因此您无法在运行时对其进行更改.

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.

但是,程序员在运行时可以通过反射api访问java批注(如果有必要).关于如何使用Javassist添加注释的示例为

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.

正如Dragan所建议的那样,我的建议是可以在不需要的地方将其设置为null.

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

这篇关于我可以动态修改Hibernate/JPA属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆