#PostMapping saving empty objects spring boot+jpa+gradle+mysql db - java

I'm trying to make a crud app using SpringBoot + JPA + HIBERNATE + GRADLE + MYSQL. My issue is that when I hit an #PostMapping endpoint to save a user, an empty object get's saved in database. Only id is generated but the other column shows null. Also when I hit #GetMapping it returns empty object list . I'm using PostMan for requests.
Images of Postamn Requests
create
all
Mysql Database
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.1'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'db.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '19'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
application.properties
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.datasource.url=jdbc:mysql://localhost:3306/auto?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mainApplication.java
package db.example.autodbconnect;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class AutodbconnectApplication {
public static void main(String[] args) {
SpringApplication.run(AutodbconnectApplication.class, args);
}
}
UserEntity
package db.example.autodbconnect.entities;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
#Entity
#Table
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column
private String name;
}
User JPA Repository
package db.example.autodbconnect.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import db.example.autodbconnect.entities.User;
#Repository
public interface UserRepository extends JpaRepository<User,Long> {
}
User Service
package db.example.autodbconnect.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import db.example.autodbconnect.entities.User;
import db.example.autodbconnect.repositories.UserRepository;
#Service
public class UserService {
#Autowired
private UserRepository userRepo;
public User create(User body) {
return userRepo.save(body);
}
public List<User> findAll(){
List<User> res=userRepo.findAll();
return res;
}
}
User Controller
package db.example.autodbconnect.contoller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import db.example.autodbconnect.entities.User;
import db.example.autodbconnect.service.UserService;
#RestController
#RequestMapping("/users")
public class UserController {
#Autowired
private UserService userService;
#PostMapping("/create")
public ResponseEntity<User> create(#RequestBody User body) {
return new ResponseEntity<User>(userService.create(body), HttpStatus.CREATED);
}
#GetMapping("/all")
public ResponseEntity<List<User>> findALL(){
return new ResponseEntity<List<User>>(userService.findAll(),HttpStatus.OK);
}
}

You have not added getter setter methods for each field that's why it is storing null values
solution-
1-either add getter setter method for each field or
2-you can also add lombook dependency and use #Data annotation it will add getter setter method for you
Hope it will help you

Related

Why does spring boot behave abnormally sometimes ? - Cannot find Autowired class

I have been experimenting with spring boot since a while now, and I have noticed few strange things. Sometimes we have to include #ComponentScan in main class to solve the error which goes like try defining the bean of type package.name .
For eg. I created many sprint boot application without including #ComponentScan or any such annotation like #EnableJpaRepositories but recently I created one spring boot project which says Cannot find bean of type RepositoryName and when I used #ComponentScan , It worked. Why is that ?
You can see the code below:
Spring Class:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan(basePackages = {"com.example.demo.repository"})
#SpringBootApplication
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
Entity Class:
package com.example.demo.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.Id;
import lombok.Data;
#Entity(name = "admin")
#Data
public class Admin {
#Id
#GenericGenerator(name = "admin_id_seq" , strategy = "increment")
#GeneratedValue(generator="admin_id_seq",strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private String name;
}
Controller
package com.example.demo.controller;
import com.example.demo.dto.AdminRegisterDTO;
import com.example.demo.repository.AdminRepository;
import com.example.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class AdminController {
#Autowired
private AdminService adminService;
#PostMapping("/login")
public AdminRegisterDTO registerAdmin(#RequestBody AdminRegisterDTO adminRegisterDTO){
return adminService.registerAdmin(adminRegisterDTO);
}
}
Service class
package com.example.demo.service.impl;
import com.example.demo.dto.AdminRegisterDTO;
import com.example.demo.entity.Admin;
import com.example.demo.repository.AdminRepository;
import com.example.demo.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class AdminServiceImpl implements AdminService {
#Autowired
private AdminRepository adminRepository;
#Override
public AdminRegisterDTO registerAdmin(AdminRegisterDTO adminRegisterDTO) {
Admin admin = new Admin();
BeanUtils.copyProperties(adminRegisterDTO,admin);
Admin savedAdmin = adminRepository.save(admin);
AdminRegisterDTO adminRegisterDTO1 = new AdminRegisterDTO();
BeanUtils.copyProperties(savedAdmin,adminRegisterDTO1);
return adminRegisterDTO1;
}
}
Repository:
package com.example.demo.repository;
import com.example.demo.entity.Admin;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface AdminRepository extends JpaRepository<Admin, Long> {
}
If I remove #ComponentScan I get the following error message:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field adminRepository in com.example.demo.service.impl.AdminServiceImpl required a bean of type 'com.example.demo.repository.AdminRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.demo.repository.AdminRepository' in your configuration.

