需要知道每个字段是否发生了变化,我应该如何在Hibernate中对此进行建模 [英] Need to know if each field has changed, how should I model this in Hibernate

查看:76
本文介绍了需要知道每个字段是否发生了变化,我应该如何在Hibernate中对此进行建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个有三个字段的类,它们使用hibernate映射到一个表

 类小组件
{
字符串field1;
字符串field2;
字符串field3;





$ b在应用程序启动时,这些小部件将被添加到数据库中一个外部文件,但是当我退出应用程序时,我需要知道自应用程序启动后用户已更改这些字段中的哪些(如果有),因此可以将更改保存回文件。我还需要存储原始值以进行日志记录。



无论我需要表中的状态字段还是已有方法,我都无法工作这使用Hibernate /数据库。



编辑:一个很好的解决方案如下。然而我使用Hibernate的主要原因是为了减少内存消耗,所以当更改时存储原始值对我来说不是一个好的解决方案,我想要存储在数据库中的everthing。所以我创建了这个新问题

一个像下面这样的实体,你可以跟踪其中一个域的变化(同时保留它的原始值)。

$ $ p $ @Entity
@Table(schema =test,name =test)
public final class Test {

private static final int ORIGINAL = 0;
private static final int CURRENT = 1;

私人整数ID;

//保存字段的原始和当前状态
private final AtomicReferenceArray< String> field = new AtomicReferenceArray<>(2);

@Id
public Integer getId(){
return id;
}

public void setId(Integer id){
this.id = id;
}

@Transient
public String getOriginalField(){
return field.get(ORIGINAL);
}

@Basic
public String getField(){
return field.get(CURRENT);
}

public void setField(String field){
this.field.compareAndSet(ORIGINAL,null,field);
this.field.set(CURRENT,field);
}

@PreUpdate
public void preUpdate(){
System.out.format(Original:%s,New:%s\\\
, getOriginalField(),getField());
}

...

}



如果数据库中有一行是这样的:

  id:1 
字段:a
版本:2011-12-02 11:24:00



<在字段被更新之前(比如说,从 a b ),我们会得到以下结果。

 原文:d,新:b 

即使实体更新多次,原始值也会保留,并且可以通过相应的getter( getField getOriginalField - 您可以在命名中获得比我更有创意的:)



通过这种方式,您可以省去在您的创建版本列数据库,也可以隐藏来自客户端的实现细节。



您可以使用数组,列表等等来代替 AtomicReferenceArray 跟踪所有这样的变化。



@PreUpdate 当然不是必需的,但这样您可以通知实体状态的变化并自动保存将更新的字段放入文件中。还有更多注释如下:请参阅 javax.persistence 用于其他注释类型。


So I have a class with three fields that maps to a table using hibernate

Class Widget
{
     String field1;
     String field2;
     String field3;
}

On application startup a number of instances these widgets will be added to the database from an external files, but when I exit the application I need to know which (if any) of these fields have been changed by the user since the application was started, so the changes can be saved back to the files. I also need to store the original value for logging purposes.

I can't work whether I need a status field in the table or whether there is already a way of doing this using Hibernate/Database.

EDIT:A good solution to the program was given below . however the main reason I am using Hibernate is to reduce memory consumption so storing the original values when changed is not a good solution for me , I want everthing stored in the database. So I have create this new question How do I store a copy of each entity I add to database in Hibernate

解决方案

Given an entity like the following you can track changes on one of it's field (while preserving its original value too).

@Entity
@Table(schema = "test", name = "test")
public final class Test {

  private static final int ORIGINAL = 0;
  private static final int CURRENT = 1;

  private Integer id;

  // holds the original and current state of the field
  private final AtomicReferenceArray<String> field = new AtomicReferenceArray<>(2);

  @Id
  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  @Transient
  public String getOriginalField() {
    return field.get(ORIGINAL);
  }

  @Basic
  public String getField() {
    return field.get(CURRENT);
  }

  public void setField(String field) {
    this.field.compareAndSet(ORIGINAL, null, field);
    this.field.set(CURRENT, field);
  }

  @PreUpdate
  public void preUpdate() {
    System.out.format("Original: %s, New: %s\n", getOriginalField(), getField());
  }

  ...

}

If there is a single row in a database like this:

     id: 1
  field: a
version: 2011-12-02 11:24:00

before the field gets updated (say, from a to b) you'll get the following output.

Original: d, New: b

The original value gets preserved even if the the entity is updated multiple times and both state can be accessed through the corresponding getters (getField and getOriginalField—you can get more creative than me in the naming :).

This way, you can spare yourself from creating version columns in your database and also can hide the implementation details from clients.

Instead of an AtomicReferenceArray you could use arrays, lists, etc, to track all changes like this way.

The @PreUpdate isn't necessary of course, but this way you can be notified of changes in the entity's state and atomically save the updated fields into file. There more annotations like these: see the documentation for javax.persistence for other annotation types.

这篇关于需要知道每个字段是否发生了变化,我应该如何在Hibernate中对此进行建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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