我想使用 hibernate 将一个简单的 Excel 表映射到我的 javaEE 应用程序。我对数据库和 ORM 很陌生,所以我想知道以下关系是否有意义,以及有多少实体可以拆分表。
这是 Excel 电子表格中包含的属性:
(办公室房间号|计算机名称|计算机IP|计算机操作系统|用户名|用户姓)
关系:
办公室编号 -- 1 : N -- 用户
- N 位用户在 1 个办公室工作?
办公室编号 -- 1 : N -- 计算机
- N 台计算机位于 1 个办公室?
用户 -- 1:1 -- 计算机
- 1 位用户拥有 1 台计算机?
感谢您的帮助,并对我糟糕的英语表示歉意。
最佳答案
这是我为您的域建模的 50c。首先,可以将抽象基类用于通用方面,例如主键生成:
@MappedSuperClass
public abstract class AbstractEntity {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pk-sequence")
@SequenceGenerator(name = "pk-sequence", sequenceName = "ID_GEN", allocationSize = 1)
protected Long objectID = -1;
@Version
private int version;
public int getVersion() {
return version;
}
public long getObjectID() {
return objectID;
}
}
请注意,这可以增强以包括其他通用方面,例如创建/修改日期/时间戳。
接下来,我们介绍三个领域类/实体,如下:
@Entity
public class OfficeRoom extends AbstractEntity {
private String name;
private String roomNumer;
@ManyToMany // Maybe an employee is associated with 2 or more office places she/he might work at?
private Collection<Employee> staff;
@OneToMany(mappedBy="location")
private Collection<Computer> equipment;
// getters and setters
}
我添加了一条评论,正如您在上面看到的关于现场工作人员的评论。人们可能希望将两个不同的办公室与某些 VIP 员工相关联,因此您应该在此处使用 @ManyToMany
为您的域建模时考虑这种情况。
继续:
@Entity
public class Computer extends AbstractEntity {
private String name;
private String model;
private String vendor;
private String installedOS;
private String ipAddress;
@ManyToOne
private OfficeRoom location;
@OneToMany(mappedBy="machine") // Maybe a computer is associated with 2 or more employees?
private Collection<Employee> user;
// getters and setters
}
再次,请仔细考虑我的评论。最后,...
@Entity
public class Employee extends AbstractEntity {
private String firstName;
private String lastName;
// other staff related attributes here ...
@ManyToOne
private Computer machine;
// getters and setters
}
请注意:在导入语句中仅使用源自 javax.persistence
包的注释,以保持符合 JPA 2.x 规范,并在您的应用程序中保持 JPA 提供者中立。
希望这有帮助。
关于mysql - 使用 JPA Hibernate 将 EXCEL 工作表映射到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49797895/