0

I have a class 'Audit' that will hold the following details about an entity:

  • Time Created
  • Created by which application user
  • Time Updated
  • Updated by which application user

On the database, these fields are stored on the same table as the entity attributes. For example,

USER table:

CREATE TABLE USERS (
    id                  BIGINT          NOT NULL    AUTO_INCREMENT,
    display_name        VARCHAR(30)     NOT NULL,

    active              BOOLEAN         NOT NULL    DEFAULT FALSE,

    created_time        TIMESTAMP       NOT NULL    DEFAULT CURRENT_TIMESTAMP,
    created_user        BIGINT          NOT NULL    DEFAULT 0,
    updated_time        TIMESTAMP       NOT NULL    DEFAULT 0,
    updated_user        BIGINT          NOT NULL    DEFAULT 0,

    PRIMARY KEY (id)
);

USER Class:

@Entity(name="USER")
@Table(name="USERS")
public class User implements Audited {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ID", nullable=false, updatable=false)
    private long id;

    @Column(name="DISPLAY_NAME", nullable=false)
    @NotNull
    @Size(min=5, max=30)
    private String displayName;

    @Column(name="ACTIVE", nullable=false)
    @NotNull
    private boolean active;

    private Audit audit;
}

AUDIT Class:

public class Audit {

    private Date createdTime;

    private User createdByUser;

    private Date updatedTime;

    private User updatedByUser;
}

On each entity table, the audit fields will be named the same. What is the best approach in mapping these fields?

1 Answer 1

1

The best way would be to make the Audit class as @Embeddable entity and use it in your all entities as an @Embedded property, which is almost what your are doing currently.

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

Comments

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.