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 7954879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:34:00+00:00 2026-06-04T03:34:00+00:00

I have trying to insert a record into the database (MySQL), using Entity Class

  • 0

I have trying to insert a record into the database (MySQL), using Entity Class and Entity Manager. But one of the field is an auto incrementing primary key, so unless I provide an value manually, the insertion is not successful.

public boolean newContributor(String name, String email, String country, Integer contactable, String address) {
        Contributors contributor = new Contributors(); //entity class
        contributor.setId(??????); //this is Primary Key field and is of int datatype
        contributor.setName(name);
        contributor.setEmail(email);
        contributor.setCountry(country);    
        contributor.setContactable(contactable);
        contributor.setAddress(address);

        em.persist(contributor);
        return true;
    }

How to solve such problem? Is there a way to tell the entity manager to attempt the insert without the ID field and use NULL value instead.

Update: Here is a portion of the entity class defining the id

...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 50)
....
  • 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-04T03:34:01+00:00Added an answer on June 4, 2026 at 3:34 am

    Is there a way to tell the entity manager to attempt the insert without the ID field and use NULL value instead?

    Sure. You need to remove the @NotNull annotation for id field in the @Entity definition, and also remove the row:

    contributor.setId(??????);
    

    from method newContributor(). The reason for this is that the @NotNull annotation enforces a validation check in the JPA stack. It doesn’t mean that the field is NOT NULL at a database level. See here a discussion about this issue.

    The rest of the code looks fine.

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

Sidebar

Related Questions

I'm using a mysql database, and I'm trying to insert a record into tableB
I'm trying to input orders into a MySQL database. It adds one record per
I am trying to insert a record into an Oracle database I have a
I have a problem when trying to insert a new record into a database.
I am trying to insert into mysql database dynamically. But I always see only
I'm trying parse an xml file and insert it into an mysql database. The
I am trying to insert data into my SQLite database. I have imported all
I have a form that is trying to insert some data into an SQL
I have created a datatable and trying to insert that datatable through SqlBulkCopy but
I'm trying to insert several newly created items to the database. I have a

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.