Results 1 to 3 of 3

Thread: SimpleJdbcInsert and auto-generated key problem?

  1. #1
    Join Date
    May 2007
    Location
    Istanbul
    Posts
    42

    Default SimpleJdbcInsert and auto-generated key problem?

    I'm using SimpleJdbcInsert object for inserting new record into Oracle 10g database. Table consists of two columns. id for pkey, and name for description. I wanna use usingGeneratedKeyColumns("id") method to generate pkey for insert statement. For this purpose, I'm creating a new instance with this statement:

    Code:
    SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(dataSource)
    		.withTableName("TPaymentOrgSector")
    		.usingGeneratedKeyColumns("id");
    And create method of my dao impl like this:
    Code:
    Number newKey = this.simpleJdbcInsert.executeAndReturnKey(createParameterSource(po));
    int newId = newKey.intValue();
    Also createParameterSource method:
    Code:
    private MapSqlParameterSource createParameterSource(PaymentOrganizationSector po) {
    		return new MapSqlParameterSource()
    		.addValue("NAME", po.getName());
    }
    in this method, i'm not mapping "id" column. because i want it to be created by Spring. But when i run the code,

    Code:
    org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL []; ORA-01400: cannot insert NULL into ("TKRY"."TPAYMENTORGSECTOR"."ID")
    ; nested exception is java.sql.SQLException: ORA-01400: cannot insert NULL into ("TKRY"."TPAYMENTORGSECTOR"."ID")
    exception occurs. What spring does here if i explicitly set the id column? I'm leaving it null, becase i expect it to be generated somehow by Spring. Am i missing some important point? What is the generation rule here? Do i need a sequence or trigger. I couldn't find any clue in the 2.5 Spring documentation...

    Thanks in advance.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    exception occurs. What spring does here if i explicitly set the id column? I'm leaving it null, becase i expect it to be generated somehow by Spring. Am i missing some important point? What is the generation rule here? Do i need a sequence or trigger. I couldn't find any clue in the 2.5 Spring documentation...
    Your understanding of the feature is off... Spring doesn't generate anything it lets the database generate a key and returns it for you. However the database must support the feature and a key must be generated by the database. For oracle this means specifing a trigger on the column which generates the key for you, in MySQL you can use an autoincrement column. As stated Spring isn't generating anything for you and that is where your logic is off...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2007
    Location
    Istanbul
    Posts
    42

    Default

    Hi, Marten

    I misunderstood the concept of auto-generated key then. It only returns the value that has been created at db layer...

    Thanks,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •