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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:46:06+00:00 2026-06-03T00:46:06+00:00

forum member I am having one problem with the insertion of data using Hibernate

  • 0

forum member I am having one problem with the insertion of data using Hibernate JPA framework.

I am having one-to-many relationship between task and resources.

my task model is like below shown code.

@Entity
@Table(name = "task")
public class Task {

    private Integer id;
    private Set<Employee> employee = new HashSet<Employee>(0);

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @OneToMany(cascade = CascadeType.ALL, fetch =FetchType.EAGER)
    @JoinTable(name = "task_employee", joinColumns = { @JoinColumn(name = "taskid") }, inverseJoinColumns = { @JoinColumn(name = "employeeid") })
    public Set<Employee> getEmployee() {
        return employee;
    }

    public void setEmployee(Set<Employee> employee) {
        this.employee = employee;
    }


}

and my Employee model is

@Entity
@Table(name = "employee")
public class Employee {

    private Integer id;
    private String firstname;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "firstname")
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

}

I am able to insert the record successfully when the

1. Task id is 1 and employeeid is 1,

2. Task id is 1 and employeeid is 2,

3. Task id is 2 and employeeid is 3,

but the error occurs when the employee id is duplicated like below insertion.

4. Task id is 1 and employeeid is 1

it gives me error

79512 [http-8080-4] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
79517 [http-8080-4] ERROR org.hibernate.util.JDBCExceptionReporter - Duplicate entry '2' for key 'employeeid'
79517 [http-8080-4] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

What’s wrong with my code? Please, help me!!!

  • 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-03T00:46:08+00:00Added an answer on June 3, 2026 at 12:46 am

    You have a OneToMany relation ship. This mean a task can have many employee, but a single employee can have only on task!

    error occurs when the employee id is duplicated

    But you violated (or tryed to violate) the second part of that constraint

    So you should use a ManyToMany relation ship.

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

Sidebar

Related Questions

hi forum member I am having one problem with setting the radiofield in the
forum member I am having one problem with date time in java. Actually I
forum member I need one help from you all. I am having two POJO
forum member I am having some problems with the file upload in extjs 4
I have a basic form that I'm using for member registration. I'm using the
I built a custom forum for my site using MySQL. The listing page is
I'm having trouble in saving a m2m data, containing a 'through' class table. I
I'm having problems with updating a child model via nested form. I've read many
Is their a way to use a non-member non-friend function on an object using
We are using C#, ASP.NET & WCF. I am trying to send form data

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.