I've tried to create JPA small demo project, I faced this problem
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personJdbcDao': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/D:/Learning/c5/springframework/target/classes/schema.sql]: create table person ( ID integer not null, PERSON_NAME varchar(255) not null, LOCATION varchar(255), BIRTH_DATE timestamp, primary key(ID) ); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" already exists; SQL statement: create table person ( ID integer not null, PERSON_NAME varchar(255) not null, LOCATION varchar(255), BIRTH_DATE timestamp, primary key(ID) ) [42101-214] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.24.jar:5.3.24]
Below my config
I made schema.sql for my H2 in the memory database.
src/main/resources/schema.sql
create table person
(
ID integer not null,
PERSON_NAME varchar(255) not null,
LOCATION varchar(255),
BIRTH_DATE timestamp,
primary key(ID)
);
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(1, 'Fazal', 'Peshawar',NOW());
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(2, 'Haroon', 'Islamabad',NOW());
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(3, 'Khan', 'Lahore',NOW());
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(4, 'Khan', 'Islamabad',NOW());
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
Main class:
@SpringBootApplication
public class DemoJPAApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(DemoJPAApplication.class);
@Autowired
PersonRepository personRepository;
public static void main(String[] args) {
SpringApplication.run(DemoJPAApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
logger.info("User id 1 -> {}", personRepository.findById(1));
}
}
Repository Class
@Repository
@Transactional
public class PersonRepository {
@PersistenceContext
EntityManager entityManager;
public Person findById(int id){
return entityManager.find(Person.class, id);
}
}
enitity class, here i use lombok
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue
private Integer id;
@Column(name = "person_name")
private String personName;
@Column(name = "location")
private String location;
@Column(name = "birth_date")
private Date birthDate;
}
Could someone find what's wrong with my project? Really appreciate your help.
I run the problem and faced this problem
CodePudding user response:
There is a small error that i face when i run this program.
The error said "Table 'PERSON' already exists" because we are already creating in schema.sql.
Spring boot auto configuration knows that we are using in-memory database H2 and that JPA is in the classpath. It also knows that I'm defining entities as well because i put @Entity in the Person class. So what it does is triggers schema update which is the hibernating feature, it automatically creates this schema for us.
so from now on, i don't need to define CREATE TABLE in schema.sql
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(1, 'Fazal', 'Peshawar',NOW());
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(2, 'Haroon', 'Islamabad',NOW());
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(3, 'Khan', 'Lahore',NOW());
INSERT INTO PERSON (ID, PERSON_NAME, LOCATION, BIRTH_DATE )
VALUES(4, 'Khan', 'Islamabad',NOW());
i remove the CREATE TABLE PERSON from src/main/resources/schema.sql because the table would be created for me by schema update.
Schema update is triggered by a spring boot auto configuration and is one of the hibernate features.