-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate + JPA + Spring: detached entity passed to persist
PostPosted: Thu Oct 29, 2009 11:56 am 
Newbie

Joined: Tue Jan 11, 2005 12:58 pm
Posts: 15
I am using spring, hibernate and JPA for my data access layer.

I am getting the following exception when try to persist the classes below.

Code:
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com. service.sqlmap.models.Product; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.service.sqlmap.models.Product


Code:
@Entity
@Table(name = "service")
@NamedQueries({@NamedQuery(name = "Service.findAll", query = "SELECT s FROM Service s")})
public class Service implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "service_code")
    private String serviceCode;
   
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "service", fetch = FetchType.LAZY)
    private Set<Product> products = new HashSet<Product>();

...getters and setters
}

@Entity
@Table(name = "product")
@NamedQueries({@NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p")})
public class Product implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "product_id")
    private Long productId;
   
    @JoinColumn(name = "service_code", referencedColumnName = "service_code", insertable = true, updatable = true)
    @ManyToOne(optional = false)
    private Service service;

...getters and setters
}

public class ServiceDao extends BaseModel {
    protected PersistenceManager persistenceManager;

    public void create(Service serviceBean) {
        persistenceManager.create(serviceBean);
    }

    public void setPersistenceManager(PersistenceManager persistenceManager) {
        this.persistenceManager = persistenceManager;
    }

    public PersistenceManager getPersistenceManager() {
        return persistenceManager;
    }
   
}

public class PersistenceManagerImpl extends JpaDaoSupport implements PersistenceManager {

    public void create(BaseModel entity) {
        getJpaTemplate().persist(entity);
    }
}

@Transactional(readOnly = true)
public interface PersistenceManager {
    @Transactional(readOnly = false, propagation=Propagation.REQUIRES_NEW, rollbackFor=SQLException.class)
    public void create(BaseModel entity) ;
}


the persistence manager is injected by spring
Code:
<bean id="persistenceManagerTarget" class="com.service.dal.PersistenceManagerImpl">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="serviceDao" class="com.service.sqlmap.dao.ServiceDao">
        <property name="persistenceManager" ref="persistenceManagerTarget"/>
</bean>


Below is an example of what I am doing with the above code.
Code:
Service serviceBean = new ServiceBean("120");
Product product = new Product(10);

product.setService(serviceBean);
serviceBean.getProducts().add(product);
serviceDao.create(serviceBean);


At first I thought it was the lazy loading but I change to eager and it was the same problem. Ideally I want it to be lazy because it loads a lot of objects into memory and I do not want all of it in there at once.

What could be the problem here? Let me know if I need to provide anything else.


Top
 Profile  
 
 Post subject: Re: Hibernate + JPA + Spring: detached entity passed to persist
PostPosted: Fri Nov 13, 2009 6:19 pm 
Beginner
Beginner

Joined: Fri Nov 13, 2009 4:05 pm
Posts: 30
Did you ever get resolution on this? I am searching examples of using Hibernate/Spring together embedded in Struts. So wasn't sure if you had a resolution I could learn from.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.