I'm seeing this when I run my test. I'm confident that the database connection is being established (or attempted) as it initially gave an error when I didn't include the password, then it complained because the database of that name didn't exist. Once I created the database, it then gave me this error:
$ mvn clean test
...
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.317 sec <<< FAILURE!
testGetAccounts(com.oreilly.repositories.JpaAccountRepositoryTest) Time elapsed: 0.104 sec <<< ERROR!
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
at org.springframework.test.context.transaction.TransactionContext.startTransaction(TransactionContext.java:98)
...
Below is my config file:
@Configuration
@ComponentScan(basePackages = "com.oreilly")
@PropertySource("classpath:prod.properties")
@EnableTransactionManagement
public class AppConfig {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("db.driver"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.user"));
dataSource.setPassword(env.getProperty("db.pass"));
return dataSource;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(true);
adapter.setGenerateDdl(true);
adapter.setDatabase(Database.MYSQL);
return adapter;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
Properties props = new Properties();
props.setProperty("hibernate.format_sql", String.valueOf(true));
LocalContainerEntityManagerFactoryBean emf =
new LocalContainerEntityManagerFactoryBean();
emf.setDataSource(dataSource);
emf.setPackagesToScan("com.oreilly.entities");
emf.setJpaVendorAdapter(jpaVendorAdapter);
emf.setJpaProperties(props);
return emf;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
@Bean
public BeanPostProcessor persistenceTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
Happy to include more code if it helps. I can successfully log into my mysql console with the same credentials, and the database is there:
$ mysql -uroot -p spring
Enter password:
...
mysql> show databases;
+-----------------------+
| Database |
+-----------------------+
| ... |
| spring |
+-----------------------+
10 rows in set (0.00 sec)
I'm following a safari books video course so pretty new to JPA. Pretty much just taken their code and still getting the error so quite difficult to know which part of the code I should be questioning.
UPDATE
Here is my test I'm running. I'm just running one test for now otherwise my console gets swamped with errors. I'll add the repository code below too:
JpaAccountRepositoryTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@Transactional
public class JpaAccountRepositoryTest {
@Autowired
private AccountRepository repository;
@Test
public void testGetAccounts() throws Exception {
List<Account> accounts = repository.getAccounts();
assertThat(accounts.size(), is(3));
}
// @Test
// public void testGetAccount() throws Exception {
// ...
JpaAccountRepository.java
@Repository
public class JpaAccountRepository implements AccountRepository {
private long nextId = 4;
@PersistenceContext
private EntityManager entityManager;
public List<Account> getAccounts() {
return entityManager.createQuery("select a from Account a", Account.class)
.getResultList();
}
public Account getAccount(Long id) {
...