【问题标题】:How to define HSQ DB properties in Spring JPA using annoations如何使用注解在 Spring JPA 中定义 HSQLDB 属性
【发布时间】:2014-10-07 12:34:33
【问题描述】:

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

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

}

服务方式: @服务

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 的连接。

【问题讨论】:

    标签: spring jpa annotations hsqldb


    【解决方案1】:

    我假设你不使用 Spring boot:

    您需要@Configuration 类-(它基本上是在java 中配置spring 应用程序的新方法)和@EnableJpaRepositories,它将它变成spring data jpa/spring data rest 为您服务。您还必须指定实体管理器、事务管理器和数据源 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;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-18
      • 2019-12-19
      • 1970-01-01
      • 2013-01-15
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2011-09-19
      • 2012-08-06
      相关资源
      最近更新 更多