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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:48:29+00:00 2026-05-16T19:48:29+00:00

I have a database model with two tables: Session and ScriptExecution which are associated

  • 0

I have a database model with two tables: Session and ScriptExecution which are associated with a one-to-many relationship:

public class ScriptExecution implements Serializable {

 @ManyToOne
 @PrimaryKeyJoinColumn
 private Session session;

...
}

public class Session implements Serializable {

 @OneToMany(fetch=FetchType.EAGER, mappedBy="session")
 private Set<ScriptExecution> scriptExecutions;

...
}

If I create a session, load it and assign it to a script execution everything works fine:

Session session = new Session();
sessionDao.save(session);

session = sessionDao.load(1);

ScriptExecution scriptExecution = new ScriptExecution();
ScriptExecution.setSession(session);
scriptExecutionDao.save(scriptExecution);

If I use the created session directly to assign it to the ScriptExecution as follows:

Session session = new Session();
sessionDao.save(session);

ScriptExecution scriptExecution = new ScriptExecution();
ScriptExecution.setSession(session);
scriptExecutionDao.save(scriptExecution);

Hibernate fails with the following error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into script_execution (end, session_id, start, id) values (?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into script_execution (end, session_id, start, id) values (?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:637)
 org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)
 org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147)
 com.sc2.web.controller.MainController.handleRequest(MainController.java:45)
 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
 org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
 org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
 org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147)
 com.sc2.web.controller.MainController.handleRequest(MainController.java:45)
 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

root cause

java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`sc2`.`script_execution`, CONSTRAINT `fk_script_execution_session1` FOREIGN KEY (`session_id`) REFERENCES `session` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
 com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2020)
 com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1451)
 org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
 org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
 org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
 org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
 org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
 org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147)
 com.sc2.web.controller.MainController.handleRequest(MainController.java:45)
 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
 org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
 org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

I have also tried to put the code in a transaction, but the code fails with the same error.

TransactionTemplate template = new TransactionTemplate(transactionManager);
template.execute(new TransactionCallbackWithoutResult() {

   @Override
   protected void doInTransactionWithoutResult(TransactionStatus arg0) {
    Session session = new Session();
    sessionDao.save(session);

    ScriptExecution scriptExecution = new ScriptExecution();
    scriptExecution.setSession(session);
    scriptExecutionDao.save(scriptExecution);

   }
  });

Do you have any ideas what’s wrong? I can’t believe I have to load created entities after creating them.

  • 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-05-16T19:48:29+00:00Added an answer on May 16, 2026 at 7:48 pm

    I was able to solve the problem:
    The @Id annotated properties that represent the id’s in your entities and are set to auto-increment in your database schema have to be annotated with @GeneratedValue too, e.g.

    @Id
    @GeneratedValue
    private int id;
    

    instead of only.

    @Id
    private int id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two database tables (lets call them A and B) with many-to-many bridge
Say I have these two models: public class City extends Model { @ManyToOne private
I have the following two tables: Package id ClientPackage id clientNumber packageId The thing
dear all, i'll take some data from the database. and join two tables.the code
I am trying to develop a database model for a recruitment website and I
Is there a best practice in getting data from multiple database tables using Zend?
We have a simple Table per Type Entity Framework 4.0 model :- ALl classes
I've got the following entities on my EDMX :- These two entites were generated
For a new project I'm trying to create my business classes first and create

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.