Reputation: 53
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
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
Reputation: 24561
From your question, I suspect there are few misunderstandings of Spring + Java persistence ecosystem:
So to answer your question you have two options:
Upvotes: 4
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
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