BAEL-5431 BootstrapMode for JPA Repositories
This commit is contained in:
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.boot.bootstrapmode;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
AsyncTaskExecutor delayedTaskExecutor() {
|
||||
return new ThreadPoolTaskExecutor() {
|
||||
@Override
|
||||
public <T> Future<T> submit(Callable<T> task) {
|
||||
return super.submit(() -> {
|
||||
Thread.sleep(5000);
|
||||
return task.call();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, AsyncTaskExecutor delayedTaskExecutor) {
|
||||
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
|
||||
factory.setPackagesToScan("com.baeldung.boot.bootstrapmode");
|
||||
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
factory.setDataSource(dataSource);
|
||||
factory.setBootstrapExecutor(delayedTaskExecutor);
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put("hibernate.hbm2ddl.auto", "create-drop");
|
||||
factory.setJpaPropertyMap(properties);
|
||||
return factory;
|
||||
}
|
||||
}
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.boot.bootstrapmode.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Todo {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
private String label;
|
||||
|
||||
public Todo() {
|
||||
}
|
||||
|
||||
public Todo(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.boot.bootstrapmode.repository;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.baeldung.boot.bootstrapmode.domain.Todo;
|
||||
|
||||
public interface TodoRepository extends CrudRepository<Todo, Long> {
|
||||
}
|
||||
Reference in New Issue
Block a user