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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:59:46+00:00 2026-06-10T17:59:46+00:00

I have problems setting existing but detached entities into a ManyToOne relation, Hibernate throws

  • 0

I have problems setting existing but detached entities into a ManyToOne relation, Hibernate throws a PersistentObjectException saying: ‘detached entity passed to persist: model.persons.Customer’.

My use case is as follows:

I open a JSF2 view which lets me create an order through a Named bean and a call to a stateless OrderDAO EJB. On that view i can select a customer from a drop down list. The drop down list is populated using another stateless CustomerDAO EJB which fetches a list of customers from the database. I select the customer and the customer is set to the order. When i save the order using the stateless OrderDAO EJB the above mentioned exception is thrown.

My entities looks as follows:

@Entity
public class Order extends AbstractEntity implements Serializable {

    private static final long serialVersionUID = -8061887078955032972L;

    @ManyToOne(cascade={CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST}, optional = false)
    private Customer customer = null;

    ... 
}

@Entity
@DiscriminatorValue("customer")
public class Customer extends Person implements Serializable {

    private static final long serialVersionUID = 2788107135840578322L;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
    private List<Order> orders = null;

    ...
}

And the code involved looks as follows:

@Named
@ConversationScoped
public class OrderController implements Serializable {

    private static final long serialVersionUID = -4868506512979135651L;

    @EJB
    private OrderEJB orderBean;
    private Order order;
    ...
    public Order getOrder() {
        if (order == null) {
            if (id == null) {
                order = orderBean.create();
            } else {
                order = orderBean.findById(id);
            }
        }
        return order;
    }

    public String saveOrder() {
        order = orderBean.save(order);
        return "savedOrder";
    }
}

@Stateless
public class OrderEJB extends GenericDAO<Order> {
}

public class GenericDAO<T extends AbstractEntity> {

    public T create() {
        try {
            return getClassType().newInstance();
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Error creating new instance of "+getClassType(), e);
        }
        return null;
    }

    public T save(T entity) {
        if (entity.getId() == null) {
            saveNew(entity);
        } else {
            entity = update(entity);
        }
        return entity;
    }

    private void saveNew(T entity) {
        em.persist(entity);
    }

    private T update(T entity) {
        return em.merge(entity);
    }

}

Is the fact that i use two different EJBs, one for the order and one for the customer a problem?

  • 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-10T17:59:47+00:00Added an answer on June 10, 2026 at 5:59 pm

    The problem is that you save (persist) a new entity with a reference to a detached entity with cascade enabled for persisting (via cascade all) on that relation.

    This is an unsupported combination.

    JPA wants the entire chain in this relation to be new objects. If one already has a persistent identity (attached or detached), it won’t work.

    There are two solutions:

    Remove the cascade on the relation from Customer to Order. Customer will still get a reference to the existing Order, but JPA will not attempt to persist the Order itself. You’ll loose the ability to persist new Customers with new Orders in one go, but if this never happens, then this could be a good solution.

    The second solution is a bit counter intuitive, but does work; call em.merge() instead of em.persist() for the new Customer with existing Order.

    Merge actually has “saveOrUpdate” semantics, and thus does a save without caring if the object is new or not.

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

Sidebar

Related Questions

I have problems setting the focus on an input element that has been created
Hallo. I have problems by setting the Eclipse envirorment, can anybody provide me a
i have problems with a really basic thing. setting properties after instantiation a UIViewController-
I have had many problems in Django (not all of them solved), but this
I have experience with setting up multiple Perl program with mac before, but come
I have problem with setting proper charset on my jsf pages. I use MySql
I have a problem with setting the HTTPBody of a request correctly. I'm using
I have a problem with setting Browsing Path in Delphi 2009: When I install
I have this weird problem with setting up cookies with PHP. Everything worked fine
i have problem to correctly bind data to WPF Chart. When i'm setting ItemsSource

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.