It's actually a shipment_history table. Add a primary key sequence ID Shipmenthistoryid, add a record, and return this sequence ID
XML code
- <insert id="Abatorgenerated_insert" parameterclass="Cn.hot.delivery.domain.ShipHistory" >
- Insert into Shipment_history (shipment_id, Record_created_date, REMARK)
- VALUES (#shipmentId:D ecimal#, #recordCreatedDate:D ate#, #remark: varchar#)
- <selectkey keyproperty="Shipmenthistoryid" resultclass="Java.math.BigDecimal">
- Select Shipment_history_id_sequence.nextval from dual
- <<!---->selectkey>
- <<!---->insert>
And this section of the statement is automatically generated by Abator, pretty sure it won't go wrong. But this statement in the run time error, said can not insert the nullable value inserted into the Shipment_history table, indicating that this selectkey does not play a role at all.
Looked up the Chinese document of Ibatis, which is explained by the following:
Many databases support the automatic generation of data types for primary keys. However, this is usually (not always) a private feature. SQL map supports auto-generated key values through <insert></insert> 's child elements <selectkey></selectkey>. It supports both pre-build (such as Oracle) and two types of epigenetic (such as Ms-sql Server). Here are two examples:
XML code
- <!-oracle SEQUENCE Example-->
- <insert id="insertproduct-oracle" parameterclass="com.domain.Product">
- <selectkey resultclass="int" keyproperty="id" >
- SELECT stockidsequence. Nextval as ID from DUAL
- <!----><selectkey>
- Insert into PRODUCT (prd_id,prd_description)
- VALUES (#id #, #description #)
- <insert>
- <!---->
- <insert id="Insertproduct-ms-sql" parameterclass="com.domain.Product">
- Insert into PRODUCT (prd_description)
- VALUES (#description #)
- <selectkey resultclass="int" keyproperty="id" >
- SELECT @ @IDENTITY as ID
- <<!---->selectkey>
- <insert>
This means that for Oracle,<selectkey> statement must be pre-placed, and the sequence ID must be put into the insert into sentence, there will be no problem. Follow the above changes to my own XML as follows, sure enough no problem
XML code
- <insert id="Abatorgenerated_insert" parameterclass="Cn.hot.delivery.domain.ShipHistory" >
- <selectkey keyproperty="Shipmenthistoryid" resultclass="Java.math.BigDecimal " >
- Select Shipment_history_id_sequence.nextval as value from dual
- <!----><selectkey>
- Insert into Shipment_history (shipment_hestory_id,shipment_id, Record_created_date, REMARK) value (#shipmentHistoryId :D ecimal#, #shipmentId:D ecimal#, #recordCreatedDate:D ate#, #remark: varchar#)
- <insert>
So for a different database. <!---->selectkey> Usage may not be the same, the Internet is explained by a lot of data is based on different database drivers, and then <!---->selectkey the use of > is not the same. But even if you use Abator to be generated, and in the Abator configuration file
<jdbcconnection driverclass= "Oracle.jdbc.driver.OracleDriver" ></jdbcconnection>
Represents a oracledriver driver, but Abator does not generate different <!----> Selectkey > Statements depending on the driver,so if you use Oracle, This statement can only be changed by hand.
In fact, you can also use the INSERT statement to call Sequence.nextval's method directly to generate the sequence ID, for example
XML Code
- < Insert id="Abatorgenerated_insert " parameterclass="cn.hot.delivery.domain.ShipHistory">
- Insert into Shipment_history (shipment_history_id,shipment_id,record_created_date, REMARK)
- Values
- (Shipment_history_id_sequence.nextval, #shipmentId:D ecimal#, #recordCreatedDate:D ate#, #remark: varchar#)
- <insert >
This statement can generate sequence and insert records, the only drawback is that you can't pass
Object NewKey = Getsqlmapclienttemplate (). Insert (
"Shipment_history.abatorgenerated_insert", record);
At the same time get newkey this sequence
In short, relatively speaking,<!---->selectkey> is still relatively useful, as long as you pay attention to the database you use the problem (besides, Ibatis does not block the differences between the database)
Ibatis Selectkey Usage Issues