如何使用注释在Spring JPA中定义HSQLDB属性

时间:2014-10-07 12:34:33

标签: spring jpa annotations hsqldb

我使用运行管理器Swing运行HSQL DB作为Im内存。我必须使用注释从Spring JPA存储库连接HSQLDB服务器。 我的存储库类。

@RepositoryRestResource
public interface Vehicle extends JpaRepository<Vehicle , BigInteger>{
    public List<Vehicle > findAll(Sort sort);

}

服务方式: @Service

public class LocationService {

        @Autowired
       VehicletRepository vehicleRepository = null;

        /**
         * This method is to get all the locations from the repository
         */
        public List<Vehicle> getVehicless() {
            Order order = new Order(Direction.ASC,"vehicleCode");
            Sort sort = new Sort(order);
            List<Airport> airports = vehicletRepository .findAll(sort);
            System.out.println("inside service");

            return vehicles;
        }     

    }

任何人都可以使用注释帮助实现Spring JPA与HSQL DB的连接。

1 个答案:

答案 0 :(得分:0)

我假设您不使用Spring启动:

你需要@Configuration类 - (它基本上是在java中配置spring应用程序的新方法)和@EnableJpaRepositories,它会为你提供spring数据jpa / spring数据。您还必须指定实体管理器,事务管理器和数据源bean。示例如下:

@Configuration
@EnableJpaRepositories("your.package.with.repositories")
public class DBConfig{
 @Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();

    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

    return transactionManager;
}

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    DBConfigurationCommon.configureDB(dataSource, your_jdbc_url_here, db_username_here, db_password_here);
    return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

    //you may need to define new Properties(); here with hibernate dialect and add it to entity manager factory
    entityManagerFactoryBean.setPackagesToScan("your_package_with_domain_classes_here");

    return entityManagerFactoryBean;
}
}