title: Spring Boot Test: Unknown Entity when using hibernate
body: <p>Following along on a theme of questions related to Spring Boot. I am trying to set up a an integration test with the following:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {TestApplicationConfig.class,TestPersistenceConfig.class,MvcConfig.class,SecurityConfig.class},loader=AnnotationConfigWebContextLoader.class)
  @WebAppConfiguration
  public class ApplicationIntegrationTest {
  
      MockMvc mockMvc;
  
      @Autowired
      private WebApplicationContext wac;
  
      //@Resource(name="springSecurityFilterChain")
      @Autowired
      private FilterChainProxy springSecurityFilterChain;
  
      @Autowired
      private UserDao userDao;
  
      @Autowired
      private ClientDao clientDao;
  
      @Autowired
      private RoleDao roleDao;
  
      @Before
      public void setup() {
  
          // using the web application to initate the mock
          mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilter(springSecurityFilterChain).build();
  
          // here we should build up the data structure using hibernate
          List&lt;Client&gt; clients = new ArrayList&lt;Client&gt;();
  
          Client clientEN = new Client();
          clientEN.setDeviceId("444444444");
          clientEN.setLanguage("en-EN");
          clientEN.setAgentId("444444444|68:5b:35:8a:7c:d0");
          Client clientENDomain = clientDao.save(clientEN);
          clients.add(clientENDomain);
  
          List&lt;Role&gt; roles = new ArrayList&lt;Role&gt;();
          Role roleUser = new Role();
          roleUser.setRole("user");
          Role roleUserDomain = roleDao.save(roleUser);
          roles.add(roleUserDomain);
  
          Role roleAdmin = new Role();
          roleAdmin.setRole("admin");
          Role roleAdminDomain = roleDao.save(roleAdmin);
          roles.add(roleAdminDomain);
  
          User user = new User();
          user.setLogin("user");
          user.setPassword("password");
          user.setClients(clients);
          user.setRoles(roles);
  
          userDao.save(user);
  
      }
  
      @Test
      public void thatViewBootstrapUsesHttpNotFound() throws Exception {
  
          // testing that a correct login into the form will result in a cookie being set
          MvcResult result = mockMvc.perform(post("/login")
                  .param("username", "user").param("password", "password")).andReturn();
          Cookie c = result.getResponse().getCookie("my-cookie");
  
          Cookie[] cookies = result.getResponse().getCookies();
          for (int i = 0; i &lt;= cookies.length; i++) {
              System.out.println("cookie " + i + " name: " + cookies[i].getName());
              System.out.println("cookie " + i + " value: " + cookies[i].getValue());
          }
          //assertThat(c.getValue().length(), greaterThan(10));
  
          // No cookie; 401 Unauthorized
          mockMvc.perform(get("/")).andExpect(status().isUnauthorized());
  
          // With cookie; 200 OK
          mockMvc.perform(get("/").cookie(c)).andExpect(status().isOk());
  
          // Logout, and ensure we're told to wipe the cookie
          result = mockMvc.perform(delete("/session")).andReturn();
          c = result.getResponse().getCookie("my-cookie");
          assertThat(c.getValue().length(), is(0));
      }
  }
  </code></pre>
  
  <p>The test application config is as follows:</p>
  
  <pre><code>@Configuration
  @EnableAutoConfiguration
  @ComponentScan(basePackages = {"com.touchcorp.touchpoint"})
  public class TestApplicationConfig {
  }
  </code></pre>
  
  <p>and the TestPersistenceConfig is as follows:</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories
  @ComponentScan(basePackages = {"com.touchcorp.touchpoint.model"})
  public class TestPersistenceConfig {
  
      @Bean
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory( emf );
          return transactionManager;
      }
  
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
          emf.setDataSource( hsqlDataSource() );
          emf.setPackagesToScan(new String[] { "com.touchcorp.touchppoint.model" });
  
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          emf.setJpaVendorAdapter(vendorAdapter);
          emf.setJpaProperties( buildHibernateProperties() );
  
         return emf;
      }
  
      @Bean
      public DataSource hsqlDataSource()
      {
          return new EmbeddedDatabaseBuilder()
              .setType(EmbeddedDatabaseType.HSQL)
              .addScript("classpath:schema.sql")
              .build();
  
      }
  
      protected Properties buildHibernateProperties()
      {
          Properties hibernateProperties = new Properties();
  
          hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
          hibernateProperties.setProperty("hibernate.show_sql", "true");
          hibernateProperties.setProperty("hibernate.use_sql_comments", "true");
          hibernateProperties.setProperty("hibernate.format_sql", "true");
          hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
  
          hibernateProperties.setProperty("hibernate.generate_statistics", "false");
  
          return hibernateProperties;
      }
  }
  </code></pre>
  
  <p>Here is the Client model:</p>
  
  <pre><code>@Entity
  @Table(name="client")
  public class Client {
      @Id
      @GeneratedValue
      private Long id;
  
      @Column(name="deviceId")
      private String deviceId;
  
      @Column(name = "language")
      private String language;
  
      @Column(name = "agentId")
      private String agentId;
  
      @Column(name = "token")
      private String token;
  
      @Temporal(TemporalType.DATE)
      @Column(name = "expiry")
      private Date expiry;
  
      @ManyToMany(cascade=CascadeType.ALL)
      @JoinTable(name="client_role",
          joinColumns = {@JoinColumn(name="client_id", referencedColumnName="id")},
          inverseJoinColumns = {@JoinColumn(name="role_id", referencedColumnName="id")}
      )
      private List&lt;Role&gt; roles;
  
      @ManyToMany(cascade= CascadeType.ALL, mappedBy = "clients", fetch = FetchType.LAZY)
      private Set&lt;User&gt; users;
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getDeviceId() {
          return deviceId;
      }
  
      public void setDeviceId(String deviceId) {
          this.deviceId = deviceId;
      }
  
      public String getLanguage() {
          return language;
      }
  
      public void setLanguage(String language) {
          this.language = language;
      }
  
      public String getAgentId() {
          return agentId;
      }
  
      public void setAgentId(String agentId) {
          this.agentId = agentId;
      }
  
      public String getToken() {
          return token;
      }
  
      public void setToken(String token) {
          this.token = token;
      }
  
      public Date getExpiry() {
          return expiry;
      }
  
      public void setExpiry(Date expiry) {
          this.expiry = expiry;
      }
  
      public List&lt;Role&gt; getRoles() {
          return roles;
      }
  
      public void setRoles(List&lt;Role&gt; roles) {
          this.roles = roles;
      }
  
      public Set&lt;User&gt; getUsers() {
          return users;
      }
  
      public void setUsers(Set&lt;User&gt; users) {
          this.users = users;
      }
  }
  </code></pre>
  
  <p>and here's the error:</p>
  
  <pre><code>org.springframework.dao.InvalidDataAccessApiUsageException: Unknown entity: com.touchcorp.touchpoint.model.domain.Client; nested exception is java.lang.IllegalArgumentException: Unknown entity: com.touchcorp.touchpoint.model.domain.Client
      at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:157)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
      at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
      at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy59.save(Unknown Source)
      at com.touchcorp.touchpoint.application.ApplicationIntegrationTest.setup(ApplicationIntegrationTest.java:89)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  Caused by: java.lang.IllegalArgumentException: Unknown entity: com.touchcorp.touchpoint.model.domain.Client
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1184)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
      at com.sun.proxy.$Proxy56.persist(Unknown Source)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:262)
      at com.sun.proxy.$Proxy56.persist(Unknown Source)
      at com.touchcorp.touchpoint.model.dao.ClientDaoImpl.save(ClientDaoImpl.java:57)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
      ... 35 more
  </code></pre>
  
  <p>I have spent a lot of time trawling around to find the right combination of annotations to solve the general application booting. It appears that everything on the classpath is being picked up now, but it seems that spring boot is not somehow making the @Entites available to hibernate/JPA.</p>
  
  <p>can anyone help?</p>