【问题标题】:How to have a log or history table for User in MySQL and Hibernate如何在 MySQL 和 Hibernate 中为用户创建日志或历史记录表
【发布时间】:2017-01-06 13:08:18
【问题描述】:

我在为我的应用程序创建数据库设计时感到困惑。我正在使用 MySql 作为数据库以及 spring 和 hibernate。

我的要求是: 我有一个角色为 admin/user 的用户表,我有一个设备表。在设备表中,我有一个名为createdBy 的列,它是添加设备的userId。 用户可以更新设备状态,因此每次更改设备值或状态时,我都需要记录这一点。我正在使用一个名为DeviceHistory 的单独表来记录此内容。

我被困在hibernate中实现这个。

  • 我应该在 device 表和 device_history 表中使用 userId 作为 int 字段吗?
  • 我应该在 Device 表和 deviceHistory 表中使用OneToOne 映射 o userId
  • 我应该对设备和设备历史表使用OneToMany 映射以及从设备到用户表的OneToOne 映射。 如果有此类场景的示例代码,请分享链接

用户表 - 包含用户详细信息

@Entity
@Table(name = "user")
public class User {

    @Id 
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="id", unique = true, nullable = false)
    @Column(name="user_id")
    private int userId;

    @Column(name="name")
    @Nonnull
    private String name;

    @Column(name="password")
    @Nonnull
    private String password;

    @Column(name="role")
    private String role;

    @Column(name="creation_date", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP", insertable=false, updatable=false)
    @Generated(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Nonnull
    private Date creationDate;

    @Column(name="approved_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date approvedDate;

    @Column(name="approved_by")
    private int approvedBy;

}

设备表:包含设备详细信息

@Entity
@Table(name = "device")
public class Device {

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id", unique = true, nullable = false)
    @Column(name="device_id")
    private String deviceId;

    @Column(unique=true, name="serial_number")
    @Nonnull
    private String serialNumber;

    @Column(name="creation_date")
    @Generated(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Nonnull
    private Date creationDate;

    @Column(name="created_by")
    private int createdBy;

}

DeviceHistory 表:这里是用户更新设备状态以及何时保存的历史记录

@Entity
@Table(name = "device_history")
public class DeviceHistory {

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id", unique = true, nullable = false)
    private int id;

    @Column(name="device_id")
    @Nonnull
    private String deviceId;

    @Column(name="notes")
    private String notes;

    @Column(name="last_modified_date")
    @Generated(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Nonnull
    private Date lastModifiedDate;

    @Column(name="last_modified_by")
    private int lastModifiedBy;
}

【问题讨论】:

  • 请仅将代码块用于格式化代码,而不用于您的问题文本。

标签: mysql hibernate hibernate-mapping hibernate-criteria hibernate-onetomany


【解决方案1】:

您应该在 Device for User 中使用 ManyToOne 映射。同样,在 DeviceHistory 中,您应该为 DeviceId 和 lastModifiedBy 使用 ManyToOne 映射。

您的 Device 和 DeviceHistory 实体应该是:

@Entity
@Table(name = "device")
public class Device {

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="device_id", unique = true, nullable = false)
    private String deviceId;

    @Column(unique=true, name="serial_number")
    @Nonnull
    private String serialNumber;

    @Column(name="creation_date")
    @Generated(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Nonnull
    private Date creationDate;

    @ManyToOne(optional = false)
    @JoinColumn(name="created_by")
    private User createdBy;

}

@Entity
@Table(name = "device_history")
public class DeviceHistory {

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id", unique = true, nullable = false)
    private int id;

    @ManyToOne(optional = false)
    @JoinColumn(name="device_id")
    private Device deviceId;

    @Column(name="notes")
    private String notes;

    @Column(name="last_modified_date")
    @Generated(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Nonnull
    private Date lastModifiedDate;

    @ManyToOne(optional = false)
    @JoinColumn(name="last_modified_by")
    private User lastModifiedBy;
}

如果要获取用户创建的所有设备,可以考虑用户和设备的双向 OneToMany 映射。为此,您必须对用户实体中的设备使用 OneToMany 注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-23
    • 1970-01-01
    • 2022-12-12
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多