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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:59:15+00:00 2026-06-06T10:59:15+00:00

I have one to many association Student entity to Subject entity. Student entity association

  • 0

I have one to many association Student entity to Subject entity.

Student entity association mapping is

<class name="Student" table="STUDENT">
    <id name="studentId" column="STUD_ID">
        <generator class="increment"/>
    </id>
    <property name="studentName" column="STUD_NAME"/>
   <list access="field" lazy="false" name="subjects" cascade="all-delete-orphan" fetch="subselect">
     <key column="STUD_ID" />
     <list-index column="SUBJECT_ORDER_ID" />
     <one-to-many class="com.sample.Subject" />
    </list>
</class>

The Subject table mapping is

<class name="Subject" table="SUBJECT">
    <id name="subjectId" column="SUB_ID">
        <generator class="increment"/>
    </id>
   <property name="subjectName" column="SUB_NAME"/>

</class>

I have a table level constraint in the SUBJECT table that the STUDENT_ID and SUBJECT_ORDER_ID should not be null.

ALTER TABLE SUBJECT ADD CONSTRAINT CHK_FIELD CHECK(STUD_ID IS NOT NULL AND SUBJECT_ORDER_ID IS NOT NULL) 

The main class for saving these entries

public static void main(String[] args) {
         Session session = HibernateHelper.getSessionFactory().getCurrentSession();
         session.beginTransaction();

          Subject subject = new Subject();
          subject.setSubjectName("Test Subject2");
         // session.save(subject);
          List<Subject> subjects  = new ArrayList<Subject>();
          subjects.add(subject);
         Student student = new Student();
         student.setStudentName("Test student2");
         student.setSubjects(subjects);

         session.save(student);
         session.getTransaction().commit();
    }

When the Student entity is created with associated Subject. Hibernate is trying to create Subject entity with empty STUD_ID and SUBJCT_ORDER_ID and later assigning the mapping. So the Subject entity creation fails due to the constraint violation.
The exception log is

Hibernate: select max(STUD_ID) from STUDENT
Hibernate: select max(SUB_ID) from SUBJECT
Hibernate: insert into STUDENT (STUD_NAME, STUD_ID) values (?, ?)
Hibernate: insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?)
2012-06-07 17:26:52,484 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Check constraint violation: "CHK_FIELD: ((STUD_ID IS NOT NULL)
    AND (SUBJECT_ORDER_ID IS NOT NULL))"; SQL statement:
insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?) [23513-164]
2012-06-07 17:26:52,484 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Check constraint violation: "CHK_FIELD: ((STUD_ID IS NOT NULL)
    AND (SUBJECT_ORDER_ID IS NOT NULL))"; SQL statement:
insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?) [23513-164]
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
    at com.sample.TestMain.main(TestMain.java:27)
Caused by: org.h2.jdbc.JdbcBatchUpdateException: Check constraint violation: "CHK_FIELD: ((STUD_ID IS NOT NULL)
    AND (SUBJECT_ORDER_ID IS NOT NULL))"; SQL statement:
insert into SUBJECT (SUB_NAME, SUB_ID) values (?, ?) [23513-164]
    at org.h2.jdbc.JdbcPreparedStatement.executeBatch(JdbcPreparedStatement.java:1107)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
    ... 8 more

When I disable the constraint CHK_FIELD: STUD_ID and SUBJECT_ORDER_ID should not be null, both the entities created successfully.
Please help me in figure out whether I have missed any configuration?

  • 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-06T10:59:18+00:00Added an answer on June 6, 2026 at 10:59 am

    This entity creation Student and Subject is done in three steps

    1. Create the Student entity
    2. Create all the subject entity
    3. Set STUD_ID in all the Subject entity

    Since the the Subject entity has constraint STUD_ID should not be null. The second step violates this constraint.

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

Sidebar

Related Questions

I have these 2 mappings: <hibernate-mapping> <class name=sample.Operator table=OPERATOR> <id name=id > <generator class=native
I have a table with One-To-Many reflexive association. I need insert the first value
I'm trying to make simple many-to-one association, using NHibernate.. I have class Recruit with
I have two dependent models with one to many association class Post < ActiveRecord::Base
I have a entity Player that has one to many association to other entities.
If I have a company entity, with a one to many association to user
I've got to Tables related in one-to-many association: Product*1 - n*Inventory @Entity public class
I have a one-directional many-to-many association: the ListDefinition class has the Columns property of
I have a many-to-many association between a User class and a Table class. Additionally,
my issue lokks similar to this one: (link) but i have one-to-many association: <set

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.