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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:20:20+00:00 2026-05-28T06:20:20+00:00

I got a problem with relations in Hibernate. Scenario: I got 2 Entities ‘Task’

  • 0

I got a problem with relations in Hibernate.

Scenario: I got 2 Entities ‘Task’ and ‘Resource’. Every ‘Task’ can have multiple ‘Resource’s and every ‘Resource’ can have multiple ‘Task’s. So that the relation is a n:m-Relation. In the relation table I need an additional column for the type of ‘Resource’ because there are different roles in the ‘Task’ that the ‘Resource’ can have.

I found some examples for join table with extra column and tried to implement it that way. Example: http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/

The problem:
When I delete a ‘Task‘ that has an association with ‘Resource’ an javax.persistence.EntityNotFoundException: deleted entity passed to persist: […] is thrown.

Now my question:
Is the ‘join table with extra column’ the best way handle this scenario? Or is it better to use simple ManyToMany(@JoinTable) Relations for every role that the ‘Resource’ can have?
For example: When there are 4 roles there would be 4 different relation tables between ‘Task’ and ‘Resource’.

What can be the reason for the following exception?

‘javax.persistence.EntityNotFoundException: deleted entity passed to persist: […]’

Edited:

deleted entity passed to persist: [com.domain.Task# ]
    org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1369)
    org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)
    org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81)
    org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
    org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    $Proxy43.deleteVorgaenge(Unknown Source)
    com.service.locklayers.TaskLockService.deleteTasks(TaskLockService.java:364)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:303)
    sun.rmi.transport.Transport$1.run(Transport.java:159)
    java.security.AccessController.doPrivileged(Native Method)
    sun.rmi.transport.Transport.serviceCall(Transport.java:155)
    sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
    sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
    sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
    java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    java.lang.Thread.run(Thread.java:662)

Here are the involved Classes…

@Entity
@Table( name = "task" )
public class Task implements Serializable
{
  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @Column( name = "task_id" )
  private int         taskId;

  private String      name;

  @OneToMany( fetch = FetchType.EAGER, mappedBy = "taskFk", cascade = CascadeType.ALL )
  Set<TaskZuResource> taskZuResourceList = new HashSet<TaskZuResource>();

  public int getId()
  {
    return taskId;
  }
}

@Entity
@Table( name = "resource" )
public class Resource implements Serializable
{
  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @Column( name = "resource_id" )
  private int         resourceId;

  private String      name;


  @OneToMany( mappedBy = "resourceFk", fetch = FetchType.EAGER, cascade = CascadeType.ALL )
  Set<TaskZuResource> taskZuResourceList = new HashSet<TaskZuResource>();

  public int getId()
  {
    return resourceId;
  }
}

@Entity
@Table( name = "task_zu_resource" )
public class TaskZuResource implements Serializable
{
  @EmbeddedId
  private TaskZuResourceId pk;

  @MapsId( "taskFk" )
  @ManyToOne( optional = false, fetch = FetchType.EAGER )
  @JoinColumn( name = "task_fk" )
  private Task                 taskFk;

  @MapsId( "resourceFk" )
  @ManyToOne( optional = false, fetch = FetchType.EAGER )
  @JoinColumn( name = "resource_fk" )
  private Resource             resourceFk;

  public TaskZuResource()
  {
  }

  public TaskZuResource( Task taskFk, Resource resourceFk, int resourcenArt )
  {
    this.taskFk = taskFk;
    this.resourceFk = resourceFk;

    pk = new TaskZuResourceId( taskFk.getId(), resourceFk.getId(), resourcenArt );
  }


  public TaskZuResourceId getPk()
  {
    return pk;
  }

  public void setPk( TaskZuResourceId pk )
  {
    this.pk = pk;
  }


  public Task getTaskFk()
  {
    return taskFk;
  }

  public void setTaskFk( Task taskFk )
  {
    this.taskFk = taskFk;
  }

