在Spring Boot应用程序中的模型类中同时使用JPA和MongoDB注释的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Spring Boot应用程序中的模型类中同时使用JPA和MongoDB注释的问题相关的知识,希望对你有一定的参考价值。

我决定将JPA应用程序迁移到MongoDB,并且在某些时候,模型变成了带有JPA和MongoDB注释的分类,如下所示:

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;

   }

我已经在app config java class:https://spring.io/guides/gs/accessing-data-mongodb/中添加了所需的MongoDB注释

并为此类Spring数据定义了MongoDB存储库(仅保存对象的唯一预期行为):

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

我已经在服务类中通过@Autowired添加了repo并尝试运行该应用程序。但是由于与JPA相关的错误,我的bean没有被初始化:

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)

有什么想法吗?

PS。包含版本的pom.xml部分:

<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>
答案

我已经意识到问题是通过添加修复的

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

进入appconfig java类。

在此修复之前,我只有@EnableMongoRepositories并没有特定于JPA,因为最初它只在我开始迁移之前使用JPA。

以上是关于在Spring Boot应用程序中的模型类中同时使用JPA和MongoDB注释的问题的主要内容,如果未能解决你的问题,请参考以下文章

多部分表单数据输入 Java 模型属性未在请求类中注入元素 - Spring Boot

如何检查spring boot应用程序在自动配置类中是不是有某个注释

Spring Boot 希望 @Component 类成为 @Configuration 类中的 @Bean

Spring Boot AnnotationConfigEmbeddedWebApplicationContext无法填充动态类中的自动声明的声明字段

内容类型'text/plain;charset = UTF-8'在RestController类中的spring boot中不支持错误

如何在Spring Boot控制器类中传递参数(app正在使用Spring Security)