我想使用hibernate将一个简单的excel表映射到我的javaEE应用程序。我是数据库和ORM的新手,所以我想知道以下关系是否有意义以及有多少实体分割表是有意义的。
这是Excel电子表格中包含的属性:
(Office Room Number|ComputerName|ComputerIP|Computer OS|UserFirstName|UserLastName)
关系:
OfficeRoomNumber -- 1 : N -- Users
OfficeRoomNumber -- 1 : N -- Computer
User -- 1:1 -- Computer
感谢您的帮助,并为我可怕的英语感到抱歉。
答案 0 :(得分:2)
以下是我的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-provider与您的应用程序保持中立。
希望这有帮助。