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.Productthe persistence manager is injected by springCode:@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) ; }
Below is an example of what I am doing with the above code.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>
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.Code:Service serviceBean = new ServiceBean("120"); Product product = new Product(10); product.setService(serviceBean); serviceBean.getProducts().add(product); serviceDao.create(serviceBean);
What could be the problem here? Let me know if I need to provide anything else.


Reply With Quote