2

Take my domain class for example

public class Person
{
  private Integer id;
  private String name;
  private String address;
  private String telephone;

  //Accessors here..
}

This is great for storing 1 instance of a given Person, however, the name for example would most likely change over time, and I would like to retain any previous values, I may wish to see what addresses this person has lived at over the past 10 years.

What are my options for doing this? This is a Java web app so I could potentially have an AUDIT_LOG table on my schema, but that doesn't sound a very reliable way of keeping track of these changes

Another thought is to have a PersonFamily and keep all instances of person, assuming the last item in the List is the most recent, such as..

 public class PersonFamily
 {
   private Integer id;
   private List<Person> persons;

   //Accessors here...
 }

Any suggestions on how I can achieve this? Is there a really clean and simple process I've missed?

Thanks


Google for temporal patterns or event sourcing.

http://martinfowler.com/eaaDev/timeNarrative.html

4

5 に答える 5

1
public class Person
{
  private Integer id;
  private String name;
  private Address address;

 }


public class Address {
  Integer id;
  String line1;
  String line2.
}

Then model this in your database either as :

Person Table

  • id
  • name

Address Table

  • id
  • line1
  • line2

PersonAddressHistory Table with fields:

  • id
  • personId
  • addressId
  • activeDate

the last table maintains a list of the current and previous addresses associated with a person, with the current Address being the last entry (i.e. with max value of id).

于 2009-12-03T19:31:55.970 に答える
1

An option you might consider would be a simple version number tied to class:

public class Person 
{
  private Integer id;
  private Integer version;

  private String name;
  private String address;
  private String telephone;

  //additional class stuff...

}

Using this approach, whenever the name, address, or other attributes are updated, the version number is incremented by one. This preserves the original data under the previous version, and allows for easy retrieval of all previous version by id, which does not change.

于 2009-12-03T19:48:20.577 に答える
0

履歴データの使用要件によると思います。

たとえば、あなたの個人の例では、データベースに「現在/過去」フィールドがあります。ほとんどの場合、「現在」のレコードを取得するだけで、現在と同じように機能します。DAOまたはオブジェクトモデルに変更はありません(開始/終了タイムスタンプを追加する場合を除く)。

履歴が必要な場合、それは非常に特殊な理由である可能性が高く、その時点で、変更日順に並べられた別のリストを取得できます。これは、一般的に使用されるデータモデルとは別のものです。

誰かが名前を変更したら、その人のクローンを作成し、新しいレコードを「前へ」に変更してから、既存のレコードの名前を変更してから、同じトランザクションで両方の変更をコミットします。

履歴データが非常に一般的に使用されている場合、上記は関係がないため、最初のエントリが最新のリストが必要であることに注意してください。これは、金融アプリケーションで非常に一般的に使用されるアドレス履歴の場合に当てはまる可能性があります。名前の変更に加えて、最も一般的な変更は結婚/離婚であるため、データモデルに追加のフィールド(旧姓)を含める必要があります。

于 2009-12-03T19:36:32.730 に答える
0

There are lots of ways to do it, The simplest would be to have a "Person" which has a list of "PersonHistory" objects. Whenever you call a setter, create a new PersonHistory object with the same values as the most recent. Then alter the changed field, and save that in your list.

The problem with that is you would have a lot of redundant data, one for each change. If this is going to be backed by A database, though rather then storing a huge list, you could dump each old instance to the DB whenever a change is made (so you only store the most recent one in memory)

Otherwise, you could have one list for each variable, and store a Tuple with value and the 'step'. So each time a variable is changed, you update a counter, and store that counter with the value that changed.

于 2009-12-03T19:31:20.853 に答える
0

一時的なパターンまたはイベント ソーシングについては Google。

http://martinfowler.com/eaaDev/timeNarrative.html

于 2009-12-03T19:29:44.853 に答える