如何在 MySQL 和 Hibernate 中为用户创建日志或历史记录 table
How to have a log or history table for User in MySQL and Hibernate
我在为我的应用程序创建数据库设计时感到困惑。我使用 MySql 作为数据库,spring 和休眠。
我的要求是:
我有一个用户 table,角色 admin/user,我有一个设备 table。在设备 table 中,我有一个名为 createdBy
的列,它是添加设备的 userId
。
用户可以更新设备状态,因此每次更改设备值或状态时,我都需要记录它。我正在使用一个名为 DeviceHistory
的单独 table 来记录它。
我坚持在休眠中实现这个。
- 我应该在设备 table 和 device_history table 中使用 userId 作为 int 字段吗?
- 我应该在 Device table 和 deviceHistory table
中使用 OneToOne
映射 o userId
- 我应该使用
OneToMany
映射设备和设备历史记录 table 和 OneToOne
从设备到用户的映射 table。
如果有此类场景的示例代码,请分享链接
用户 table - 包含用户详细信息
@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;
}
设备table:包含设备详细信息
@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 table: 这里保存用户更新设备状态和时间的历史
@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;
}
您应该在用户设备中使用 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;
}
如果想获取一个用户创建的所有设备,可以考虑User和Device的双向OneToMany映射。为此,您必须对用户实体中的设备使用 OneToMany 注释。
我在为我的应用程序创建数据库设计时感到困惑。我使用 MySql 作为数据库,spring 和休眠。
我的要求是:
我有一个用户 table,角色 admin/user,我有一个设备 table。在设备 table 中,我有一个名为 createdBy
的列,它是添加设备的 userId
。
用户可以更新设备状态,因此每次更改设备值或状态时,我都需要记录它。我正在使用一个名为 DeviceHistory
的单独 table 来记录它。
我坚持在休眠中实现这个。
- 我应该在设备 table 和 device_history table 中使用 userId 作为 int 字段吗?
- 我应该在 Device table 和 deviceHistory table 中使用
- 我应该使用
OneToMany
映射设备和设备历史记录 table 和OneToOne
从设备到用户的映射 table。 如果有此类场景的示例代码,请分享链接
OneToOne
映射 o userId
用户 table - 包含用户详细信息
@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;
}
设备table:包含设备详细信息
@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 table: 这里保存用户更新设备状态和时间的历史
@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;
}
您应该在用户设备中使用 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;
}
如果想获取一个用户创建的所有设备,可以考虑User和Device的双向OneToMany映射。为此,您必须对用户实体中的设备使用 OneToMany 注释。