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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:53:40+00:00 2026-06-12T05:53:40+00:00

I have two tables between which I am trying to establish a 1-to-1 mapping

  • 0

I have two tables between which I am trying to establish a 1-to-1 mapping using Hibernate.

ExamDetail.java

package com.hibernate.mapping;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="exam_detail")
public class ExamDetail {

    private int id;
    private String fullName;
    private int numberOfQuestions;
    private int passingPercentage;

    @Id
    @GeneratedValue
    @Column(name="id")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getFullName() {
        return fullName;
    }
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
    public int getNumberOfQuestions() {
        return numberOfQuestions;
    }
    public void setNumberOfQuestions(int numberOfQuestions) {
        this.numberOfQuestions = numberOfQuestions;
    }
    public int getPassingPercentage() {
        return passingPercentage;
    }
    public void setPassingPercentage(int passingPercentage) {
        this.passingPercentage = passingPercentage;
    }
}

Exam.java

package com.hibernate.mapping;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="exam")
public class Exam {

    private int id;
    private String shortName;
    private ExamDetail detail;

    @Id
    @GeneratedValue
    @Column(name="id")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getShortName() {
        return shortName;
    }
    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    @OneToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name="detail_id")
    public ExamDetail getDetail() {
        return detail;
    }
    public void setDetail(ExamDetail detail) {
        this.detail = detail;
    }
}

TestMapping.java

package com.hibernate.mapping;

import org.hibernate.Session;

import com.hibernate.util.HibernateUtil;

public class TestMapping {

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

        Exam exam = new Exam();
        exam.setShortName("SCJA");

        ExamDetail detail = new ExamDetail();
        detail.setFullName("Sun Certified Associate");
        detail.setNumberOfQuestions(50);
        detail.setPassingPercentage(60);

        exam.setDetail(detail);

        session.save(exam);
        HibernateUtil.commitTransaction();
    }
}

When I ran this code, I encounter following exception :-

Exception in thread “main”
org.hibernate.exception.ConstraintViolationException: could not
insert: [com.hibernate.mapping.Exam]
at
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2186)
at
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2666)
at
org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) at
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
at
org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
at
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at
org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at
org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at
org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562) at
org.hibernate.impl.SessionImpl.save(SessionImpl.java:550) at
org.hibernate.impl.SessionImpl.save(SessionImpl.java:546) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy14.save(Unknown Source) at
com.hibernate.mapping.TestMapping.main(TestMapping.java:22) Caused by:
com.sybase.jdbc2.jdbc.SybSQLException: The column detail_id in table
exam does not allow null values.

at com.sybase.jdbc2.tds.Tds.processEed(Tds.java:2535) at
com.sybase.jdbc2.tds.Tds.nextResult(Tds.java:1916) at
com.sybase.jdbc2.jdbc.ResultGetter.nextResult(ResultGetter.java:69)
at
com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:201)
at
com.sybase.jdbc2.jdbc.SybStatement.nextResult(SybStatement.java:182)
at
com.sybase.jdbc2.jdbc.SybStatement.executeLoop(SybStatement.java:1596)
at com.sybase.jdbc2.jdbc.SybStatement.execute(SybStatement.java:1588)
at
com.sybase.jdbc2.jdbc.SybPreparedStatement.execute(SybPreparedStatement.java:580)
at
org.hibernate.id.IdentityGenerator$InsertSelectDelegate.executeAndExtract(IdentityGenerator.java:138)
at
org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
… 22 more

What am I doing wrong ?

Any missing statement ?

  • 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-12T05:53:41+00:00Added an answer on June 12, 2026 at 5:53 am

    You need to reference ID generation in JPA.

    Here : reference

    JPA Example

    ID Generation

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

Sidebar

Related Questions

I have two tables both of which have columns StartDate and EndDate. I'm trying
I have two tables. I am trying to find rows in one table which
I have two tables Service and Provider . Between them is a joining table
We have two tables Family and Member, the relation between these two is Family
I have two tables, blogs and posts, with the expected relationship between them. Posts
In my model, I have two tables with a 1:0..1 relationship between them: (Example
android noob... I have two tables, with a one to many relationship between country_tbl
Let's say, I have two tables, entities and tags. Relation between them is expressed
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a need to sync auto_increment fields between two tables in different databases

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.