Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

org.hibernate.PersistentObjectException: detached entity passed to persist H2 DB

$
0
0
i am using H2 in memory database for testing and my configuration is as follows:

1- SpringTestingConfig:

Code:

@Configuration
    @ComponentScan(basePackages = "com.myapp.data", excludeFilters = { @Filter(Configuration.class) })
    @PropertySource("classpath:/test.properties")
    @Profile("test")
    public class SpringTestingConfig {
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("org.h2.Driver");
          dataSource.setUrl("jdbc:h2:mem:test;MODE=Mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS MYAPP");
          dataSource.setUsername("sa");
          dataSource.setPassword("");
          return dataSource;
      }
    }

2- MyTestClass:

Code:

@RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners({ WebContextTestExecutionListener.class,
                    DependencyInjectionTestExecutionListener.class,
                    DirtiesContextTestExecutionListener.class,
                    TransactionalTestExecutionListener.class })
    @ActiveProfiles("test")
    @DirtiesContext
    @ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {
                    SpringConfig.class, SpringTestingConfig.class,
                    SpringLocalContainerJPAConfig.class, CustomConfiguration.class })
    @PrepareForTest({ FacesContext.class })
    @PowerMockIgnore({ "javax.management.*", "javax.xml.parsers.*",
                "com.sun.org.apache.xerces.internal.jaxp.*", "ch.qos.logback.*",
                "org.slf4j.*" })
    public class MyTestClass{

      @Test
            public void signup(){
       
          this.company = userService.saveCompany(company);
          this.user.setCompany(company);
          this.user = userService.saveUser(this.user); // gives exception
       
          }
   
    }

3- Save methods:

Code:

@Transactional(propagation = Propagation.REQUIRED)
            public User saveUser(User user) {
                    return userRepository.saveAndFlush(user); //JpaRepository
            }
   
    @Transactional(propagation = Propagation.REQUIRED)
            public Company saveCompany(Company company) {
                    return companyRepository.saveAndFlush(company); //JpaRepository
            }


ISSUE:the code in test method works very fine when running the application (local database), but when running it from test (in memory database) it saves company and when trying to save user it gives the exception:

Code:

org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.myapp.data.domain.Company; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.myapp.data.domain.Company

Viewing all articles
Browse latest Browse all 297

Trending Articles