简体   繁体   中英

I have a problem when trying to run a spring boot application

So i have been working on a spring boot application for a school project and i cant run it because when i try to save in a repository i get the following errors. I have tried many things like changing the relation types,adding cascades but nothing seems to work. Below is the code for 2 classes and main and also the errors

package com.site.anime.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Fetch;

import javax.persistence.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Show {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;


    private String name;
    private String image;



    private Integer no_episodes;
    private String description;
    private Double overall_score;

    @ManyToMany
    private List<Caracter> caracters;

    @OneToMany
    private List<Review> reviews;

    @ManyToMany
    private List<Category> categories;

}
package com.site.anime.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.List;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Caracter {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
    private String image;


    private String description;

    @ManyToMany
    private List<Show> shows;
}
package com.site.anime;
import com.site.anime.model.*;
import com.site.anime.repository.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@EnableJpaRepositories
@SpringBootApplication
public class AnimeApplication {

    public static void main(String[] args) {
        SpringApplication.run(AnimeApplication.class, args);
    }

    @Bean
    CommandLineRunner init(RepositoryShow repositoryShow, RepositoryCategory repositoryCategory,
                           RepositoryCaracter repositoryCaracter, RepositoryUser repositoryUser,
                           RepositorySuperUser repositorySuperUser){
        return args -> {

            List<Show> shows=new LinkedList<>();
            Caracter caracter=new Caracter(null,"rusu","","",null);

            List<Caracter> caracters=new LinkedList<>();
            caracters.add(caracter);

            Profile profile=new Profile();
            Utilizator user=new Utilizator(null,"","","",profile,null);
            //Review review=new Review(null,user,"",0);
            Review review=new Review(null,null,"",0);
            List<Review> reviews=new LinkedList<>();
            reviews.add(review);

            user.setReviews(reviews);
            Show show=new Show(null,"ReZero Season 1","",25,"",0.0,null,reviews,null);

            shows.add(show);

            caracter.setShows(shows);

            show.setCaracters(caracters);
//


            //repositoryShow.save(show);
            repositoryCaracter.save(caracter);
            //repositoryUser.save(user);





        };
    }
}

and the errors

2021-03-11 14:11:02.765  INFO 10248 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-03-11 14:11:02.771  INFO 10248 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-03-11 14:11:03.007  WARN 10248 --- [           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
2021-03-11 14:11:03.065  INFO 10248 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 14:11:03.193  INFO 10248 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 14:11:03.198  INFO 10248 --- [           main] com.site.anime.AnimeApplication          : Started AnimeApplication in 2.922 seconds (JVM running for 3.566)
2021-03-11 14:11:03.245  WARN 10248 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1064, SQLState: 42000
2021-03-11 14:11:03.245 ERROR 10248 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show (description, image, name, no_episodes, overall_score, id) values ('', '', ' at line 1
2021-03-11 14:11:03.252  INFO 10248 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-11 14:11:03.263 ERROR 10248 --- [           main] o.s.boot.SpringApplication               : Application run failed
``

SHOW is a keyword for MySql (as well as NAME and DESCRIPTION ), so you should use SQL quoted identifier , like below:

@Entity
@Table(name = "`show`")
public class Show {

  // ...

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

  @Column(name = "`description`")
  private String description;

}

I solved the problem For anyone wondering,one of my classes was called Show,and sql has a reserved word called show

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM