Hi,
also ich habe im Prinzip ein ganz einfaches Problem, weiß jedoch nicht wo in Hibernate ich suchen muss, damit ich den vermeindlichen Fehler finden muss. Ich habe eine Sequenz für auto_increment erstellt in Oracle, diese funktioniert auch Fehlerfrei, sobald ich sie aber von Oracle ausführen lassen, erstellt er die ID falsch, sprich die ID, in sparePart.getSPID() = 47 in der Datenbank diejenige aber 48. Also immer +1.
Jemand eine Ahnung woran das liegt?
Hibernate version: 3
Mapping file: <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="itecon.bo.model.Sparepart" table="SPAREPART" schema="DELL">
<id name="Spid" type="java.lang.Long">
<column name="SPID" scale="22" precision="0" not-null="true" unique="true" sql-type="NUMBER" />
<generator class="sequence">
<param name="sequence">AINCREMENT</param>
</generator>
</id>
<property name="Spname" type="java.lang.String">
<column name="SPNAME" scale="100" precision="0" not-null="true" sql-type="VARCHAR2" />
</property>
<property name="Spinventory" type="java.lang.Integer">
<column name="SPINVENTORY" scale="22" precision="0" not-null="true" sql-type="NUMBER" />
</property>
</class>
</hibernate-mapping>
Databaseversion: Oracle 10g
The generated SQL (show_sql=true):
select AINCREMENT.nextval from dual
Hibernate: select AINCREMENT.nextval from dua
insert into DELL.SPAREPART (SPNAME, SPINVENTORY, SPID) values (?, ?, ?)
Hibernate: insert into DELL.SPAREPART (SPNAME, SPINVENTORY, SPID) values (?, ?, ?)l
Sequence:
Create sequence aincrement start with 1 increment by 1 nomaxvalue;
CREATE OR REPLACE TRIGGER "DELL"."INCREMENT_SPID" BEFORE
INSERT ON "DELL"."SPAREPART" REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW Begin
select AINCREMENT.nextval into :NEW.SPID from DUAL;
End;
DAOTest:
Code:
/**
* 17.05.2005
* 16:46:00
* johannes.hiemer
* SparepartDAOTest.java
*/
package itecon.dao.hibernate.test;
import java.util.List;
import itecon.bo.dao.ISparepartDAO;
import itecon.bo.model.Sparepart;
import junit.awtui.TestRunner;
import org.apache.log4j.Logger;
/**
* @author johannes.hiemer
*
* ITecon Dell Call System
*/
public class SparepartDAOTest extends BaseDAOTestCase {
private ISparepartDAO sparePartDAO = null;
private Sparepart sparePart = null;
protected void setUp() throws Exception {
log = Logger.getLogger(SparepartDAOTest.class.getName());
sparePartDAO = (ISparepartDAO) ctx.getBean("sparepartDAO");
}
protected void tearDown() throws Exception {
sparePartDAO = null;
}
public static void main(String args []) {
TestRunner.run(SparepartDAOTest.class);
}
public Sparepart createSparepart() {
sparePart = new Sparepart();
sparePart.setSpinventory(new Integer(2));
sparePart.setSpname("TX-45359A34");
return sparePart;
}
public void testAddAndRemoveSparepart() throws Exception {
createSparepart();
log.info(sparePart.getSpid());
sparePartDAO.saveSparepart(sparePart);
List spareList = sparePartDAO.getSpareparts();
log.info(spareList.get(0).toString());
assertTrue("primary key assigned", sparePart.getSpid() != null);
log.info(sparePart.getSpid());
assertTrue(sparePart.getSpname() != null);
sparePartDAO.removeSparepart(sparePart.getSpid()+1);
assertTrue("primary key not assigned", sparePart.getSpid() != null);
}
/**
* @return Returns the sparePart.
*/
public Sparepart getSparePart() {
return sparePart;
}
}
Gruß Valentin-