我在Spring 4+上使用neo4j-jdbc-2.3.2。对于CRUD,我正在使用JDBCTemplate。
我正面临两个挑战
这是一个小测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Neo4jJdbcSpike.Config.class })
@Transactional
@Rollback(false)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class Neo4jJdbcSpike {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void transactionManagementNotWorkingTest() {
String cypher = "create (a:Person { name: ?})";
jdbcTemplate.update(cypher, new Object[] { "Foo" });
throw new IllegalStateException("This will not rollback trnsaction");
}
@Test
public void transactionManagementNotWorkingCheckedExceptioTest() throws CheckedException {
String cypher = "create (a:Person { name: ?})";
jdbcTemplate.update(cypher, new Object[] { "Bar" });
throw new CheckedException();
}
@Test
public void getGeneratedGraphIDTest() throws CheckedException {
String cypher = "create (a:Person { name: ?})";
KeyHolder holder = new GeneratedKeyHolder();
PreparedStatementCreator psc = new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = con.prepareStatement(cypher, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, "Bar");
return ps;
}
};
jdbcTemplate.update(psc, holder);
Assert.assertNotNull(holder.getKey());
Assert.assertTrue(holder.getKey().longValue() > 0L);
}
public static class CheckedException extends Exception {
}
@Configuration
@Import(TestConfig.class)
@EnableTransactionManagement
static class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource("jdbc:neo4j://localhost:7474", "neo4j", "passw0rd");
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public DataSourceTransactionManager txManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
}
注意:所有测试用例都失败了...... 在此先感谢....