我来自 jdbc 背景,我决定自学使用hibernate。所以我做了一个有序列的表:
CREATE TABLE TST_PERSON(
ID NUMBER,
NAME VARCHAR(30),
SURNAME VARCHAR(30)
);
CREATE SEQUENCE TST_PERSON_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999
INCREMENT BY 1 START WITH 1000 CACHE 20 NOORDER NOCYCLE ;
和我的映射java代码:
@Entity
@Table(name="TST_PERSON")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO,generator="pers_seq")
@SequenceGenerator(name="pers_seq",sequenceName="TST_PERSON_SEQ")
private Long id;
@Column(name="NAME")
private String name;
@Column(name="SURNAME")
private String surname;
... getters and setters ...
}
我的休眠配置是:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:sid</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="com.domain.Person"/>
</session-factory>
</hibernate-configuration>
但是当我在调用 session.save 时查看 Hibernate 日志生成的 SQL 时:
Hibernate: select TST_PERSON_SEQ.nextval from dual
Hibernate: insert into TST_PERSON (NAME, SURNAME, id) values (?, ?, ?)
我认为可能有一些缓存原因,但是当我尝试通过循环保存时,我得到了相同的输出。
所以问题是我如何让休眠将序列作为插入语句的一部分包含在内(这是我打算发生的)。就版本而言,我使用的是休眠 4.1.8