简体   繁体   中英

Problem on using both JPA and MongoDB annotations in the model class in Spring Boot app

I decided to migrate the JPA app to the MongoDB and at some time the models turned into classed annotated with both JPA and MongoDB annotations like this:

import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.solr.core.mapping.Indexed;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by eljah32 on 4/22/2018.
 */

@Entity
@Setter
@Getter
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = {"name"})
@ToString(exclude = {"ownerShare", "shareVotes"})
@Slf4j
@Document
public class Share {
    @Id
    @GeneratedValue
    //private Long id;
    private String id;

    @Version
    private Long version;

    @Indexed
    @Access(AccessType.PROPERTY)
    private String name;

    @Column(unique = true)
    public void setName(String name) {
        //log.info("Setter Name is called");
        this.name = name;
        try {
            this.setNumber(number = name != null ? Integer.parseInt(name.replaceAll("[^0-9]", "")) : null);
        }
        catch (java.lang.NumberFormatException e)
        {
            this.setNumber(0);
        }
    }



    @Access(AccessType.PROPERTY)
    private Integer number;

    public void setNumber(Integer number) {
        if (number == null) {
        } else {
            log.trace(number + " number passed to store");
            this.number = number;
        }
    }

    private long shareNominator;
    private long shareDenominator;
    private double shareValue;

    @Indexed
    private Double area;
    private ShareType type;

    private int floor;

    @OneToMany(mappedBy = "share")
    private List<OwnerShare> ownerShare = new ArrayList<>();

    public enum ShareType {
        RESIDENTAL,
        NONRESIDENTAL
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "share")
    private List<ShareVote> shareVotes;

    private Boolean active;

   }

I have added the needed MongoDB annotations as described there in the app config java class: https://spring.io/guides/gs/accessing-data-mongodb/

and defined the MongoDB repository for the Spring Data like this (with the only expected behavior just to save the object):

import com.github.eljah.saylaw.model.Share;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.List;

/**
 * Created by eljah32 on 3/20/2019.
 */
public interface ShareMongoRepository extends MongoRepository<Share, String> {
//    List<Share> findByActiveIsTrue();
//    List<Share> findByActiveIsFalse();
//    List<Share> findAllByOrderByNumberAscNameAsc();
//    List<Share> findAllByOrderByNumberAsc();
//    List<Share> findAllByOrderByNameAsc();
}

I have added the repo through @Autowired in the service class and tried to run the app. However I'm getting my beans being not initialized due to this error that is related to the JPA:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property insert found for type Share!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:92)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:356)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:332)
    at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:285)
    at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:267)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:250)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:250)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:251)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:380)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:381)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:93)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:76)

Any thoughts?

PS. pom.xml parts containing versions:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

I have realized that the problem was fixed by adding

@SpringBootApplication
@EnableMongoRepositories(basePackages = "com.github.eljah.saylaw.mongo")
@EnableJpaRepositories(basePackages =  "com.github.eljah.saylaw.repository")

into the appconfig java class.

Before this fix I had only @EnableMongoRepositories and nothing specific for JPA as initially it was working with JPA only before I have started the migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM