I want to save results from a poll when the status is set to "past". I have a H2 db and want to add a mongoDB that only stores the results. How do I go about implementing this with Spring boot?
As of now I have a poll program where you can create poll, edit polls, vote on polls etc.
I have added dependencies for mongodb, spring boot, spring mvc and H2 db etc.
This is my application properties:
spring.datasource.url=jdbc:h2:file:./db;AUTO_RECONNECT=TRUE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
#Mongo Config
spring.main.allow-bean-definition-overriding=true
spring.data.mongodb.database=db-mongo
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
I have tried making an interface mongoRepository:
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import project.dat250.entity.Poll;
import project.dat250.entity.User;
import java.util.List;
public interface ImongoRepository extends MongoRepository<Poll, Integer>, PagingAndSortingRepository<Poll, Integer> {
List<Poll> findByNameContainingIgnoreCase(@Param("name") String name);
List<Poll> findByUser(@Param("user") User user);
List<Poll> findByIsPublic(@Param("isPublic") boolean isPublic);
List<Poll> findByStatus(@Param("status") String status);
}
But I am getting hundreds of lines of errors after adding this repository:
Poll class:
@Data
@Entity
public class Poll {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Setter(AccessLevel.PROTECTED)
private int pollID;
private String name;
private String description;
private boolean isPublic;
private int voteGreen;
private int voteRed;
private String status;
private int timeLimit;
@ManyToOne
private User user;
@ManyToMany(cascade = CascadeType.PERSIST)
private List<User> usersVoted = new ArrayList<>();
public Poll() {
}
public Poll(String name, String description, boolean isPublic, int voteGreen, int voteRed, String status,
int timeLimit, User user) {
this.name = name;
this.description = description;
this.isPublic = isPublic;
this.voteGreen = voteGreen;
this.voteRed = voteRed;
this.status = status;
this.timeLimit = timeLimit;
this.user = user;
}
public void setUsersVoted(User userVoted) {
this.usersVoted.add(userVoted);
}
public void setUser(User user) {
this.user = user;
if (user != null)
user.setPolls(this);
}
public void setVoteRed(int red) {
this.voteRed += red;
}
public void setVoteGreen(int green) {
this.voteGreen += green;
}
public void setPublic(boolean isPublic) {
this.isPublic = isPublic;
}
}