Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:32:04+00:00 2026-06-13T18:32:04+00:00

I have a Java POJO: public class Widget { private int id; private String

  • 0

I have a Java POJO:

public class Widget {
    private int id;
    private String name;
    // ...
}

I am trying to use MyBatis to insert a new Widget into the widget table of my Postgres database, and for this insert method to inject the passed-in POJO with the auto-generated id of the newly-inserted widget table record:

// Notice the widget's id is left null.
Widget w = new Widget("name-of-widget");

WidgetMapperImpl widgetMapper = new WidgetMapperImpl();
widgetMapper.insertWidget(w);

// At runtime this prints null (the id is not being set).
System.out.println(w.getId());

And WidgetMapper.java:

public interface WidgetMapper {
    public void insertWidget(@Param("widget") Widget widget);
}

Here is the WidgetMapper.xml:

<mapper namespace="com.myapp.WidgetMapper">
    <cache flushInterval="360000" />

    <resultMap id="WidgetMapperResult" type="com.myapp.Widget">
        <result property="id" column="widget_id"/>
        <result property="name" column="widget_name" />
    </resultMap>

    <insert id="insertWidget" parameterType="com.myapp.Widget" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
            myapp.widgets
            (
                widget_name
            )
            VALUES
            (
                #{widget.name}
            );

            <selectKey keyProperty="id" resultType="int" order="AFTER">
                SELECT CURRVAL('widget_id_seq') AS widget_id
            </selectKey>
    </insert>
</mapper>

Again, this code compiles and builds fine. At runtime, after I call widgetMapper.insertWidget(Widget), and then try to access that’s Widget‘s id, it is null, so the ID-injection is not working. Can anyone spot why? Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:32:05+00:00Added an answer on June 13, 2026 at 6:32 pm

    As far as I remember, you cannot use useGeneratedKeys="true" and selectKey at the same time.

    So a solution can be: either the implementation of the getGeneratedKeys method of the JDBC driver for Postgres can deal with what you need (I don’t know how JDBC and postgres sequences works together), so you use only useGeneratedKeys="true". Or you have to use selectKey, then you have to turn of the useGeneratedKeys attribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using GWT I have a Java class: public class Pojo { private String name;
I have the following pojo: public class Foo { @Size(min=0,max=10) private String bar =
Let's say I have a simple POJO class: public void Foo { public int
I have a simple POJO like public class Employee { int level; int salary;
I have a Java class which represents the correlation between two elements (typical POJO):
I have a DSL Java object, i.e. a POJO which returns this in setters
I have Java code and TeX documents in the same project. I use TeXlipse
I have a Java Swing application client that I want to use to consume
I am trying to create a utility class that accepts any type of POJO,
I have table /entity called SaleRecord with fields such as @Entity public class SaleRecord

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.