J-Hi development diary (2)

Source: Internet
Author: User

Zhang Hao

Recently, when J-Hi is integrated with SpringJDBC, how does one retrieve the record's primary key value when inserting a record? The problem is mainly related to the handling of SpringJDBC across databases. We all know that the values of primary keys like SQLServer or MyServer are auto-incrementing, similar to the Oracle primary value, values are generated in sequence and inserted directly into the table through insert. Therefore, SpringJDBC provides two mechanisms,
1. Primary Key auto-increment Solution
KeyHolder keyHolder = new GeneratedKeyHolder ();
This. getJdbcTemplate (). update (new PreparedStatementCreator (){

Public PreparedStatement createPreparedStatement (Connection con)
Throws SQLException {

PreparedStatement ps = con. prepareStatement (SQL, Statement. RETURN_GENERATED_KEYS );
 
Return ps;
}

}, KeyHolder );

Return keyHolder. getKey (). intValue ();
KeyHolder is the owner of the auto-increment primary key value of the database. It listens to the value Statement returned by PreparedStatement. RETURN_GENERATED_KEYS: Get the primary key value and store it in your own pool (actually a list). Generally, a keyHolder instance is bound to only one PreparedStatement for execution. Of course, you 'd better insert a database record, this ensures that there is only one primary key value in the pool.
After the keyHolder obtains the primary key value, you can access the keyHolder object to obtain the primary key value at any time. That is to say, the value of this primary key will not be lost as long as its life cycle exists.
Conclusion: 1) Create the keyHolder object of the auto-incrementing primary key before executing the PreparedStatement.
2) When creating a PreparedStatement object, you must declare the returned primary key value, such as con. prepareStatement (SQL, Statement. RETURN_GENERATED_KEYS)
3) as long as the life cycle of keyHolder exists, you can obtain the value of the primary key at any time and position.

2. Solutions for retrieving primary keys generated by database Sequences
OracleSequenceMaxValueIncrementer incr = new OracleSequenceMaxValueIncrementer (dataSource, "SIMPLE_SEQUENCE ");
Return incr. nextIntValue (); SpringJDBC solutions for databases such as Oracle can provide the maximum value of this sequence by specifying the data source and the sequence name "SIMPLE_SEQUENCE. Of course, you can also use this class to design the buffer size by using the setCacheSize method. This method can retrieve multiple values at a time to reduce the number of accesses to the database (Database interaction is time-consuming and resource-consuming)

J-Hi Problem and Solution
Because J-Hi needs to implement cross-database and cross-multiple ORM frameworks, SpringJDBC must be integrated into the two solutions, in addition, the overall design should be compatible with the interface declarations of other ORM frameworks (currently, the J-Hi converged ORM frameworks include hibernate, ibatis2, and ibatis3, therefore, I learned from the hibernate dialect idea in the overall design of SpringJDBC integration, springJDBC is integrated into J-Hi through dialects to achieve different primary key management for different types of databases.
KeyHolder keyHolder = new GeneratedKeyHolder ();
This. getJdbcTemplate (). update (new PreparedStatementCreator (){

Public PreparedStatement createPreparedStatement (Connection con)
Throws SQLException {

ISpringJDBCHiDialect dialect = sessionFactory. getDialect ();
PreparedStatement ps = con. prepareStatement (SQL, Statement. RETURN_GENERATED_KEYS );
 
If (stepFlage = primaryKeyIndex & valueClass. getPropertyName (). equals (primaryKeyName )){
Number _ id = dialect. getSelectKey (entity. getEntityName (), getJdbcTemplate (). getDataSource ());
Return ps;
}

}, KeyHolder );

If (obj. getPrimarykey () = null)
BeanUtil. setPropertyValue (obj, "id", keyHolder. getKey (). intValue ());
From the perspective of native SQL statements, Oracle needs to insert the primary key value during insert, while SQL server does not have to insert the primary key value on the contrary. In design, I grasp this feature, combined with dialects, SpringJDBC and dialect are implemented across databases. the getSelectKey () method corresponds to different database dialects. If it is oracle, the primary key value is generated. If it is SQLServer, this method does not return any value. The Code is as follows:
Oracle dialect method:
Public Number getSelectKey (String entityName, DataSource dataSource ){
OracleSequenceMaxValueIncrementer incr = new OracleSequenceMaxValueIncrementer (dataSource, "HIBERNATE_SEQUENCE ");
Return incr. nextIntValue ();
} Dialect method:
Public Number getSelectKey (String entityName, DataSource dataSource ){
// This method is not required for auto-increment primary keys.
Return null;
} Whether the primary key value is null is returned, and whether the value of the primary key field is inserted in the spelling SQL statement
Finally
If (obj. getPrimarykey () = null)
BeanUtil. setPropertyValue (obj, "id", keyHolder. getKey (). intValue ());
Whether the Pojo object has a primary key value (if not, it indicates that it is an auto-incrementing type such as SQLServer, and if so, it indicates that it is a sequence generated such as Oracle) to assign it to the POJO attribute.

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.