有人可以为我提供有关如何使用@Embeddable的简单说明吗?
我在下面有这种情况!
一家公司,有几名员工,我的员工表有2个ID字段(register_number
和name
)
这是正确的方法吗?
@Entity
@Table(name = "COMPANY")
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private Employee employeeId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name = "ID", unique = true, nullable = false, precision = 38)
private Long id;
@Column(name = "NAME", nullable = false, length = 50)
private String name;
@ToString.Exclude
@EqualsAndHashCode.Exclude
@OneToMany(fetch = FetchType.LAZY, mappedBy = "registerNumber")
private Set<Employee> employees;
}
//
@Embeddable
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "REGISTER_NUMBER", nullable = false, length = 100)
private String registerNumber;
@Column(name = "NAME", nullable = false, length = 50)
private String name;
@Column(name = "EMAIL", nullable = false, length = 50)
private String email;
}
最佳答案
我认为您正在寻找一种使用休眠支持复合键的方法。您应该将这些字段移至新类,并使用@Embeddable
对该类进行注释。
对于问题中提到的类,请使用字段(EmployeeIdClass
和registerNumber
)创建一个新的Embeddable类name
,并在Employee
中具有该类对象的变量,并用@EmbeddedId对其进行注释。EmployeeIdClass
@Embeddable
public class EmployeeIdClass implements Serializable{
@Column(name = "REGISTER_NUMBER", nullable = false, length = 100)
private String registerNumber;
@Column(name = "NAME", nullable = false, length = 50)
private String name;
public String getRegisterNumber() {
return registerNumber;
}
public void setRegisterNumber(String registerNumber) {
this.registerNumber = registerNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EmployeeIdClass that = (EmployeeIdClass) o;
return Objects.equals(registerNumber, that.registerNumber) &&
Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(registerNumber, name);
}
}
Employee
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@Column(name = "employeeId", nullable = false, length = 50)
private EmployeeIdClass employeeId;
@Column(name = "EMAIL", nullable = false, length = 50)
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public EmployeeIdClass getEmployeeId() {
return employeeId;
}
public void setEmployeeId(EmployeeIdClass employeeId) {
this.employeeId = employeeId;
}
}
Company
@Entity
@Table(name = "COMPANY")
public class Company implements Serializable {
private static final long serialVersionUID = 1L;
// @EmbeddedId
// private Employee employeeId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
@GenericGenerator(name = "native", strategy = "native")
@Column(name = "ID", unique = true, nullable = false, precision = 38)
private Long id;
@Column(name = "NAME", nullable = false, length = 50)
private String name;
// @ToString.Exclude
// @EqualsAndHashCode.Exclude
@OneToMany(fetch = FetchType.LAZY)
private Set<Employee> employees;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
}
CompanyEmployeeTest
public class CompanyEmployeeTest {
public static void main(String[] args) {
EmployeeIdClass employeeIdClass2 = new EmployeeIdClass();
employeeIdClass2.setName("b");
employeeIdClass2.setRegisterNumber("2");
EmployeeIdClass employeeIdClass3 = new EmployeeIdClass();
employeeIdClass3.setName("c");
employeeIdClass3.setRegisterNumber("3");
Employee emp2 = new Employee();
emp2.setEmail("b@");
Employee emp3 = new Employee();
emp3.setEmail("c@");
emp2.setEmployeeId(employeeIdClass2);
emp3.setEmployeeId(employeeIdClass3);
Company company = new Company();
Set<Employee> set = new HashSet<>();
set.add(emp2);
set.add(emp3);
company.setEmployees(set);
company.setName("first-company");
company.setId(1234l);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(company);
session.save(emp3);
session.save(emp2);
transaction.commit();
session.close();
}
}
Queries ran by Hibernate
Hibernate: create table COMPANY (ID bigint not null auto_increment, NAME varchar(50) not null, primary key (ID)) engine=MyISAM
Hibernate: create table COMPANY_EMPLOYEE (Company_ID bigint not null, employees_NAME varchar(50) not null, employees_REGISTER_NUMBER varchar(100) not null, primary key (Company_ID, employees_NAME, employees_REGISTER_NUMBER)) engine=MyISAM
Hibernate: create table EMPLOYEE (NAME varchar(50) not null, REGISTER_NUMBER varchar(100) not null, EMAIL varchar(50) not null, primary key (NAME, REGISTER_NUMBER)) engine=MyISAM
Hibernate: alter table COMPANY_EMPLOYEE add constraint UK_lnmh1scqoa65gxcryjyeroyj unique (employees_NAME, employees_REGISTER_NUMBER)
Hibernate: alter table COMPANY_EMPLOYEE add constraint FKkkjhj0prbvia5yiqebnefkkb5 foreign key (employees_NAME, employees_REGISTER_NUMBER) references EMPLOYEE (NAME, REGISTER_NUMBER)
Hibernate: alter table COMPANY_EMPLOYEE add constraint FKivik2ern4s4074u9eb3c6u7jw foreign key (Company_ID) references COMPANY (ID)
Hibernate: insert into COMPANY (NAME) values (?)
Hibernate: insert into EMPLOYEE (EMAIL, NAME, REGISTER_NUMBER) values (?, ?, ?)
Hibernate: insert into EMPLOYEE (EMAIL, NAME, REGISTER_NUMBER) values (?, ?, ?)
Hibernate: insert into COMPANY_EMPLOYEE (Company_ID, employees_NAME, employees_REGISTER_NUMBER) values (?, ?, ?)
Hibernate: insert into COMPANY_EMPLOYEE (Company_ID, employees_NAME, employees_REGISTER_NUMBER) values (?, ?, ?)
关于java - 嵌入式关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59673211/