Spring book throws Field postRepository in required a bean of type 'repository.PostRepository' that could not be found

I am aware that there are a lot of questions related to the same but none of them worked for some reason. Hence, posting the same and hoping to get some response.
I am trying to follow the SpingBoot application tutorial from Youtube and build the same application. But for some reason I get following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field postRepository in com.testingconverter.service.PostService required a bean of type 'com.testingconverter.repository.PostRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.testingconverter.repository.PostRepository' in your configuration.
Following are my classes:
Main SpringBootApplication class:
package com.testingconverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
public class TestingConverterApplication {
public static void main(String[] args) {
SpringApplication.run(TestingConverterApplication.class, args);
}
}
Controller class:
package com.testingconverter.controller;
import com.testingconverter.entities.PostEntity;
import com.testingconverter.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
public class BlogController {
#Autowired
private PostService postService;
#GetMapping(value = "/")
public String index() {
return "index";
}
#GetMapping(value = "/posts")
public List<PostEntity> posts() {
return postService.getAllPost();
}
#PostMapping(value = "/posts")
public void publishPost(#RequestBody PostEntity post) {
postService.insert(post);
}
}
My Entity class:
package com.testingconverter.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;
#Entity
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#ToString
public class PostEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String title;
private String body;
private Date date;
}
My Repository class:
package com.testingconverter.repository;
import com.testingconverter.entities.PostEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface PostRepository extends JpaRepository<PostEntity, Long> {
}
My Service class:
package com.testingconverter.service;
import com.testingconverter.entities.PostEntity;
import com.testingconverter.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
#Service
public class PostService {
#Autowired
private PostRepository postRepository;
public List<PostEntity> getAllPost() {
return postRepository.findAll();
}
public void insert(PostEntity post) {
postRepository.save(post);
}
}
I tried many things like adding the #ComponentScan etc but nothing seems to work for me. Can someone please explain to me what's going wrong here? How can I fix this?
Following is my project structure:
You excluding default database configuration. Remove that exclude part and add your database configuration in application properties files.
If you going to exclude then you have to create you datasource in configuration file.
#Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder =
DataSourceBuilder.create();
dataSourceBuilder.driverClassName("");
dataSourceBuilder.url("");
dataSourceBuilder.username("");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}

Spring boot does not generate the json and shows me the white page

