Ibatis Selectkey Usage Issues

Source: Internet
Author: User

It's actually a shipment_history table. Add a primary key sequence ID Shipmenthistoryid, add a record, and return this sequence ID

XML code
  1. <insert id="Abatorgenerated_insert" parameterclass="Cn.hot.delivery.domain.ShipHistory" >
  2. Insert into Shipment_history (shipment_id, Record_created_date, REMARK)
  3. VALUES (#shipmentId:D ecimal#, #recordCreatedDate:D ate#, #remark: varchar#)
  4. <selectkey keyproperty="Shipmenthistoryid" resultclass="Java.math.BigDecimal">
  5. Select Shipment_history_id_sequence.nextval from dual
  6. <<!---->selectkey>
  7. <<!---->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
  1. <!-oracle SEQUENCE Example-->
  2. <insert id="insertproduct-oracle" parameterclass="com.domain.Product">
  3. <selectkey resultclass="int" keyproperty="id" >
  4. SELECT stockidsequence. Nextval as ID from DUAL
  5. <!----><selectkey>
  6. Insert into PRODUCT (prd_id,prd_description)
  7. VALUES (#id #, #description #)
  8. <insert>
  9. <!---->
  10. <insert id="Insertproduct-ms-sql" parameterclass="com.domain.Product">
  11. Insert into PRODUCT (prd_description)
  12. VALUES (#description #)
  13. <selectkey resultclass="int" keyproperty="id" >
  14. SELECT @ @IDENTITY as ID
  15. <<!---->selectkey>
  16. <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
  1. <insert id="Abatorgenerated_insert" parameterclass="Cn.hot.delivery.domain.ShipHistory" >
  2. <selectkey keyproperty="Shipmenthistoryid" resultclass="Java.math.BigDecimal " >
  3. Select Shipment_history_id_sequence.nextval as value from dual
  4. <!----><selectkey>
  5. 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#)
  6. <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
  1. < Insert id="Abatorgenerated_insert " parameterclass="cn.hot.delivery.domain.ShipHistory">   
  2. Insert into Shipment_history (shipment_history_id,shipment_id,record_created_date, REMARK)
  3. Values
  4. (Shipment_history_id_sequence.nextval, #shipmentId:D ecimal#, #recordCreatedDate:D ate#, #remark: varchar#)
  5. <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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.