public T insert ( T entity ) { if ( ! hasPrimaryKey ( entity ) ) { throw new RuntimeException ( String . format ( "Tried to insert entity of type %s with null or zero primary key" , entity . getClass ( ) . getSimpleName ( ) ) ) ; } InsertCreator insert = new InsertCreator ( table ) ; insert . setValue ( idColumn . getColumnName ( ) , getPrimaryKey ( entity ) ) ; if ( versionColumn != null ) { insert . setValue ( versionColumn . getColumnName ( ) , 0 ) ; } for ( Column column : columns ) { if ( ! column . isReadOnly ( ) ) { insert . setValue ( column . getColumnName ( ) , getFieldValueAsColumn ( entity , column ) ) ; } } new JdbcTemplate ( ormConfig . getDataSource ( ) ) . update ( insert ) ; if ( versionColumn != null ) { ReflectionUtils . setFieldValue ( entity , versionColumn . getFieldName ( ) , 0 ) ; } return entity ; }
Insert entity object . The caller must first initialize the primary key field .