1

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: enter image description here

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;
    }

}
4
  • Can you show the Poll class?\ Commented Nov 10, 2020 at 22:29
  • Added poll class! Commented Nov 10, 2020 at 22:32
  • Poll is JPA entity class. MongoRepository is not compatible with @Entity. It should be @Document. Commented Nov 10, 2020 at 22:45
  • You are reading from a H2 database table and writing it to MongoDB database collection. What is the process that reads and writes? Commented Nov 11, 2020 at 6:31

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.