英文:
Is there a way to add information on fields of a class?
问题
我有这个模型,里面有不同类型的变量(String、Date、Integer),其中一些是其他嵌套对象。我需要找出两个该模型实例之间的差异,并进行标记(标记类型很重要,但标记有三种:
- 变量值已更改
- 变量之前的值为null,现在已定义
- 值已被删除
问题在于我无法更改所使用的模型,因为它非常复杂,并且与JPA(Java持久化API)相关联(我正在考虑在某种可标记对象中更改任何变量的类型)。
也不可能仅生成修改后的集合、添加的值集合和删除的值集合,输出必须保留对象结构,因为之后它应该打印在一个.xlsx文件中,并对数据进行一些自定义处理。
所以,我的问题是:“有没有一种方法可以向类的字段添加信息?”或者可能有一种不同的方法来处理这个差异标记问题?
英文:
I have this model that has different variable inside(String, Date, Integer), some of them are other nested object.
I need to find the differences between two instance of this model and mark them(the type of marking is important, but the marks are three:
- Value of variable changed
- Value of variable before was null and now is defined
- Value has been erased
The problem is that I cannot change the model used, because is very complex, and is attached to JPA( I was thinking of change the type of any variable in some kind of markable object).
Neither is possible to produce only set of Modified, Added, Removed value, the output must preserve the object structure, because after that it should be printed in an .xlsx file, with some custom manipulation on the data.
So, my question is:"Is there a way to add information on fields of a class?" or maybe a different approach to handle this diff-marking problem?
答案1
得分: 1
是否有方法在类的字段上添加信息?
没有,没有这种方法。你想要做的通常称为日志记录。大多数数据库,例如,会创建一个记录所有数据库更改的日志。
我们来看一个简单的例子。假设我们有一个 Person
类。
Person
------
人员 ID
人员姓名
人员地址
我们想要监视姓名或地址的更改。
我们使用另一个类来实现。我们称其为 PersonChanges
。
PersonChanges
-------------
变更 ID
人员 ID
变更字段(姓名或地址)
变更前内容
变更后内容
变更时间戳
执行变更的人员
对于插入操作,变更前字段为空或为 null。对于删除操作,变更后字段为空或为 null。时间戳允许您查看更改发生的时间。通常还会记录执行更改的人员,以便您可以查明是谁进行了未经授权的更改。
您可以通过在现有系统之上创建日志记录系统来监视更改。您需要找出应用程序中每个模型更改的实例,并添加代码来记录更改。
英文:
> Is there a way to add information on (the) fields of a class?
No, there isn't. What you want to do is generally called logging. Most databases, as an example, create a log of all the database changes.
Let's take a simple example. Let's say we have a Person
class.
Person
------
Person ID
Person Name
Person Address
And we want to monitor changes to the name or address.
We do this with an additional class. Let's call it PersonChanges
.
PersonChanges
-------------
PersonChanges ID
Person ID
PersonChanges Field (Name or Address)
PersonChanges Before
PersonChanges After
PersonChanges Timestamp
PersonChanges Who Made The Change
For an insert, the before field is blank or null. For a delete, the after field is blank or null. The timestamp allows you to see when the change was made. The person that made the change is also usually recorded, so you can see who made an unauthorized change.
You monitor changes by creating a logging system on top of the existing system. You have to find every instance of a model change in your application and add code to log the change.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论