I've done a simple list with sql server, but it doesn't show me the result, it just shows me the white page.
I have no error in the console. Will the sql server 2017 be? please any help?
connection to sql server 2017
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=DB_PRUEBA_V1;integratedSecurity=true
spring.jpa.show-sql=true
#spring.jpa.hibernate.ddl-auto=update
server.port=8090
package app
package app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ProyectoV2Application {
public static void main(String[] args) {
SpringApplication.run(ProyectoV2Application.class, args);
}
}
package model
package model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "TB_USERS")
public class Users implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "COD_USER")
private int codUser;
#Column(name = "NOM_USER")
private String nomUser;
#Column(name = "EMA_USER")
private String emUser;
public int getCodUser() {
return codUser;
}
public void setCodUser(int codUser) {
this.codUser = codUser;
}
public String getNomUser() {
return nomUser;
}
public void setNomUser(String nomUser) {
this.nomUser = nomUser;
}
public String getEmUser() {
return emUser;
}
public void setEmUser(String emUser) {
this.emUser = emUser;
}
}
package repository
package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import model.Users;
#Repository
public interface UserDAO extends JpaRepository<Users, Integer>{
}
service
package service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import model.Users;
import repository.UserDAO;
#Service
public class UserService {
#Autowired
private UserDAO dao;
public List<Users> lista(){
return dao.findAll();
}
}
package controller
package controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import model.Users;
import service.UserService;
#RestController
#RequestMapping(value = "/CrudUsers")
public class UserController {
#Autowired
private UserService us;
#ResponseBody
#GetMapping(path = "/lista", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Users> lista(){
return us.lista();
}
}
Whitelabel Error Page
Because you created each class in different package like below,
app --> ProyectoV2Application
model --> Users
repository --> UserDAO
service --> UserService
controller --> UserController
But spring boot scans the classes that are either in root package or in sub package of root package, so move all these classes into sub packages of root package
app --> ProyectoV2Application
app.model --> Users
app.repository --> UserDAO
app.service --> UserService
app.controller --> UserController

Added #Transactional into a test to avoid org.hibernate.LazyInitializationException no Session error. Why is it needed?

