这是我的员工类代码
@Entity
@Table(name = "ACCOUNT")
public class Account {
@Id
@SequenceGenerator(name = "account_seq", sequenceName = "seq_account")
@GeneratedValue(generator = "account_seq")
@Column(name = "ACCOUNT_ID")
private Integer accountId;
@Column(name = "ACCOUNT_NUMBER")
private Integer accountNumber;
@OneToOne
private Employee employee;
//getters and setters
}
帐户类
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@SequenceGenerator(name = "emp_seq", sequenceName = "seq_employee")
@GeneratedValue(generator = "emp_seq")
@Column(name = "EMPLOYEE_ID")
private Integer employeeId;
@Column(name = "EMPLOYEE_NAME")
private String employeeName;
//getters and setters
}
这是测试类
Account a = new Account();
a.setAccountNumber(12345);
Employee e = new Employee();
e.setEmployeeName("emp-1");
a.setEmployee(e);
em.persist(e);
em.persist(a);
以下是为上述测试生成的输出sql
Hibernate: drop table ACCOUNT cascade constraints
Hibernate: drop table EMPLOYEE cascade constraints
Hibernate: drop sequence seq_account
Hibernate: drop sequence seq_employee
Hibernate: create sequence seq_account start with 1 increment by 50
Hibernate: create sequence seq_employee start with 1 increment by 50
Hibernate: create table ACCOUNT (ACCOUNT_ID number(10,0) not null, ACCOUNT_NUMBER number(10,0), employee_EMPLOYEE_ID number(10,0), primary key (ACCOUNT_ID))
Hibernate: create table EMPLOYEE (EMPLOYEE_ID number(10,0) not null, EMPLOYEE_NAME varchar2(255 char), primary key (EMPLOYEE_ID))
Hibernate: alter table ACCOUNT add constraint FKg2gm43aorntmns07tkd6h9osd foreign key (employee_EMPLOYEE_ID) references EMPLOYEE
Sep 07, 2015 5:14:29 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select seq_employee.nextval from dual
Hibernate: select seq_employee.nextval from dual
Hibernate: select seq_account.nextval from dual
Hibernate: select seq_account.nextval from dual
Hibernate: insert into EMPLOYEE (EMPLOYEE_NAME, EMPLOYEE_ID) values (?, ?)
Hibernate: insert into ACCOUNT (ACCOUNT_NUMBER, employee_EMPLOYEE_ID, ACCOUNT_ID) values (?, ?, ?)
正如您所看到的那样,对于{hidnate调用seq_emplyee.nextval
的序列生成的部分也是seq_account
两次。我很困惑,为什么hibernate会调用两次这个?