Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate调用sequence.nextVal两次_Java_Hibernate - Fatal编程技术网

Java Hibernate调用sequence.nextVal两次

Java Hibernate调用sequence.nextVal两次,java,hibernate,Java,Hibernate,这是我的employee类代码 @Entity @Table(name = "ACCOUNT") public class Account { @Id @SequenceGenerator(name = "account_seq", sequenceName = "seq_account") @GeneratedValue(generator = "account_seq") @Column(name = "ACCOUNT_ID") private Int

这是我的employee类代码

@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 (?, ?, ?)

正如您所看到的,ids hibernate调用的序列生成部分
seq_emplyee.nextval
两次也用于
seq_account
。我不明白为什么hibernate会调用这两次?

这只是一个猜测,但hibernate的最新版本默认使用hi/lo生成器,而不是普通序列生成器。也许生成器通过两个调用来初始化自己,以便再次检查增量或其他什么?如果是这样,您应该能够在下次使用序列之前插入49个额外的帐户。如果您喜欢使用普通序列,请将allocationSize=1添加到SequenceGenerator注释中。@user261203您说得对,分配大小为1时,它只调用一次。