Entitätsklasse
@ToString
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "game_articles")
public class Articles {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "title")
private String title;
@Column(name = "reviewer")
private String reviewer;
@Column(name = "review")
private String review;
@Column(name = "path")
private String path;
@Column(columnDefinition = "integer default 0")
private int comments;
public Articles(String title, String reviewer, String review, String path, int comments){
this.title = title;
this.reviewer = reviewer;
this.review = review;
this.path = path;
this.comments = comments;
}
}
Controller: Die kommentierte Methode war mein erster Versuch.
@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping("/api/v1")
@RestController
@AllArgsConstructor
public class ArticlesController {
@Autowired
ArticlesRepository articlesRepository;
@GetMapping("/articles")
public List getAllArticles(){
// createArticle();
return articlesRepository.findAll();
}
public void createArticle(){ //ignore
Articles a = new Articles(1L,"Title Test", "TyTest", "This is a test review", "path/totest", 10);
articlesRepository.save(a);
}
// @GetMapping("/articles")
// public ResponseEntity getAllArticles(){
// try {
//// articlesRepository.findAll().forEach(articles::add);
// return new ResponseEntity(articles, HttpStatus.OK);
// }
// catch (Exception e){
// return new ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR);
// }
// }
}
Repository
@Repository
public interface ArticlesRepository extends JpaRepository {
}
SQL-Abfrage in den Protokollen. Ich bin mir nicht sicher, warum auf die Spalten „Artikel0“ verwiesen wird, aber es hatte keine Auswirkung auf das Hinzufügen von Daten zur Datenbank.
Hibernate:
select
articles0_.id as id1_0_,
articles0_.comments as comments2_0_,
articles0_.path as path3_0_,
articles0_.review as review4_0_,
articles0_.reviewer as reviewer5_0_,
articles0_.title as title6_0_
from
game_articles articles0_
