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

时间:2019-03-20 18:13:05

标签: java spring mongodb spring-boot jpa

我决定将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;

   }

我已添加了所需的MongoDB注释,如app config java类中所述:https://spring.io/guides/gs/accessing-data-mongodb/

并为Spring Data定义了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在服务类中添加了仓库,并试图运行该应用程序。但是,由于与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>

1 个答案:

答案 0 :(得分:0)

我已经意识到,通过添加

可以解决问题。
@SpringBootApplication
@EnableMongoRepositories(basePackages = "com.github.eljah.saylaw.mongo")
@EnableJpaRepositories(basePackages =  "com.github.eljah.saylaw.repository")

进入appconfig java类。

在此修复程序之前,我只有@EnableMongoRepositories,对于JPA没有任何特定的东西,因为最初它仅在开始迁移之前才与JPA一起使用。