maybe someone can help me with a little problem, so my application is starting, working perfectly etc. But, without aspect annotations (#After) declared to methods in service. I tried declaration thru annotation #HelloData via #annotation and path, also file path, and result is the same.
...
Benchmark.java
#Aspect
#Component
public class Benchmark {
Logger log = LoggerFactory.getLogger(Benchmark.class);
#Around("#annotation(HelloData)")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("DADADADA!");
long startTime = System.currentTimeMillis();
Object out = joinPoint.proceed();
long timeTaken = System.currentTimeMillis() - startTime;
logTimeTaken(joinPoint, timeTaken);
return out;
}
protected void logTimeTaken(ProceedingJoinPoint joinPoint, long timeTaken) {
System.out.println("DADADADA2!");
log.info("..............................................................Time Taken by " + joinPoint + ": " + timeTaken);
}
}
DataService.java [bottom]
#Service
public class DataService {
private String fileName = "src/main/resources/MOCK_DATA.csv";
Logger logger = LoggerFactory.getLogger(DataService.class);
DataRepo dataRepo;
DataSqlRepo dataSqlRepo;
List<Data> list;
List<DataSql> listTwo;
//
public DataService(final DataRepo dataRepo, final DataSqlRepo dataSqlRepo ) {
this.dataRepo = dataRepo;
this.dataSqlRepo = dataSqlRepo;
list = new ArrayList<>();
listTwo = new ArrayList<>();
mapFile(fileName);
}
//
public DataRepo getDataRepo() {
return dataRepo;
}
public void setDataRepo(final DataRepo dataRepo) {
this.dataRepo = dataRepo;
}
public DataSqlRepo getDataSqlRepo() {
return dataSqlRepo;
}
public void setDataSqlRepo(final DataSqlRepo dataSqlRepo) {
this.dataSqlRepo = dataSqlRepo;
}
public List<Data> getList() {
return list;
}
public void setList(final List<Data> list) {
this.list = list;
}
public List<DataSql> getListTwo() {
return listTwo;
}
public void setListTwo(final List<DataSql> listTwo) {
this.listTwo = listTwo;
}
//
public void mapFile(String fileName){
Map<String, String> mapping = new
HashMap<String, String>();
mapping.put("id", "id");
mapping.put("first_name", "firstName");
mapping.put("last_name", "lastName");
mapping.put("email", "email");
mapping.put("gender", "gender");
mapping.put("ip_address", "ipAddress");
HeaderColumnNameTranslateMappingStrategy<Data> strategy =
new HeaderColumnNameTranslateMappingStrategy<Data>();
strategy.setType(Data.class);
strategy.setColumnMapping(mapping);
CSVReader csvReader = null;
try {
csvReader = new CSVReader(new FileReader
(fileName));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
CsvToBean csvToBean = new CsvToBean();
list = csvToBean.parse(strategy, csvReader);
for (Data e : list) {
listTwo.add(new DataSql(Integer.parseInt(e.getId()),e.getFirstName(),e.getLastName(),e.getEmail(),e.getGender(),e.getIpAddress()));
}
saveToMongoDb(list);
saveToSqlDb(listTwo);
}
#HelloData
public void saveToMongoDb(List<Data> list) {
logger.info("////");
for (Data e : list) {
dataRepo.save(e);
}
logger.info("////");
}
//
#HelloData
public void saveToSqlDb(List<DataSql> listTwo) {
logger.info("//");
for (DataSql f : listTwo) {
dataSqlRepo.save(f);
}
logger.info("//");
}
}
Console:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)
2020-06-09 21:42:24.160 INFO 26000 --- [ main] c.e.csvtomongo.CsvToMongoApplication : Starting CsvToMongoApplication on MSI with PID 26000 (C:\Users\Vetom\OneDrive\Dokumenty\GitHub\csv-to-mongo\target\classes started by Vetom in C:\Users\Vetom\OneDrive\Dokumenty\GitHub\csv-to-mongo)
2020-06-09 21:42:24.163 INFO 26000 --- [ main] c.e.csvtomongo.CsvToMongoApplication : No active profile set, falling back to default profiles: default
2020-06-09 21:42:24.710 INFO 26000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-06-09 21:42:24.711 INFO 26000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-06-09 21:42:24.740 INFO 26000 --- [ main] .RepositoryConfigurationExtensionSupport : Spring Data JPA - Could not safely identify store assignment for repository candidate interface com.example.csvtomongo.model.DataRepo. If you want this repository to be a JPA repository, consider annotating your entities with one of these annotations: javax.persistence.Entity, javax.persistence.MappedSuperclass (preferred), or consider extending one of the following types with your repository: org.springframework.data.jpa.repository.JpaRepository.
2020-06-09 21:42:24.757 INFO 26000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 41ms. Found 1 JPA repository interfaces.
2020-06-09 21:42:24.772 INFO 26000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2020-06-09 21:42:24.772 INFO 26000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2020-06-09 21:42:24.776 INFO 26000 --- [ main] .RepositoryConfigurationExtensionSupport : Spring Data MongoDB - Could not safely identify store assignment for repository candidate interface com.example.csvtomongo.model.DataSqlRepo. If you want this repository to be a MongoDB repository, consider annotating your entities with one of these annotations: org.springframework.data.mongodb.core.mapping.Document (preferred), or consider extending one of the following types with your repository: org.springframework.data.mongodb.repository.MongoRepository.
2020-06-09 21:42:24.778 INFO 26000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5ms. Found 1 MongoDB repository interfaces.
2020-06-09 21:42:25.284 INFO 26000 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-06-09 21:42:25.291 INFO 26000 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-09 21:42:25.291 INFO 26000 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-06-09 21:42:25.375 INFO 26000 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-09 21:42:25.375 INFO 26000 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1179 ms
2020-06-09 21:42:25.553 INFO 26000 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-09 21:42:25.589 INFO 26000 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-06-09 21:42:25.621 INFO 26000 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.15.Final
2020-06-09 21:42:25.689 INFO 26000 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2020-06-09 21:42:25.733 INFO 26000 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:254}] to localhost:27017
2020-06-09 21:42:25.736 INFO 26000 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2071700}
2020-06-09 21:42:25.744 INFO 26000 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-06-09 21:42:25.892 INFO 26000 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-06-09 21:42:25.980 INFO 26000 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-06-09 21:42:25.990 INFO 26000 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
2020-06-09 21:42:26.465 INFO 26000 --- [ task-1] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-06-09 21:42:26.474 INFO 26000 --- [ task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-06-09 21:42:26.805 INFO 26000 --- [ main] c.e.csvtomongo.service.DataService : ////
2020-06-09 21:42:26.847 INFO 26000 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:255}] to localhost:27017
2020-06-09 21:42:27.334 INFO 26000 --- [ main] c.e.csvtomongo.service.DataService : ////
2020-06-09 21:42:27.335 INFO 26000 --- [ main] c.e.csvtomongo.service.DataService : //
2020-06-09 21:42:29.000 INFO 26000 --- [ main] c.e.csvtomongo.service.DataService : //
2020-06-09 21:42:29.049 WARN 26000 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-06-09 21:42:29.665 INFO 26000 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-09 21:42:29.665 INFO 26000 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-06-09 21:42:29.666 INFO 26000 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-06-09 21:42:29.671 INFO 26000 --- [ main] c.e.csvtomongo.CsvToMongoApplication : Started CsvToMongoApplication in 5.803 seconds (JVM running for 6.387)
Maybe someone do have any idea how to force this to work.
Related
I am trying to read data from multiple csv file present in resources/input/user_*.csv
and writing in one csv file present in resources/output/user.csv
Below is my Config File
#Configuration
#EnableBatchProcessing
public class BatchConfig {
private static final Logger log = LoggerFactory.getLogger(BatchConfig.class);
#Autowired
private StepBuilderFactory stepBuilderFactory;
#Autowired
private JobBuilderFactory jobBuilderFactory;
#Value(value = "classpath:input/user_*.csv")
private Resource[] resources;
#Bean
public FlatFileItemReader<User> flatFileItemReader(){
log.info("flatFileItemReader execution started");
FlatFileItemReader<User> reader = new FlatFileItemReader<>();
reader.setName("ReadMultipleCsv");
reader.setLineMapper(lineMapper());
log.info("flatFileItemReader execution completed");
return reader;
}
private LineMapper<User> lineMapper(){
DefaultLineMapper<User> lineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setDelimiter(",");
lineTokenizer.setStrict(false);
lineTokenizer.setNames(new String[] {"id","name","department","salary"});
BeanWrapperFieldSetMapper<User> fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(User.class);
lineMapper.setLineTokenizer(lineTokenizer);
lineMapper.setFieldSetMapper(fieldSetMapper);
return lineMapper;
}
#Bean
public MultiResourceItemReader<User> multiResourceItemReader(){
log.info("multiResourceItemReader() execution started");
MultiResourceItemReader<User> reader = new MultiResourceItemReader<>();
reader.setResources(resources);
reader.setDelegate(flatFileItemReader());
log.info("multiResourceItemReader() execution completed");
return reader;
}
#Bean
public FlatFileItemWriter<User> writer(){
log.info("writer() execution started");
FlatFileItemWriter<User> writer = new FlatFileItemWriter<>();
writer.setResource(new ClassPathResource("output/user.csv"));
writer.setAppendAllowed(true);
DelimitedLineAggregator<User> delimitedLineAggregator = new DelimitedLineAggregator<>();
delimitedLineAggregator.setDelimiter(",");
BeanWrapperFieldExtractor<User> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>();
beanWrapperFieldExtractor.setNames(new String[] {"id","name","department","salary"});
delimitedLineAggregator.setFieldExtractor(beanWrapperFieldExtractor);
writer.setLineAggregator(delimitedLineAggregator);
log.info("writer() execution completed");
return writer;
}
#Bean
public Step step(){
return stepBuilderFactory.get("get-student").<User, User>chunk(5)
.reader(multiResourceItemReader())
.writer(writer()).build();
}
#Bean
public Job job(){
return jobBuilderFactory.get("process-student").incrementer(new RunIdIncrementer())
.flow(step()).end().build();
}
}
And this is my user entity
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class User {
private String id;
private String name;
private String department;
private String salary;
}
logs =>
2022-08-22 16:40:06.295 INFO 17264 --- [ main] c.s.SpringBatchMultipleCsvToApplication : Starting SpringBatchMultipleCsvToApplication using Java 1.8.0_121 on GBMLVVCSW3823 with PID 17264 (C:\Users\NavghS\Downloads\spring-batch-multiple-csv-to\spring-batch-multiple-csv-to\target\classes started by NavghS in C:\Users\NavghS\Downloads\spring-batch-multiple-csv-to\spring-batch-multiple-csv-to)
2022-08-22 16:40:06.300 INFO 17264 --- [ main] c.s.SpringBatchMultipleCsvToApplication : No active profile set, falling back to 1 default profile: "default"
2022-08-22 16:40:08.999 INFO 17264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-08-22 16:40:09.017 INFO 17264 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-08-22 16:40:09.018 INFO 17264 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-08-22 16:40:09.349 INFO 17264 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-08-22 16:40:09.350 INFO 17264 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2936 ms
2022-08-22 16:40:09.542 INFO 17264 --- [ main] com.shrikant.config.BatchConfig : flatFileItemReader execution started
2022-08-22 16:40:09.554 INFO 17264 --- [ main] com.shrikant.config.BatchConfig : flatFileItemReader execution completed
2022-08-22 16:40:09.559 INFO 17264 --- [ main] com.shrikant.config.BatchConfig : multiResourceItemReader() execution started
2022-08-22 16:40:09.574 INFO 17264 --- [ main] com.shrikant.config.BatchConfig : multiResourceItemReader() execution completed
2022-08-22 16:40:09.576 INFO 17264 --- [ main] com.shrikant.config.BatchConfig : writer() execution started
2022-08-22 16:40:09.579 INFO 17264 --- [ main] com.shrikant.config.BatchConfig : writer() execution completed
2022-08-22 16:40:10.299 INFO 17264 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-08-22 16:40:10.313 INFO 17264 --- [ main] c.s.SpringBatchMultipleCsvToApplication : Started SpringBatchMultipleCsvToApplication in 5.205 seconds (JVM running for 6.176)
2022-08-22 16:40:10.316 INFO 17264 --- [ main] o.s.b.a.b.JobLauncherApplicationRunner : Running default command line with: []
2022-08-22 16:40:10.317 WARN 17264 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No datasource was provided...using a Map based JobRepository
2022-08-22 16:40:10.318 WARN 17264 --- [ main] o.s.b.c.c.a.DefaultBatchConfigurer : No transaction manager was provided, using a ResourcelessTransactionManager
2022-08-22 16:40:10.339 INFO 17264 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.
2022-08-22 16:40:10.393 INFO 17264 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=process-student]] launched with the following parameters: [{run.id=1}]
2022-08-22 16:40:10.612 INFO 17264 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [get-student]
2022-08-22 16:40:10.894 INFO 17264 --- [ main] o.s.batch.core.step.AbstractStep : Step: [get-student] executed in 282ms
2022-08-22 16:40:10.907 INFO 17264 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=process-student]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED] in 344ms
When I run this code, I don't get error, but I don't see the data in output/user.csv file
I don't what's wrong, can anyone help me ?
Thanks
The problem is in the writer you have to use new FileSystemResource("src/main/resources/output/user.csv") instead of new ClassPathResource("output/user.csv"). With this you also don't have to worry to create the file first, it will create it if not exists.
#Bean
public FlatFileItemWriter<User> writer(){
log.info("writer() execution started");
FlatFileItemWriter<User> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("src/main/resources/output/user.csv"));
writer.setAppendAllowed(true);
DelimitedLineAggregator<User> delimitedLineAggregator = new DelimitedLineAggregator<>();
delimitedLineAggregator.setDelimiter(",");
BeanWrapperFieldExtractor<User> beanWrapperFieldExtractor = new BeanWrapperFieldExtractor<>();
beanWrapperFieldExtractor.setNames(new String[] {"id","name","department","salary"});
delimitedLineAggregator.setFieldExtractor(beanWrapperFieldExtractor);
writer.setLineAggregator(delimitedLineAggregator);
log.info("writer() execution completed");
return writer;
}
I have a simple spring boot rest application. Trying to create a collection using #Document annotation in spring data mongo db. I know spring framework creates a collection if the document is denoted with #Document annotation.
Entity
#Document("User")
public class User {
#Id
private String Id;
#Field("firstName")
#TextIndexed
private String firstName;
#Field("lastName")
#TextIndexed
private String lastName;
private String address;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
properties
spring.data.mongodb.uri=mongodb://localhost:27017/Order
However, in the rest controller, it creates a collection on insert command, but still, it doesn't create a Text-index on insert.
#RestController
public class Controller {
private MongoTemplate mongoTemplate;
public Controller(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
#GetMapping("/get")
public String Get(){
mongoTemplate.insert(new User());
return "HelloWorld";
}
}
Don't have any error in the console as well
Console
2020-09-03 12:52:00.657 INFO 865 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on macbooks-MacBook-Air.local with PID 865 (/Users/macbook/Projects/Fete/demo/build/classes/java/main started by macbook in /Users/macbook/Projects/Fete/demo)
2020-09-03 12:52:00.662 INFO 865 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-09-03 12:52:02.676 INFO 865 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2020-09-03 12:52:02.712 INFO 865 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 22ms. Found 0 MongoDB repository interfaces.
2020-09-03 12:52:04.106 INFO 865 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-09-03 12:52:04.136 INFO 865 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-09-03 12:52:04.137 INFO 865 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-09-03 12:52:04.269 INFO 865 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-09-03 12:52:04.270 INFO 865 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3500 ms
2020-09-03 12:52:04.558 INFO 865 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2020-09-03 12:52:04.692 INFO 865 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017
2020-09-03 12:52:04.731 INFO 865 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=8577619}
2020-09-03 12:52:06.165 INFO 865 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-09-03 12:52:06.746 INFO 865 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-09-03 12:52:06.764 INFO 865 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 6.69 seconds (JVM running for 13.238)
Code repository
https://github.com/anandjaisy/mongoDBSpringBoot
I have to set implicitly to work
In application.properties file
spring.data.mongodb.auto-index-creation=true
Or application.yml file
spring:
data:
mongodb:
auto-index-creation: true
Reference - Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit
[New to SpringBatch] Using Spring Boot, I am trying to create a job which reads names from MongoDB, converts to lowercase, and outputs to CSV file. My reader and processor are working but the writer isn't.
My code is as follows.
Configuration file:
package bbye;
import java.util.HashMap;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.data.MongoItemReader;
import org.springframework.batch.item.data.builder.MongoItemReaderBuilder;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.DelimitedLineAggregator;
import org.springframework.batch.item.file.transform.FieldExtractor;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import hello.Person;
#Configuration
#EnableBatchProcessing
public class BatchConfigProcessing {
#Autowired
public JobBuilderFactory jobBuilderFactory;
#Autowired
public StepBuilderFactory stepBuilderFactory;
#Autowired
private MongoTemplate mongoTemplate;
private String readQuery = "{}";
// tag::readerwriterprocessor[]
#Bean
public MongoItemReader<Person> readMongo(DataSource dataSource) {
return new MongoItemReaderBuilder<Person>()
.name("mongoDocReader")
.jsonQuery(readQuery)
.targetType(Person.class)
.sorts(sort())
.template(mongoTemplate)
.collection("people")
.build();
}
#Bean
public PersonDocProcessor processor() {
return new PersonDocProcessor();
}
#Bean
public FlatFileItemWriter<Person> writer() {
/*FlatFileItemWriterBuilder<Person> writePerson = new FlatFileItemWriterBuilder<Person>();
writePerson.name("personDocWriter");
writePerson.resource(new ClassPathResource("PersonExtracted.csv"));
writePerson.lineAggregator(new DelimitedLineAggregator<Person>());
writePerson.shouldDeleteIfExists(true);
writePerson.build();*/
FlatFileItemWriter<Person> fileWriter = new FlatFileItemWriter<>();
fileWriter.setName("csvWriter");
fileWriter.setResource(new ClassPathResource("PersonExtracted.csv"));
fileWriter.setLineAggregator(lineAggregator());
fileWriter.setForceSync(true);
fileWriter.close();
return fileWriter;
}
// end::readerwriterprocessor[]
// tag::jobstep[]
#Bean
public Job exportUserJob(FileUploadNotificationListener listener, Step step1) {
return jobBuilderFactory.get("exportUserJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1)
.end()
.build();
}
#Bean
public Step step2(MongoItemReader<Person> reader) {
return stepBuilderFactory.get("step2")
.<Person, Person> chunk(10)
.reader(reader)
.processor(processor())
.writer(writer())
.build();
}
// end::jobstep[]
public FieldExtractor<Person> fieldExtractor()
{
BeanWrapperFieldExtractor<Person> extractor = new BeanWrapperFieldExtractor<>();
extractor.setNames( new String[] { "firstName",
"lastName"});
return extractor;
}
public LineAggregator<Person> lineAggregator() {
DelimitedLineAggregator<Person> la = new DelimitedLineAggregator<Person>();
la.setDelimiter(",");
la.setFieldExtractor(fieldExtractor());
return la;
}
public Map<String, Sort.Direction> sort(){
String firstName = "firstName";
Map<String, Sort.Direction> sortMap = new HashMap();
sortMap.put(firstName, Sort.DEFAULT_DIRECTION);
return sortMap;
}
}
Processor file
package bbye;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;
import hello.Person;
#Component
public class PersonDocProcessor implements ItemProcessor<Person, Person> {
private static final Logger log = LoggerFactory.getLogger(PersonDocProcessor.class);
#Override
public Person process(final Person person) throws Exception {
final String firstName = person.getFirstName().toLowerCase();
final String lastName = person.getLastName().toLowerCase();
final Person transformedPerson = new Person(firstName, lastName);
log.info("Converting (" + person + ") into (" + transformedPerson + ")");
return transformedPerson;
}
}
Listener
package bbye;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.stereotype.Component;
#Component
public class FileUploadNotificationListener implements JobExecutionListener {
#Override
public void beforeJob(JobExecution jobExecution) {
System.out.println("===== listening for job - mongoReader - fileWriter ====");
}
#Override
public void afterJob(JobExecution jobExecution) {
System.out.println("==== file write job completed =====");
}
}
Here Person is a simple POJO. The stack trace with and without manual file creation is as follows:
If the file is not present under src/main/resources FlatFileItemWriter does not create a new file and throws
org.springframework.batch.item.ItemStreamException: Could not convert resource to file: [class path resource [PersonExtracted.csv]]
at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:399) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
at org.springframework.batch.item.file.FlatFileItemWriter.open(FlatFileItemWriter.java:337) ~[spring-batch-infrastructure-4.0.1.RELEASE.jar:4.0.1.RELEASE]
........
Caused by: java.io.FileNotFoundException: class path resource [PersonExtracted.csv] cannot be resolved to URL because it does not exist
If I create the PersonExtracted.csv file manually, the program runs without errors but does not write anything to the file. In fact, a blank file is returned. The stack trace is as below.
:: Spring Boot :: (v2.0.2.RELEASE)
2018-06-19 11:35:17.663 INFO 25136 --- [ main] hello.Application : Starting Application on MyPC with PID 25136 (C:\eclipse-workspace\gs-batch-processing-master\complete\target\classes started by shristi in C:\eclipse-workspace\gs-batch-processing-master\complete)
2018-06-19 11:35:17.666 INFO 25136 --- [ main] hello.Application : No active profile set, falling back to default profiles: default
2018-06-19 11:35:17.689 INFO 25136 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#15bb6bea: startup date [Tue Jun 19 11:35:17 EDT 2018]; root of context hierarchy
2018-06-19 11:35:18.135 INFO 25136 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-06-19 11:35:18.136 WARN 25136 --- [ main] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=org.hsqldb.jdbcDriver was not found, trying direct instantiation.
2018-06-19 11:35:18.282 INFO 25136 --- [ main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (feature not supported)
2018-06-19 11:35:18.284 INFO 25136 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2018-06-19 11:35:18.293 INFO 25136 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from URL [file:/C:/eclipse-workspace/gs-batch-processing-master/complete/target/classes/schema-all.sql]
2018-06-19 11:35:18.297 INFO 25136 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from URL [file:/C:/eclipse-workspace/gs-batch-processing-master/complete/target/classes/schema-all.sql] in 4 ms.
2018-06-19 11:35:18.518 INFO 25136 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2018-06-19 11:35:18.552 INFO 25136 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:140}] to localhost:27017
2018-06-19 11:35:18.554 INFO 25136 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 0]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=1438717}
2018-06-19 11:35:18.723 INFO 25136 --- [ main] o.s.b.c.r.s.JobRepositoryFactoryBean : No database type set, using meta data indicating: HSQL
2018-06-19 11:35:18.770 INFO 25136 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.
2018-06-19 11:35:18.778 INFO 25136 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql]
2018-06-19 11:35:18.781 INFO 25136 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [org/springframework/batch/core/schema-hsqldb.sql] in 3 ms.
2018-06-19 11:35:18.870 INFO 25136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-06-19 11:35:18.871 INFO 25136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name 'dataSource' has been autodetected for JMX exposure
2018-06-19 11:35:18.873 INFO 25136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]
2018-06-19 11:35:18.880 INFO 25136 --- [ main] hello.Application : Started Application in 1.357 seconds (JVM running for 2.284)
2018-06-19 11:35:18.881 INFO 25136 --- [ main] o.s.b.a.b.JobLauncherCommandLineRunner : Running default command line with: []
2018-06-19 11:35:18.908 INFO 25136 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=exportUserJob]] launched with the following parameters: [{run.id=1}]
===== listening for job - mongoReader - fileWriter ====
2018-06-19 11:35:18.917 INFO 25136 --- [ main] o.s.batch.core.job.SimpleStepHandler : Executing step: [step2]
2018-06-19 11:35:18.995 INFO 25136 --- [ main] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:141}] to localhost:27017
2018-06-19 11:35:19.022 INFO 25136 --- [ main] bbye.PersonDocProcessor : Converting (firstName: ALICE, lastName: WONDERLAND) into (firstName: alice, lastName: wonderland)
2018-06-19 11:35:19.022 INFO 25136 --- [ main] bbye.PersonDocProcessor : Converting (firstName: FIRSTNAME, lastName: LASTNAME) into (firstName: firstname, lastName: lastname)
2018-06-19 11:35:19.022 INFO 25136 --- [ main] bbye.PersonDocProcessor : Converting (firstName: JANE, lastName: DOE) into (firstName: jane, lastName: doe)
2018-06-19 11:35:19.022 INFO 25136 --- [ main] bbye.PersonDocProcessor : Converting (firstName: JOHN, lastName: DOE) into (firstName: john, lastName: doe)
2018-06-19 11:35:19.022 INFO 25136 --- [ main] bbye.PersonDocProcessor : Converting (firstName: MARK, lastName: WINN) into (firstName: mark, lastName: winn)
==== file write job completed =====
2018-06-19 11:35:19.031 INFO 25136 --- [ main] o.s.b.c.l.support.SimpleJobLauncher : Job: [FlowJob: [name=exportUserJob]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED]
2018-06-19 11:35:19.032 INFO 25136 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#15bb6bea: startup date [Tue Jun 19 11:35:17 EDT 2018]; root of context hierarchy
2018-06-19 11:35:19.033 INFO 25136 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2018-06-19 11:35:19.034 INFO 25136 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans
2018-06-19 11:35:19.035 INFO 25136 --- [ Thread-2] org.mongodb.driver.connection : Closed connection [connectionId{localValue:2, serverValue:141}] to localhost:27017 because the pool has been closed.
2018-06-19 11:35:19.036 INFO 25136 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2018-06-19 11:35:19.037 INFO 25136 --- [ Thread-2] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
I think we should use FileSystemResource instead of ClassPathResource. Could you please try and let us know.
Answering the question. Turns out the writer was working fine but I was looking at the wrong file.
When using ClassPathResource the file gets created and updated under target/classes directory. However, I was looking at PersonExtracted.csv under src/main/resources directory, which was never updated.
If I specify FileSystemResource, the file gets created and updated at specified location.
Attempting to create a simple site that displays the content on my MySql database. I use the word attempting loosely lol. Notice the pages I have "/all" which I use to check if my list is empty. I'm trying to figure out why is my list empty and not storing the content of my database?
MySql:
CREATE DATABASE IF NOT EXISTS `web_customer_tracker`;
USE `web_customer_tracker`;
DROP TABLE IF EXISTS `customer`;
CREATE TABLE `customer`(
`id` INT(11) NOT NULL auto_increment,
`first_name` VARCHAR(45) DEFAULT NULL,
`last_name` VARCHAR(45)DEFAULT NULL,
`email` VARCHAR(45) DEFAULT NULL,
PRIMARY KEY(`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 CHARSET=latin1;
INSERT INTO `customer` VALUES
(1, 'David', 'Adams', 'david#luv2code.com'),
(2, 'John', 'Die', 'john#luv2code.com'),
(3, 'Ajay', 'Rao', 'ajay#luv2code.com'),
(4, 'Mary', 'Publiidc', 'mary#luv2code.com'),
(5, 'Maxwell', 'Dixon', 'maxwell#luv2code.com');
Entity:
#Entity
#Table(name = "customer")
public class Customer implements Serializable {
#Column(name = "id")
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "email")
private String email;
public Customer(){}
public Customer(String firstName, String lastName, String company){
this.firstName = firstName;
this.lastName = lastName;
this.email = company;
}
#Override
public String toString() {
return "Customer{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCompany() {
return email;
}
public void setCompany(String company) {
this.email = company;
}
}
DAO
#Repository
#Transactional
public interface CustomerDAO extends CrudRepository<Customer, String> {
public List<Customer> findAll();
}
Impl:
#Transactional
#Repository
public abstract class CustomerDAOImpl implements CustomerDAO {
private CustomerDAO customerDAO;
#Override
public List<Customer> findAll() {
return customerDAO.findAll();
}
}
Controller:
#Controller
public class CustomerController {
#Autowired
private CustomerDAO customerDAO;
public String string;
#RequestMapping(value = "/list")
public String listCustomers(Model model){
List<Customer> customers = customerDAO.findAll();
String string = customers.get(1).toString();
model.addAttribute("text", string);
model.addAttribute("customers", customers);
return "list-customers";
}
#RequestMapping(value = "/all")
#ResponseBody
public String get() {
List<Customer> customers = customerDAO.findAll();
String names;
if(customers.isEmpty() == true){
names = "This is empty";
} else {
names = "Something is in here";
}
return names;
}
}
Stack Trace:
/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java -Didea.launcher.port=7536 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/lib/tools.jar:/Users/ronald/IdeaProjects/springtutorial/target/classes:/Users/ronald/mysql-connector-java-5.1.41/mysql-connector-java-5.1.41-bin.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter-thymeleaf/1.5.1.RELEASE/spring-boot-starter-thymeleaf-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter/1.5.1.RELEASE/spring-boot-starter-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot/1.5.1.RELEASE/spring-boot-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.5.1.RELEASE/spring-boot-autoconfigure-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.5.1.RELEASE/spring-boot-starter-logging-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/ch/qos/logback/logback-classic/1.1.9/logback-classic-1.1.9.jar:/Users/ronald/.m2/repository/ch/qos/logback/logback-core/1.1.9/logback-core-1.1.9.jar:/Users/ronald/.m2/repository/org/slf4j/jul-to-slf4j/1.7.22/jul-to-slf4j-1.7.22.jar:/Users/ronald/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.22/log4j-over-slf4j-1.7.22.jar:/Users/ronald/.m2/repository/org/yaml/snakeyaml/1.17/snakeyaml-1.17.jar:/Users/ronald/.m2/repository/org/thymeleaf/thymeleaf-spring4/2.1.5.RELEASE/thymeleaf-spring4-2.1.5.RELEASE.jar:/Users/ronald/.m2/repository/nz/net/ultraq/thymeleaf/thymeleaf-layout-dialect/1.4.0/thymeleaf-layout-dialect-1.4.0.jar:/Users/ronald/.m2/repository/org/codehaus/groovy/groovy/2.4.7/groovy-2.4.7.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter-web/1.5.1.RELEASE/spring-boot-starter-web-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/1.5.1.RELEASE/spring-boot-starter-tomcat-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/8.5.11/tomcat-embed-websocket-8.5.11.jar:/Users/ronald/.m2/repository/org/hibernate/hibernate-validator/5.3.4.Final/hibernate-validator-5.3.4.Final.jar:/Users/ronald/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar:/Users/ronald/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.8.6/jackson-databind-2.8.6.jar:/Users/ronald/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.8.0/jackson-annotations-2.8.0.jar:/Users/ronald/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.6/jackson-core-2.8.6.jar:/Users/ronald/.m2/repository/org/springframework/spring-web/4.3.6.RELEASE/spring-web-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-aop/4.3.6.RELEASE/spring-aop-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-context/4.3.6.RELEASE/spring-context-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-webmvc/4.3.6.RELEASE/spring-webmvc-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-expression/4.3.6.RELEASE/spring-expression-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-core/4.3.6.RELEASE/spring-core-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-orm/4.3.7.RELEASE/spring-orm-4.3.7.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-beans/4.3.6.RELEASE/spring-beans-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-jdbc/4.3.6.RELEASE/spring-jdbc-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/spring-tx/4.3.6.RELEASE/spring-tx-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/hibernate/hibernate-core/5.2.9.Final/hibernate-core-5.2.9.Final.jar:/Users/ronald/.m2/repository/org/jboss/logging/jboss-logging/3.3.0.Final/jboss-logging-3.3.0.Final.jar:/Users/ronald/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar:/Users/ronald/.m2/repository/org/javassist/javassist/3.21.0-GA/javassist-3.21.0-GA.jar:/Users/ronald/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar:/Users/ronald/.m2/repository/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.1.Final/jboss-transaction-api_1.2_spec-1.0.1.Final.jar:/Users/ronald/.m2/repository/org/jboss/jandex/2.0.3.Final/jandex-2.0.3.Final.jar:/Users/ronald/.m2/repository/com/fasterxml/classmate/1.3.3/classmate-1.3.3.jar:/Users/ronald/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar:/Users/ronald/.m2/repository/org/hibernate/common/hibernate-commons-annotations/5.0.1.Final/hibernate-commons-annotations-5.0.1.Final.jar:/Users/ronald/.m2/repository/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar:/Users/ronald/.m2/repository/c3p0/c3p0/0.9.1.2/c3p0-0.9.1.2.jar:/Users/ronald/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter-data-jpa/1.5.1.RELEASE/spring-boot-starter-data-jpa-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter-aop/1.5.1.RELEASE/spring-boot-starter-aop-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/aspectj/aspectjweaver/1.8.9/aspectjweaver-1.8.9.jar:/Users/ronald/.m2/repository/org/springframework/boot/spring-boot-starter-jdbc/1.5.1.RELEASE/spring-boot-starter-jdbc-1.5.1.RELEASE.jar:/Users/ronald/.m2/repository/org/apache/tomcat/tomcat-jdbc/8.5.11/tomcat-jdbc-8.5.11.jar:/Users/ronald/.m2/repository/org/apache/tomcat/tomcat-juli/8.5.11/tomcat-juli-8.5.11.jar:/Users/ronald/.m2/repository/org/hibernate/hibernate-entitymanager/5.0.11.Final/hibernate-entitymanager-5.0.11.Final.jar:/Users/ronald/.m2/repository/javax/transaction/javax.transaction-api/1.2/javax.transaction-api-1.2.jar:/Users/ronald/.m2/repository/org/springframework/data/spring-data-jpa/1.11.0.RELEASE/spring-data-jpa-1.11.0.RELEASE.jar:/Users/ronald/.m2/repository/org/springframework/data/spring-data-commons/1.13.0.RELEASE/spring-data-commons-1.13.0.RELEASE.jar:/Users/ronald/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.22/jcl-over-slf4j-1.7.22.jar:/Users/ronald/.m2/repository/org/springframework/spring-aspects/4.3.6.RELEASE/spring-aspects-4.3.6.RELEASE.jar:/Users/ronald/.m2/repository/org/thymeleaf/thymeleaf-spring3/3.0.3.RELEASE/thymeleaf-spring3-3.0.3.RELEASE.jar:/Users/ronald/.m2/repository/org/thymeleaf/thymeleaf/2.1.5.RELEASE/thymeleaf-2.1.5.RELEASE.jar:/Users/ronald/.m2/repository/org/unbescape/unbescape/1.1.0.RELEASE/unbescape-1.1.0.RELEASE.jar:/Users/ronald/.m2/repository/org/slf4j/slf4j-api/1.7.22/slf4j-api-1.7.22.jar:/Users/ronald/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.11/tomcat-embed-core-8.5.11.jar:/Users/ronald/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/8.5.11/tomcat-embed-el-8.5.11.jar:/Users/ronald/.m2/repository/javax/servlet/jstl/1.2/jstl-1.2.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain com.luv2code.springtutorial.Application
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.1.RELEASE)
2017-04-16 20:11:51.726 INFO 8023 --- [ main] com.luv2code.springtutorial.Application : Starting Application on Ronalds-MacBook-Pro.local with PID 8023 (/Users/ronald/IdeaProjects/springtutorial/target/classes started by ronald in /Users/ronald/IdeaProjects/springtutorial)
2017-04-16 20:11:51.729 INFO 8023 --- [ main] com.luv2code.springtutorial.Application : No active profile set, falling back to default profiles: default
2017-04-16 20:11:52.009 INFO 8023 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#4f9a3314: startup date [Sun Apr 16 20:11:52 EDT 2017]; root of context hierarchy
2017-04-16 20:11:53.416 INFO 8023 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-04-16 20:11:53.517 INFO 8023 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-04-16 20:11:53.579 INFO 8023 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$84f7773b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-04-16 20:11:54.026 INFO 8023 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-04-16 20:11:54.050 INFO 8023 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-04-16 20:11:54.051 INFO 8023 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-04-16 20:11:54.217 INFO 8023 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-04-16 20:11:54.218 INFO 8023 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2212 ms
2017-04-16 20:11:54.424 INFO 8023 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-04-16 20:11:54.431 INFO 8023 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-04-16 20:11:54.432 INFO 8023 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-04-16 20:11:54.432 INFO 8023 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-04-16 20:11:54.432 INFO 8023 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-04-16 20:11:55.310 INFO 8023 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-04-16 20:11:55.339 INFO 8023 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-04-16 20:11:55.420 INFO 8023 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.2.9.Final}
2017-04-16 20:11:55.422 INFO 8023 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-04-16 20:11:55.607 INFO 8023 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-04-16 20:11:55.739 INFO 8023 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-04-16 20:11:56.398 INFO 8023 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl#1e225820'
2017-04-16 20:11:56.403 INFO 8023 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-04-16 20:11:57.156 INFO 8023 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#4f9a3314: startup date [Sun Apr 16 20:11:52 EDT 2017]; root of context hierarchy
2017-04-16 20:11:57.276 INFO 8023 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/all]}" onto public java.lang.String com.luv2code.springtutorial.controller.CustomerController.get()
2017-04-16 20:11:57.278 INFO 8023 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/list]}" onto public java.lang.String com.luv2code.springtutorial.controller.CustomerController.listCustomers(org.springframework.ui.Model)
2017-04-16 20:11:57.283 INFO 8023 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-04-16 20:11:57.284 INFO 8023 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-04-16 20:11:57.342 INFO 8023 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-16 20:11:57.342 INFO 8023 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-16 20:11:57.425 INFO 8023 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-16 20:11:58.236 INFO 8023 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-04-16 20:11:58.335 INFO 8023 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-04-16 20:11:58.342 INFO 8023 --- [ main] com.luv2code.springtutorial.Application : Started Application in 17.231 seconds (JVM running for 17.958)
2017-04-16 20:12:03.155 INFO 8023 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-04-16 20:12:03.155 INFO 8023 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-04-16 20:12:03.176 INFO 8023 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 21 ms
2017-04-16 20:12:04.403 INFO 8023 --- [nio-8080-exec-2] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
First things first ,
Make sure you have done the below things.
You do not need CustomerDAOImpl, remove it.
In CustomerDAO change <Customer,String> to <Customer,Integer>
Again in the same interface remove #Transactional and #Repository, they are not required.
Rest of the code looks just fine. Try the above steps.
Thank you
suppose you remove the CustomerDAOImpl class and see.
You only need the Entity class (Customer) , Repository interface (CustomerDAO) which extendsCrudRepository<Customer, int> and the controller.
Sorry, if i am not wrong then where is your entitymanager and other database operation to fetch the list(). I mean where is the logic to fetch data.
your return customerDAO.findAll(); did nothing at all.you just calling method and no other things actually.
I want to save data to a table in postgresql. I am using spring boot + postgresql along with hibernate. My Application does not have any error but it is not creating table in database.
This is my controller class
package com.ge.health.poc.controlleer;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ge.health.poc.model.Bookmodel;
import com.ge.health.poc.service.BookServiceImplementation;
#RestController
public class HttpController {
#Autowired
BookServiceImplementation bookserviceimpl;
#RequestMapping(value = "/httpmethod", method = RequestMethod.POST)
#ResponseBody
public void helloService(#RequestBody String input) throws JsonParseException, JsonMappingException, IOException {
System.out.println(input);
ObjectMapper mapper = new ObjectMapper();
Bookmodel pojodata = mapper.readValue(input, Bookmodel.class);
System.out.println(pojodata);
}
}
AppConfig.java
package com.ge.health.poc.configuration;
import java.util.Properties;
import javax.annotation.Resource;
import javax.jms.ConnectionFactory;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
public class AppConfig {
#Resource
private SettingConfig settings;
#Bean
JmsTemplate jmsTemplate(ConnectionFactory connectionFactory) {
return new JmsTemplate(connectionFactory);
}
#Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(settings.getDriverClassName());
dataSource.setUrl(settings.getDatasource());
dataSource.setUsername(settings.getUsername());
dataSource.setPassword(settings.getPassword());
return dataSource;
}
/**
* Declare the JPA entity manager factory.
*/
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
// Hibernate properties
Properties additionalProperties = new Properties();
additionalProperties.put("hibernate.dialect", settings.getDialect());
additionalProperties.put("hibernate.show_sql", settings.getShowsql());
additionalProperties.put("hibernate.hbm2ddl.auto", settings.getDdlauto());
entityManagerFactory.setJpaProperties(additionalProperties);
return entityManagerFactory;
}
/**
* Declare the transaction manager.
*/
#Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
/**
* PersistenceExceptionTranslationPostProcessor is a bean post processor
* which adds an advisor to any bean annotated with Repository so that any
* platform-specific exceptions are caught and then rethrown as one Spring's
* unchecked data access exceptions (i.e. a subclass of
* DataAccessException).
*/
#Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
SettingConfig.java
package com.ge.health.poc.configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class SettingConfig {
public String getDdlauto() {
return ddlauto;
}
public void setDdlauto(String ddlauto) {
this.ddlauto = ddlauto;
}
public String getShowsql() {
return showsql;
}
public void setShowsql(String showsql) {
this.showsql = showsql;
}
public String getDialect() {
return dialect;
}
public void setDialect(String dialect) {
this.dialect = dialect;
}
#Value("${spring.datasource.url}")
private String datasource;
#Value("${hibernate.hbm2ddl.auto}")
private String ddlauto;
#Value("${hibernate.show_sql}")
private String showsql;
#Value("{hibernate.dialect}")
private String dialect;
#Value("${spring.datasource.username}")
private String username;
#Value("${spring.datasource.password}")
private String password;
#Value("${spring.datasource.driver-class-name}")
private String driverClassName;
public String getDatasource() {
return datasource;
}
public void setDatasource(String datasource) {
this.datasource = datasource;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
application.properties
# Database
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/bookdetails
spring.datasource.username=postgres
spring.datasource.password=admin
# Hibernate
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
POJO class
package com.ge.health.poc.model;
import javax.persistence.Column;
import javax.persistence.Table;
import org.springframework.data.annotation.Id;
import org.springframework.stereotype.Component;
#Component
#Table
public class Bookmodel {
#Id
private String id;
#Column
private String name;
#Column
private String isbn;
#Column
private String author;
#Column
private String pages;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPages() {
return pages;
}
public void setPages(String pages) {
this.pages = pages;
}
#Override
public String toString() {
return "Bookmodel [id=" + id + ", name=" + name + ", isbn=" + isbn + ", author=" + author + ", pages=" + pages
+ "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
I want to save data to a table in postgresql.I am using spring boot + postgresql along with hibernate. My Application donot have any error but it is not creating table in database.
The problem is that you are using wrong annotation i.e #component.
Remove this annotation and use #Entity.
This is your code.
After executing this code check the logs, you will see no sql create query is fired by hibernate.
package com.example.demo.hibernateDemoEntity;
import javax.persistence.*;
import org.springframework.stereotype.Component;
#Component
#Table
public class Bookmodel {
#Id
private int id;
#Column
private String name;
#Column
private String isbn;
#Column
private String author;
#Column
private String pages;
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-09-01 16:52:15.532 INFO 5436 --- [ main] c.example.demo.HibernateDemoApplication : Starting HibernateDemoApplication on BGINMAC004.local with PID 5436 (/Users/Dildeep.Singh/Documents/workspace-sts-3.9.9.RELEASE/HibernateDemo/target/classes started by Dildeep.Singh in /Users/Dildeep.Singh/Documents/workspace-sts-3.9.9.RELEASE/HibernateDemo)
2019-09-01 16:52:15.536 INFO 5436 --- [ main] c.example.demo.HibernateDemoApplication : No active profile set, falling back to default profiles: default
2019-09-01 16:52:16.331 INFO 5436 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-01 16:52:16.350 INFO 5436 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 12ms. Found 0 repository interfaces.
2019-09-01 16:52:16.730 INFO 5436 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$de6750d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-09-01 16:52:17.026 INFO 5436 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-01 16:52:17.049 INFO 5436 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-01 16:52:17.050 INFO 5436 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-09-01 16:52:17.156 INFO 5436 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-01 16:52:17.156 INFO 5436 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1581 ms
2019-09-01 16:52:17.355 INFO 5436 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-09-01 16:52:17.805 INFO 5436 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-09-01 16:52:17.844 INFO 5436 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-09-01 16:52:17.894 INFO 5436 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-09-01 16:52:17.895 INFO 5436 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-09-01 16:52:18.056 INFO 5436 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-09-01 16:52:18.147 INFO 5436 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-09-01 16:52:18.388 INFO 5436 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl#43a09ce2'
2019-09-01 16:52:18.390 INFO 5436 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-09-01 16:52:18.657 INFO 5436 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-01 16:52:18.690 WARN 5436 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-09-01 16:52:18.883 INFO 5436 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-01 16:52:18.886 INFO 5436 --- [ main] c.example.demo.HibernateDemoApplication : Started HibernateDemoApplication in 18.83 seconds (JVM running for 24.343)
Now replace #Component with #Entity annotation and see the logs.
#Entity
#Table
public class Bookmodel {
#Id
private int id;
#Column
private String name;
#Column
private String isbn;
#Column
private String author;
#Column
private String pages;
}
Spring Logs
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.7.RELEASE)
2019-09-01 17:10:59.943 INFO 5461 --- [ main] c.example.demo.HibernateDemoApplication : Starting HibernateDemoApplication on BGINMAC004.local with PID 5461 (/Users/Dildeep.Singh/Documents/workspace-sts-3.9.9.RELEASE/HibernateDemo/target/classes started by Dildeep.Singh in /Users/Dildeep.Singh/Documents/workspace-sts-3.9.9.RELEASE/HibernateDemo)
2019-09-01 17:10:59.946 INFO 5461 --- [ main] c.example.demo.HibernateDemoApplication : No active profile set, falling back to default profiles: default
2019-09-01 17:11:00.629 INFO 5461 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-01 17:11:00.649 INFO 5461 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 13ms. Found 0 repository interfaces.
2019-09-01 17:11:00.996 INFO 5461 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d77725a9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-09-01 17:11:01.227 INFO 5461 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-01 17:11:01.249 INFO 5461 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-01 17:11:01.249 INFO 5461 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-09-01 17:11:01.357 INFO 5461 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-01 17:11:01.357 INFO 5461 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1374 ms
2019-09-01 17:11:01.519 INFO 5461 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-09-01 17:11:01.919 INFO 5461 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-09-01 17:11:01.958 INFO 5461 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-09-01 17:11:02.006 INFO 5461 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-09-01 17:11:02.010 INFO 5461 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-09-01 17:11:02.182 INFO 5461 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-09-01 17:11:02.283 INFO 5461 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
**Hibernate: drop table if exists bookmodel
Hibernate: create table bookmodel (id integer not null, author varchar(255), isbn varchar(255), name varchar(255), pages varchar(255), primary key (id)) engine=MyISAM**
2019-09-01 17:11:02.893 INFO 5461 --- [ main] o.h.t.schema.internal.SchemaCreatorImpl : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl#134f8ef6'
2019-09-01 17:11:02.895 INFO 5461 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-09-01 17:11:03.209 INFO 5461 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-01 17:11:03.239 WARN 5461 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-09-01 17:11:03.442 INFO 5461 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-01 17:11:03.445 INFO 5461 --- [ main] c.example.demo.HibernateDemoApplication : Started HibernateDemoApplication in 18.81 seconds (JVM running for 24.307)
Now you can see sql create query is fired by hibernate.
You are "using" Spring Boot and the first thing you do is try very hard not to use Spring Boot.
Instead of doing all the configuration by yourself let Spring Boot do the heavy lifting for you.
In your application.properties set the proper dialect and use the correct properties (see this section of the Spring Boot Reference Guide for a full list of properties).
# Database
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/bookdetails
spring.datasource.username=postgres
spring.datasource.password=admin
# Hibernate
spring.jpa.database=org.hibernate.dialect.PostgreSQL94Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
And remove your custom configuration (i.e. the AppConfig and SettingConfig) that breaks the auto configuration of Spring Boot.
I don't see any column definition in your POJO, how do you expect hibernate to know what type of column to create by simply give a Java type such as String? For my MySQL database I would define POJO like this
#Component
#Table(name = "Book")
public class Bookmodel {
#Size(max = 32)
#Id
#Column(columnDefinition = "varchar(128)", nullable = false, unique = true)
private String id;
#Column(columnDefinition = "varchar(128)", nullable = false)
private String name;
...
}