org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptDataSourceInitializer'

1.6k Views Asked by At

I tried to compile very easy program with saving easy table with 3 users to http://localhost/phpmyadmin to empty database named ,,users'' but it still show me exceptions which you can see.

1 exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scriptDataSourceInitializer' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceInitializationConfiguration$SharedCredentialsDataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [data.sql]: insert into user (name,surname,email,city) values ('Przemek','Mazurek','[email protected]','Warsaw'), ('Anna', 'Grodna','[email protected]','Lublin'), ('Zosia','Gulina','[email protected]','Praga'); nested exception is java.sql.SQLSyntaxErrorException: Table 'users.user' doesn't exist

2 exception: Caused by: java.sql.SQLSyntaxErrorException: Table 'users.user' doesn't exist

3 exception: Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [data.sql]: insert into user (name,surname,email,city) values ('Przemek','Mazurek','[email protected]','Warsaw'), ('Anna', 'Grodna','[email protected]','Lublin'), ('Zosia','Gulina','[email protected]','Praga'); nested exception is java.sql.SQLSyntaxErrorException: Table 'users.user' doesn't exist

  [applications.properties file][1]

[User and Entity class][2]
[data.sql file][3] 
[User Repository][4]


  [1]: https://i.stack.imgur.com/R8A5T.png
  [2]: https://i.stack.imgur.com/29Yym.png
  [3]: https://i.stack.imgur.com/u4aWt.png
  [4]: https://i.stack.imgur.com/laAB3.png
1

There are 1 best solutions below

0
On

I think that you are missing a table in your users database. The issue is that you are trying to insert the data, into your table, which is not present in your database.

Try to execute this script and try again

CREATE TABLE user (
    id int NOT NULL AUTO_INCREMENT,
    name varchar(255),
    surname varchar(255),
    email varchar(255),
    city varchar(255),
    PRIMARY KEY (id)
);

Also don't forget to properly map your entity to your database

@Entity
@Getter @Setter @NoArgsConstructor
@Table(name="user")
public class User{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    
    @Column(name="name")
    private String name;

    @Column(name="surname")
    private String surname;

    @Column(name="email")<-- this email is matching value of the field in the database
    private String email;<-- this email is your entity field

    @Column(name="city")
    private String city ;