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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:08:08+00:00 2026-06-11T15:08:08+00:00

Using hibernate for data access. I have a column in database: varchar(40), default value

  • 0

Using hibernate for data access.

I have a column in database:

varchar(40), default value is set to 0, NOT NULL

I have a case, that user sends null value, and getting error:

Error: org.hibernate.exception.ConstraintViolationException: Cannot insert the value
    NULL into column 'foo', table 'MyTable'; column does not allow nulls. INSERT fails.

And the column in hibernate is defined like this:

@Column(name = "foo")
private String foo;

Where could the problem be? Must I define the default in hibernate annotations or smthing? How? Or any other suggetsions?

  • 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-11T15:08:09+00:00Added an answer on June 11, 2026 at 3:08 pm

    What did you expect would happen when trying to insert NULL into a NOT NULL column? Either you want to enforce a NOT NULL constraint, and then you need to reject the user input, or you want to accept NULL values and need to specify that the column is nullable using, of course, @Column(nullable = true).


    Since you actually want a default value in the column when the user doesn’t provide that field, and your code explicitely sets the field value even when it’s null (or empty, which is the same for Oracle, for example), I suggest having a smarter setter on the field:

    private String foo = "0";
    
    public String getFoo() {
        return foo;
    }
    
    public void setFoo(String foo) {
        if (foo != null && !foo.isEmpty()) { // Or StringUtils.isNotBlank(foo)
            this.foo = foo;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One challenge using hibernate is that manged classes have to have a default constructor
I'm using hibernate for the data access layer, so i have to write HQL
I needed to create an application using Struts2 as MVC,Hibernate for data access and
I'm using SQL Server 2008 with Hibernate 3.0 at data access layer of my
I have build my data model using JPA and am using Hibernate's EntityManager 3
I am using Spring for DI and Hibernate for data access on a mySQL
I am using playframework with a legacy SQL Server 2008 database that has data
I am using Hibernate in a Java application to access my Database and it
I have a basic MVC application that is using NHibernate for OR/M and data
I am using Hibernate in an Eclipse RAP application. I have database tables mapped

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.