  public Resource getResourceFk()
  {
    return resourceFk;
  }

  public void setResourceFk( Resource resourceFk )
  {
    this.resourceFk = resourceFk;
  }


  @Transient
  public int getResourcenType()
  {
    return getPk().getResourcenType();
  }


  public void setResourcenType( int resourcenArt )
  {
    getPk().setResourcenType( resourcenArt );
  }

  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((pk == null) ? 0 : pk.hashCode());
    result = prime * result + ((resourceFk == null) ? 0 : resourceFk.hashCode());
    result = prime * result + ((taskFk == null) ? 0 : taskFk.hashCode());
    return result;
  }

  @Override
  public boolean equals( Object obj )
  {
    if ( this == obj )
      return true;
    if ( obj == null )
      return false;
    if ( getClass() != obj.getClass() )
      return false;
    TaskZuResource other = (TaskZuResource) obj;
    if ( pk == null )
    {
      if ( other.pk != null )
        return false;
    }
    else
      if ( !pk.equals( other.pk ) )
        return false;
    if ( resourceFk == null )
    {
      if ( other.resourceFk != null )
        return false;
    }
    else
      if ( !resourceFk.equals( other.resourceFk ) )
        return false;
    if ( taskFk == null )
    {
      if ( other.taskFk != null )
        return false;
    }
    else
      if ( !taskFk.equals( other.taskFk ) )
        return false;
    return true;
  }

}

@Embeddable
public class TaskZuResourceId implements Serializable
{
  private int taskFk;
  private int resourceFk;

  @Column( name = "resourcen_type" )
  private int resourcenType;

  public TaskZuResourceId()
  {
  }

  public TaskZuResourceId( int taskFk, int resourceFk, int resourcenType )
  {
    this.taskFk = taskFk;
    this.resourceFk = resourceFk;
    this.resourcenType = resourcenType;
  }

  public int getTaskFk()
  {
    return taskFk;
  }

  public void setTaskFk( int taskFk )
  {
    this.taskFk = taskFk;
  }

  public int getResourceFk()
  {
    return resourceFk;
  }

  public void setResourceFk( int resourceFk )
  {
    this.resourceFk = resourceFk;
  }

  public int getResourcenType()
  {
    return resourcenType;
  }

  public void setResourcenType( int resourcenType )
  {
    this.resourcenType = resourcenType;
  }

  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + resourceFk;
    result = prime * result + resourcenType;
    result = prime * result + taskFk;
    return result;
  }

  @Override
  public boolean equals( Object obj )
  {
    if ( this == obj )
      return true;
    if ( obj == null )
      return false;
    if ( getClass() != obj.getClass() )
      return false;
    TaskZuResourceId other = (TaskZuResourceId) obj;
    if ( resourceFk != other.resourceFk )
      return false;
    if ( resourcenType != other.resourcenType )
      return false;
    if ( taskFk != other.taskFk )
      return false;
    return true;
  }
}

Thx

  • 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-28T06:20:20+00:00Added an answer on May 28, 2026 at 6:20 am

    It sounds like you still have references to a deleted entity. For example, you delete a Task object, but there is still a TaskResource object that references the deleted task.

    If this is the problem, then after you delete the Task you can try something like taskResource.setTaskFk(null) to remove the dangling reference.

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

Sidebar

Related Questions

Got a problem with a query I'm trying to write. I have a table
I got problem with core data, I had a table that exactly look like
I have got a problem with two tables: CREATE TABLE IF NOT EXISTS `addresses`
Here's the problem I'm having, I've got a set of logs that can grow
I've got a problem with the removal of entities in my JPA application: basically,
I've got a problem here with a join and pagination. I have 3 Models:
I'm dealing with an annoying problem in core data I've got a table named
Hia, I got a somewhat odd problem. Im using two managed Classes that relate
I have got a List<Problem> which I want to export to a column based
I have the following problem using a has_many :through many-to-many relation in a multi-select

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.