3

I'm trying to implement some simple auditing functionality for my java app. Here's a snippet of my audit class:

public class Audit<C, T> {
    public final Class<C> modifiedClass;
    public final Date modificationTime;
    public final MyFieldMeta<C, T> fieldMeta; //contains Class<T>
    public final T newValue;

    //constructor, etc...
}

When it comes to persisting these audit objects, I'd prefer to have just one table storing all varieties independent of what T is. I'm using postresql, and I'm wondering what the best approach is for saving newValue to a column, then getting it back again.

newValue is limited to fairly simple types that have postgresql equivalents - String, Integer, Date etc. So storing the value as text and the type as varchar in a separate column, then mapping them back in the java wouldn't be too hard. Is there a slicker approach?

0

1 Answer 1

1

Once you 'flatten' the data types in your database, you will rely on logic to convert it back, which could cause problems. Consider the value: 11.12

Is that text 11.12, as in chapter numbers? Is that 11.12 as in a monetary value? Is that 11th December?

Unless you can guarantee that a value can always be distinctly mapped to a type, the only other way around it is to add an additional type marker to the field, which you must then parse out, for instance: s11.12 to mean "a String of value 11.12".

Sign up to request clarification or add additional context in comments.

2 Comments

as I said, I could store both the type and value, so inferring the type is not an issue. I guess my question is a) is there a more elegant way than the option I mentioned? and b) does this approach/architecture make sense in general for simple auditing functionality?
This method is simple, if not very elegant. A little nicer and more flexible would be to have a separate column for the type. For a simple audit, it should be fine. Just be aware of the limitations regarding adding support for new data types in later. If the data types won't change (much), then you shouldn't have problems with maintenance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.