I annotated a test method with #Transactional to avoid:
org.hibernate.LazyInitializationException: could not initialize proxy [com....OrderEntity#6def569a-ebf2-473e-b1b1-8b67e62fd17d] - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:169)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:309)
at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:45)
at org.hibernate.proxy.ProxyConfiguration$InterceptorDispatcher.intercept(ProxyConfiguration.java:95)
at com...orders.OrderEntity$HibernateProxy$wwLGAOuY.getDescription(Unknown Source)
I do not know why it is needed and wonder whether my application configuration is correct.
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import java.util.UUID;
#Entity
#Table(name = "orders")
#Getter
#Setter
public class OrderEntity {
#Id
#GeneratedValue
private UUID uid;
private Date created;
private Date updated;
private String description;
}
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
#Repository
public interface OrderRepository extends JpaRepository<OrderEntity, UUID> {
List<OrderEntity> findByDescription(String description);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.UUID;
#Service
#Transactional
public class OrderService
{
private OrderRepository repository;
#Autowired
public OrderService(OrderRepository repository) {
this.repository = repository;
}
public List<OrderEntity> findAll() {
return repository.findAll();
}
public OrderEntity save(OrderEntity order) {
return repository.save(order);
}
public OrderEntity getOne(UUID uid) {
return repository.getOne(uid);
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.assertEquals;
#RunWith(SpringRunner.class)
#SpringBootTest
public class OrderServiceTest {
#Autowired
private OrderService service;
#Test
#Transactional
public void testSave() {
OrderEntity order = new OrderEntity();
order.setDescription("Order description");
OrderEntity saved = service.save(order);
System.out.println(saved.getDescription());
OrderEntity persisted = service.getOne(saved.getUid());
// throws LazyInitializationException without #Transactional
System.out.println(persisted.getDescription());
assertEquals(persisted.getDescription(), order.getDescription());
}
}
I even added #EnableTransactionManagement but it makes no difference:
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#Configuration
#EnableTransactionManagement
public class PersistenceJPAConfig {
}
The difference between getOne and findOne is that the first always returns a lazy proxy, even if there is no actual row in the database. The lazy proxy needs an open EntityManager to operate on. However as your test method doesn't run in a single transaction the EntityManager will be closed as soon as the getOnemethod ends.
Without an open EntityManager calls on the object will fail as it cannot retrieve the values from the database anymore.
To solve use findOne instead of getOne OR make your test method transactional. The latter however has some other effects on your test-case (it will return the same object from the findOne call as it will also reuse a single EntityManager).

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'voteService'

I am new to spring, trying to create a spring project with mongodb. I get the following error when I run it.
2018-05-23 22:15:20.077 WARN 11610 --- [main]
ConfigServletWebServerApplicationContext : Exception encountered during
context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'voteController': Unsatisfied dependency
expressed through field 'voteSvc'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'voteService': Unsatisfied dependency expressed
through field 'voteRepo'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'votesRepository': Invocation of init method failed;
nested exception is
org.springframework.data.mapping.PropertyReferenceException: No
property findAll found for type Vote!
2018-05-23 22:15:20.081 INFO 11610 --- [main]
o.apache.catalina.core.StandardService : Stopping service [Tomcat]
Here my Application.java file
package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#SpringBootApplication
#ComponentScan("com.hello")
public class Application implements CommandLineRunner {
#Autowired
private UserRepository userRepo;
private IdeaRepository ideaRepo;
private VotesRepository voteRepo;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void run(String... args) throws Exception {
}
}
Here is my VoteController.java
package com.hello;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.*;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.boot.autoconfigure.*;
#Controller
#RequestMapping("/api")
public class VoteController{
#Autowired
IVoteService voteSvc;
#RequestMapping(value = "/votes", method = RequestMethod.GET)
public List<Vote> findByIdeaId(int ideaId)
{
List<Vote> votes = voteSvc.GetVotesByIdeaId(ideaId);
return votes;
}
#RequestMapping(value = "/votes", method = RequestMethod.POST)
public Vote PostByIdeaId(Vote vote)
{
Vote votes = voteSvc.SaveVotesByIdeaId(vote);
return votes;
}
#RequestMapping(value = "/votes", method = RequestMethod.DELETE)
public void DeleteByIdeaId(Vote vote)
{
voteSvc.DeleteVotesByIdeaId(vote);
}
}
Here is VoteService.java
package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import java.util.List;
import java.util.*;
#Service
public class VoteService implements IVoteService {
#Autowired
VotesRepository voteRepo;
public List<Vote> GetVotesByIdeaId(int ideaId)
{
List<Vote> votes = voteRepo.FindAll(ideaId);
//List<Vote> votes = new ArrayList<Vote>();
return votes;
}
public Vote SaveVotesByIdeaId(Vote vote)
{
Vote voteIdea = voteRepo.save(vote);
return voteIdea;
}
public void DeleteVotesByIdeaId(Vote vote)
{
voteRepo.delete(vote);
}
}
Here is my VoteRepository.java
package com.hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
#Repository
public interface VotesRepository extends MongoRepository<Vote, String> {
public List<Vote> FindAll(int IdeaId);
}
Here is my Interface
package com.hello;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.*;
#Service
public interface IVoteService {
public List<Vote> GetVotesByIdeaId(int ideaId);
public Vote SaveVotesByIdeaId(Vote vote);
public void DeleteVotesByIdeaId(Vote vote);
}
Here is my gradle.build file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'idea-platform'
version = '0.1.0'
}
jar {
manifest {
attributes(
'Main-Class': 'hello.Application'
)
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-data-mongodb")
testCompile("junit:junit")
}
Here is my entity
package com.hello;
import org.springframework.data.annotation.Id;
public class Vote {
#Id
public String id;
public String user;
public Integer ideaId;
public Vote() {}
}
I looked into other similar questions, none of them helped me to resolve.
The method you define on your repository doesn't match the Spring Data Repositories approach. Supposing ideaId is a property on Vote, a method to find all Vote's with the given ideaId should be something like this:
#Repository
public interface VotesRepository extends MongoRepository<Vote, String> {
public List<Vote> findByIdeaId(int ideaId);
}
Have a look here for more details on how to implement Spring Data Repositories.
P.S.: On a side note, try adhering to the Java naming convention to start your method and parameter names with a lowercase.
This is not the full solution, but I noticed this issue too. Each of your repositories probably need to be autowired, not just the first one.
You had:
#Autowired
private UserRepository userRepo;
private IdeaRepository ideaRepo;
private VotesRepository voteRepo;
But I think you wanted:
#Autowired
private UserRepository userRepo;
#Autowired
private IdeaRepository ideaRepo;
#Autowired
private VotesRepository voteRepo;

Categories