Magnetotail
Magnetotail

Reputation: 53

Use Spring Data JPA without Spring Boot application


I wanted to know if it is possible to use the Spring Data JPA without the rest of the spring framework? I used Spring Data JPA in a Spring Boot web application for another project and really liked how easy it was to use.

Now I have a little project with some friends for a Desktop application without a server and would really like to use Spring Data JPA but I have not found information anywhere on wether it is possible to use it without Beans or the rest of the Spring framework.

Is this possible or should I try using another JPA?

Upvotes: 5

Views: 6062

Answers (4)

Rohim Chou
Rohim Chou

Reputation: 1269

Here's a simple example:

.
├── src/main/java/com/demo/
│   ├── Main.java
│   ├── AppContextConfig.java
│   ├── UserEnitity.java
│   └── UserRepository.java
└── pom.xml

public static void main(String[] args) {
    // set up dependency injection container
    AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
    appContext.register(AppContextConfig.class);
    appContext.refresh();

    // get an implemented UserRepository instance from spring data
    UserRepository userRepo = appContext.getBean(UserRepository.class);
    UserEnitity user = userRepo.findById("user001").get();
    System.out.println(user.getName());
}

dependencies:

<dependencies>
  <!-- for di container -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.29</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.7.15</version>
  </dependency>
  <dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>23.2.0.0</version>
  </dependency>
</dependencies>
public interface UserRepository extends JpaRepository<UserEnitity, String> {
}
@Configuration
@EnableJpaRepositories
public class AppContextConfig {
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(oracle.jdbc.driver.OracleDriver.class.getName());
        dataSource.setUrl("jdbc:oracle:thin:@//demo.com:1521/orcl");
        dataSource.setUsername("user");
        dataSource.setPassword("user123");
        return dataSource;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(true);
        vendorAdapter.setDatabasePlatform(Oracle12cDialect.class.getName()); // org.hibernate.dialect.Oracle12cDialect

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.demo");
        factory.setDataSource(dataSource());
        Map<String, Object> jpaProperties = new HashMap<>();
        jpaProperties.put("hibernate.physical_naming_strategy", new SpringPhysicalNamingStrategy()); // tableName -> table_name
        jpaProperties.put("hibernate.implicit_naming_strategy", new SpringImplicitNamingStrategy());
        factory.setJpaPropertyMap(jpaProperties);
        factory.afterPropertiesSet();

        return factory.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }
}

reference: spring-data-jpa repository annotation based config

Upvotes: 1

luboskrnac
luboskrnac

Reputation: 24561

From your question, I suspect there are few misunderstandings of Spring + Java persistence ecosystem:

  • You can use Spring Data JPA without Spring Boot (you need to use Spring Core because it is Spring Data JPA dependency), but it doesn't make much sense to me as Spring Boot gives you a lot of convenient magic and syntactic sugar.
  • Spring + Spring Boot ecosystem are primarily tailored towards web applications, but can be used for non-web facing applications as well. If you are writing desktop application, chances are you will be using some of the desktop platforms (e.g. Eclipse RCP). These often doesn't work well with Spring's dependency injection. You can still use other isolated Spring + Spring Boot features, but you are missing important Spring + Spring Boot backbone -> IoC container.
  • Spring Data JPA is not JPA provider. It is just convenience layer on top of JPA standard that makes it easy to plug JPA ORM into Spring applications. It also uses similar patterns from Spring Data projects family to make various persistence stores easy to use for Spring developers.
  • If you are looking to use JPA (JEE standard APIs that are separate from Spring ecosystem), you can definitely use some ORM (e.g. Hibernate, Toplink) with JPA (javax.persistence) APIs without Spring altogether.

So to answer your question you have two options:

  1. If you are using some kind of Java desktop framework (e.g. Eclipse RCP or something OSGi based), do not use Spring at all. You probably want to use Toplink (which comes from Eclipse community) or Hibernate with JPA APIs.
  2. If you are doing desktop application for which make sense to take advantage of Spring IoC, definitely use Spring + Spring Boot + Spring Data JPA combo without web features.

Upvotes: 4

jkerak
jkerak

Reputation: 317

Apache DeltaSpike Data module provides similar functionality to Spring Data JPA (extend Repository interface, query by convention), without requiring Spring. You just have to provide CDI and JPA implementations (for example, Weld and Hibernate would work)

Upvotes: 0

Simon Martinelli
Simon Martinelli

Reputation: 36163

Spring Data relays on Spring Framework. So it's not possible to use it without the Spring Framework.

But you can use Hibernate as it is in a standalone application.

Upvotes: 1

Related Questions