Announcement Announcement Module
Collapse
No announcement yet.
Spring Data + Hibernate, how to update saved entity? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Data + Hibernate, how to update saved entity?

    Hello. I'm using Spring Data and Hibernate and get an error when I'm trying to save entity with set id:
    Code:
    org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.snp.cm.persistency.contact.Contact; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.snp.cm.persistency.contact.Contact
    	at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:668)
    	at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:104)
    	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:403)
    	at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
    	at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    Please help me how can I update an entity via JpaRepositories or what config do I miss?
    persistence.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    	version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    	<persistence-unit name="contactManagerPU">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    
    		<class>com.snp.cm.persistency.contact.Contact</class>
    		<class>com.snp.cm.persistency.contact.TelephoneNumber</class>
    
    		<exclude-unlisted-classes>false</exclude-unlisted-classes>
    		
    		<properties>
    			<property name="hibernate.max_fetch_depth" value="3" />
    		</properties>
    	</persistence-unit>
    </persistence>
    spring context:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:p="http://www.springframework.org/schema/p" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xmlns:util="http://www.springframework.org/schema/util"
    	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    	default-lazy-init="true">
    
    	<import resource="classpath*:META-INF/spring/dbs-repository-context.xml" />
    
    	<!-- Activates JPA's @PersistenceContext and @PersistenceUnit (if available) 
    		annotations to be detected in bean classes. -->
    	<context:annotation-config />
    
    	<!-- Enable Transaction using @Transactional annotation -->
    	<tx:annotation-driven />
    
    	<bean id="daTM"
    		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    		p:dataSource-ref="dataSource" />
    
    	<!-- Create local transaction manager -->
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    		p:entityManagerFactory-ref="entityManagerFactory" lazy-init="true"
    		p:dataSource-ref="dataSource" />
    
    	<!-- Create EntityManagerFactory for injection into services. -->
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    		p:dataSource-ref="dataSource" p:persistenceXmlLocation-ref="persistenceXmlLocation">
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    			</bean>
    		</property>
    	</bean>
    
    	<!-- Database access configuration -->
    	<bean id="persistenceXmlLocation" class="java.lang.String">
    		<constructor-arg value="classpath*:META-INF/persistence.xml"></constructor-arg>
    	</bean>
    
    	<context:property-placeholder
    		location="classpath:META-INF/spring/jdbc.properties" />
    
    	<!-- Dev's env DataSource -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="${jdbc.driver}" />
    		<property name="url" value="${jdbc.url}" />
    		<property name="username" value="${jdbc.username}" />
    		<property name="password" value="${jdbc.password}" />
    	</bean>
    
    </beans>
    Failed integration unit test:
    Code:
    package com.snp.cm.dbs.repository.integration;
    
    import junit.framework.Assert;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.snp.cm.dbs.repository.ContactRepository;
    import com.snp.cm.dbs.repository.TelephoneNumberRepository;
    import com.snp.cm.persistency.contact.Contact;
    import com.snp.cm.persistency.contact.TelephoneNumber;
    import com.snp.cm.persistency.contact.TelephoneType;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath*:META-INF/spring/dbs-application-context.xml" })
    //@Transactional
    public class ContactRepositoryTest {
    	
    	@Autowired
    	private ContactRepository contactRepository;
    	@Autowired
    	private TelephoneNumberRepository telephoneNumberRepository;
    	
    	@Test
    	public void testRepositorySaveMethod() {
    		Contact contact = new Contact();
    		contact.setLastName("Carter");
    		contact.setName("John");
    		contact.setNickName(null);
    		contact = this.contactRepository.saveAndFlush(contact);
    		Assert.assertNotNull(contact.getContactId());
    		
    		TelephoneNumber telephoneNumber1 = new TelephoneNumber();
    		telephoneNumber1.setTelephoneNumber("*****");
    		telephoneNumber1.setContact(contact);
    		telephoneNumber1.setTelephoneType(TelephoneType.HOME);
    		this.telephoneNumberRepository.save(telephoneNumber1);
    		
    		TelephoneNumber telephoneNumber2 = new TelephoneNumber();
    		telephoneNumber2.setTelephoneNumber("*****");
    		telephoneNumber2.setContact(contact);
    		telephoneNumber2.setTelephoneType(TelephoneType.MOBILE);
    		this.telephoneNumberRepository.save(telephoneNumber2);
    		
    		contact = this.contactRepository.findOne(contact.getContactId());
    		
    		Assert.assertEquals(2, contact.getTelephoneNumbers().size());
    	}
    }
    failes after persisting at this.telephoneNumberRepository.save(telephoneNumbe r1);

  • #2
    Can some one help me? I used OpenJpa in another project and didn't have such issue: 'save' methode did create and update. Should I implement my own update methode?

    Comment

    Working...
    X