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

  • Home
  • SEARCH
  • 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 8527865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:40:37+00:00 2026-06-11T08:40:37+00:00

When saving an object to database using hibernate, it sometimes fails because of certain

  • 0

When saving an object to database using hibernate, it sometimes fails because of certain fields in the object exceeding the maximum varchar length defined in the database.

Therefore I am using the following approach:

  1. Attempt to save
  2. If getting an DataException, I then truncate the fields in the object to the max length specified in the db definition, then try to save again.

However, in the second save after truncation, I’m getting the following exception:

hibernate: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails

Here’s the relevant code, do you notice anything wrong with it?

public static void saveLenientObject(Object obj){

    try {
        save2(rec);
    } catch (org.hibernate.exception.DataException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();


        saveLenientObject(rec, e);



    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



}
private static void saveLenientObject(Object rec, DataException e) {
    Util.truncateObject(rec);
    System.out.println("after truncation ");
    save2(rec);

}
public static void save2(Object obj) throws Exception{
    try{
        beginTransaction();
        getSession().save(obj);

        commitTransaction();

    }catch(Exception e){
        e.printStackTrace();
        rollbackTransaction();
        //closeSession();
        throw e;
    }finally{
        closeSession(); 
    }
}    
  • 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-11T08:40:38+00:00Added an answer on June 11, 2026 at 8:40 am

    The hibernate documentaiton is quite clear that once an exception is thrown the session will be left in an inconsistent state so is not safe to use for further operations. I suspect that what you’re getting here is that the session is left half saving your first attempt so bad things happen.

    Fundamentally you should not rely on database errors to check the length of your fields, instead you should pre-validate this in java code. If you know the lengths enough to truncate, then I suggest you simply call your trucate util every time.

    Alternatively use Hibernate Validator to declaratively validate the objects before saving.

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

Sidebar

Related Questions

Say you create an object and saving to database by using ADO Entity Framwork
I have below method to save an object to database using Hibernate but I
I am using sequence for saving domain object instance in my oracle database. I
I am saving an XMLDocument object to disk using the Save (string) function .
Im primarily using Hibernate to reduce the memory consumption of my application because I
I'm using DDD and NHibernate to persist my domain object. In my database every
I'm using JPA (Hibernate implementation) to save objects to the database. Selecting works fine,
I've got an object model that is persisted using Seam and JPA (Hibernate). It
All my object newmedia are saving with the same name. how can I fix
I have a C# object that I am saving to RavenRB (build 1.0.960), the

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.