Reputation: 89
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;
}
}
Upvotes: 1
Views: 7805
Reputation: 25146
I was not able to reproduce your problem but I believe its because you are trying to create @MongoRepository
with class annotated with @Entity
.
@MongoRepository requires a plain POJO class or class annotated with @Document.
See this:
package gt.demo64777660;
import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
interface MongoRepo extends MongoRepository<PollMongo, Integer> {
List<PollMongo> findByIsPublic(boolean isPublic);
}
//A: doesn't work
//interface MongoRepoFromJpaEntity extends MongoRepository<PollJpa, Integer> {
// List<PollJpa> findByIsPublic(boolean isPublic);
//}
interface JPARepo extends JpaRepository<PollJpa, Integer> {
List<PollJpa> findByIsPublic(boolean isPublic);
}
//@org.springframework.data.mongodb.core.mapping.Document(collection = "poll") //B: this is optional
@Data
class PollMongo {
// @org.springframework.data.annotation.Id //B: this is optional
int id;
String name;
boolean isPublic;
}
@Data
@javax.persistence.Entity
class PollJpa {
@javax.persistence.Id
int id;
String name;
boolean isPublic;
}
Dependencies (uses H2 and embedded mongo, no config required):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>gt.mobgo</groupId>
<artifactId>mongo-h2-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Upvotes: 2