简体   繁体   中英

iBatis insert statement throwing NPE

I am new to iBatis. In my project we are using iBatis to persist the java objects in Oracle DB. I have a class, ClassA, which is having 3 columns : id, name and description. The data is going to be persisted in TableA. There is a sequence in DB to generate the value for id column in this table. We wrote the insert statement to this table as follows,

<insert id="insertTableA" parameterClass="com.ClassA">
    <selectKey resultClass="java.lang.Long" keyProperty="id">
       SELECT seq_TableA.nextval as id FROM dual
    </selectKey>
    INSERT INTO TableA(ID, NAME, DESCRIPTION) VALUES (#id#, #name#, #description#)
</insert>

This worked fine.

But becaude of our inhouse UI framework limitation we had to change some design. So we need to first generate the id long from sequence, set that value in an instance of ClassA along with name and description and then insert into DB. So in that case the insert statment doesn not need a selectKey attribute. The id , name and description values are in the object. When I updated the query like below, it is throwing Null Pointer Exception .

<insert id="insertTableA" parameterClass="com.ClassA">
    INSERT INTO TableA(ID, NAME, DESCRIPTION) VALUES (#id#, #name#, #description#)
</insert>

How we can insert data into table without using a . I am generating the key from the sequence first , populate the object with all values including the id and trying to call the statement from Java as follows,

getSqlTemplate().insert("process.insertTableA", instanceClassA);

Any pointers are welcome,

Thanks, SD

可以肯定的是,您是否在ClassA类中包含了getId()方法,以便它将返回id字段的值?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM