简体   繁体   中英

spring + testng + hibernate right approch for session factory

I am building a spring 4 + Hibernate5 application. I wonder whether is there any difference in defining the data base connection properties like url ,username etc via DataSource object and via hibernate properties like "hibernate.connection.url", "hibernate.connection.username" etc. Offcourse ultimately the datasource object will be tied to the session factory. Just want to make sure to do the things in right way.

I want to define a separate datesource object via dataSource property, so that I can use the AbstractTransactionalTestNGSpringContextTests for test cases. This classs is always expecting a data source object. I want to use the @Rollback feature and this feature is working with AbstractTransactionalTestNGSpringContextTests. AbstractTestNGSpringContextTests is not supporting the roll back feature but still persisting working perfectly.

Need inputs to implements in the right way.

Adding example code to provide more information.

@ContextConfiguration(locations = { "classpath:spring/fpda_persistence_config.xml" })
@Rollback
@Transactional
public class BankTransactionDAOTest extends AbstractTestNGSpringContextTests {


    @Autowired
    private BankTransactionDAO bankTransactionDao;

    @Test
    public void createBankTransactionTest(){

        BankTransaction bt = new BankTransaction();
        bt.setAuthoritativeTableId(new BigDecimal(1234));
        bt.setBankTransactionTypeCode(new Character('C'));
        bt.setInstanceId(new BigDecimal(1234));
        bt.setRowCreatedEpochTime(new BigDecimal(1234));
        bt.setTransactionId(new BigDecimal(1234));
        bt.setTransactionKey(new BigDecimal(System.currentTimeMillis()));
        bankTransactionDao.createBankTransaction(bt);
    }
}

here to make transaction roll back happen sucessfully, I came to know that we should extend AbstractTransactionalTestNGSpringContextTests instead of AbstractTestNGSpringContextTests. Then I should have declared the datasource property instead of defining all properties in hibernate properties. so overall is it a right approach to declare some properties in data source and some properties in hibernate?. will it make any difference.

Thanks in Advance.

在我们的项目中,我们对测试类@TestPropertySource(locations = "classpath:application-test.properties")使用类级别的注释@TestPropertySource(locations = "classpath:application-test.properties")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM