下面是我的oracle自动增长,为什么id会相差2,例如插入第一个id为2,第二个id就为4了,谢谢
create sequence SEQ_CCAR_ID minvalue 1 maxvalue 999999999999999999 start with 1 increment by 1 cache 20;
//建表
create table factory_carSave//触发器
CREATE OR REPLACE TRIGGER carS
BEFORE INSERT ON factory_carSave FOR EACH ROW
BEGIN
SELECT SEQ_CCAR_ID.NEXTVAL INTO :NEW.id FROM DUAL;
END;
///hbm.xml
<class name="CarSave" table="factory_carSave">
<id column="ID" name="id" type="integer" >
<generator class="sequence">
<param name="sequence">SEQ_CCAR_ID</param>
</generator>
</id
create sequence SEQ_CCAR_ID minvalue 1 maxvalue 999999999999999999 start with 1 increment by 1 cache 20;
//建表
create table factory_carSave//触发器
CREATE OR REPLACE TRIGGER carS
BEFORE INSERT ON factory_carSave FOR EACH ROW
BEGIN
SELECT SEQ_CCAR_ID.NEXTVAL INTO :NEW.id FROM DUAL;
END;
///hbm.xml
<class name="CarSave" table="factory_carSave">
<id column="ID" name="id" type="integer" >
<generator class="sequence">
<param name="sequence">SEQ_CCAR_ID</param>
</generator>
</id
祝好运
就没有问题了
CREATE OR REPLACE TRIGGER carS
BEFORE INSERT ON factory_carSave FOR EACH ROW
BEGIN
SELECT SEQ_CCAR_ID.NEXTVAL INTO :NEW.id FROM DUAL;
END;
///hbm.xml
<class name="CarSave" table="factory_carSave">
<id column="ID" name="id" type="integer" >
<generator class="sequence">
<param name="sequence">SEQ_CCAR_ID</param>
</generator>其实上,你同时使用了这两种方法取ID,在hibernate中定义了<param name="sequence">SEQ_CCAR_ID</param>,它在做insert时会.做SELECT SEQ_CCAR_ID.NEXTVAL得到一个+1的ID,而你又定义了TI,在TI里在次得到一个SELECT SEQ_CCAR_ID.NEXTVAL的ID为此变成+2
<class name="com.yqTxl.db.table.Txl" table="TXL" schema="HR">
<id name="id" type="java.lang.Integer">
<column name="ID" precision="6" scale="0" />
<generator class="sequence">
<param name="sequence">seq_txl</param>
</generator>
</id>
。
保存就直接把txl先定义好,直接调用
TxlDAO dao = new TxlDAO();
dao.save(txl);
其中txl.id = -1 ;
让HIBERNATE自己来帮我们修改id的值。
--可能是其他的对象使用了此序列,只有重新建个建个序列,货触发器
下面是列子
已连接。
SQL> create table factory_carSave
2 (id number,
3 name varchar2(10),
4 age number
5 )
6 /表已创建。SQL> create sequence SEQ_CCAR_ID
2 minvalue 1
3 maxvalue 100000000
4 start with 1
5 increment by 1
6 cache 20
7 /序列已创建。SQL> CREATE OR REPLACE TRIGGER carS
2 BEFORE INSERT ON factory_carSave FOR EACH ROW
3 BEGIN
4 SELECT SEQ_CCAR_ID.NEXTVAL INTO :NEW.id FROM DUAL;
5 END;
6 /触发器已创建SQL> insert into factory_carSave(name) values('wkc');已创建 1 行。SQL> select * from factory_carSave; ID NAME AGE
---------- ---------- ----------
1 wkc
1* insert into factory_carSave(name,age) values('wkc168',23)
SQL> /已创建 1 行。
SQL> select * from factory_carSave; ID NAME AGE
---------- ---------- ----------
1 wkc
2 wkc168 23SQL> create table test as select * from factory_carSave where 1<>1;表已创建。SQL> insert into test values(SEQ_CCAR_ID.NEXTVAL,'wkc',23);已创建 1 行。SQL> select * from test; ID NAME AGE
---------- ---------- ----------
3 wkc 23SQL> insert into factory_carSave(name,age) values('wkc128',23);已创建 1 行。SQL> select * from factory_carSave; ID NAME AGE
---------- ---------- ----------
1 wkc
2 wkc168 23
4 wkc128 23
下面是我的saveCarSaveHibernateDAO.java
public void save(CarSave carSave) {
getHibernateTemplate().save(carSave);
}
CarSaveServiceImpl.java
private CarSaveDAO carSaveDao;
public void saveCarSave(CarSave carSave) {
carSaveDao.save(carSave);
}
CarFormAction.java
String carModel = request.getParameter("carModel");
String price = request.getParameter("price");
String text = request.getParameter("text");
String carbrand = request.getParameter("carbrand");
String index = request.getParameter("index");
String car = request.getParameter("car"); carSave.setAddTime(new Date());
carSave.setCarBrand(carbrand);
carSave.setPrice(price);
carSave.setCar(car);
carSave.setCarModel(carModel);
carSave.setOther(text);
carSave.setIsIndex(index); this.carSaveService.saveCarSave(carSave);