在Spring Data JPA中将ExampleMatcher与@embeded实体一起使用时出错

时间:2018-07-07 22:31:58

标签: java spring spring-data-jpa spring-data-commons

我在spring数据jpa中有一个带有@embeded对象的实体,

在查询示例匹配器时,出现以下错误:

java.lang.IllegalArgumentException: address.mylocation.country 的意外路径类型。找到org.hibernate.query.criteria.internal.path.SingularAttributePath@2d0c22c6,其中应包含From.class。     在org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicates(QueryByExamplePredicateBuilder.java:142)〜[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

我的源代码在这里:https://github.com/harsh-hardaha/springboot-h2-example

Product.java

package com.harsh.springframework.domain;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import java.math.BigDecimal;


@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long _id;
private String description;
private BigDecimal price;
private String imageUrl;

@ManyToOne
@JoinColumn(name="addressId")
private Address address;

public Long getId() {
    return _id;
}

public void setId(Long id) {
    this._id = id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public BigDecimal getPrice() {
    return price;
}

public void setPrice(BigDecimal price) {
    this.price = price;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

/**
 * @return the address
 */
public Address getAddress() {
    return address;
}

/**
 * @param address the address to set
 */
public void setAddress(Address address) {
    this.address = address;
}


}

Address.java

package com.harsh.springframework.domain;


import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class Address {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String building;
private String street;

@Embedded
private Location mylocation;

/**
 * @return the id
 */
public Long getId() {
    return id;
}
/**
 * @param id the id to set
 */
public void setId(Long id) {
    this.id = id;
}
/**
 * @return the building
 */
public String getBuilding() {
    return building;
}
/**
 * @param building the building to set
 */
public void setBuilding(String building) {
    this.building = building;
}
/**
 * @return the street
 */
public String getStreet() {
    return street;
}
/**
 * @param street the street to set
 */
public void setStreet(String street) {
    this.street = street;
}
/**
 * @return the mylocation
 */
@Embedded
public Location getMylocation() {
    return mylocation;
}
/**
 * @param mylocation the mylocation to set
 */
public void setMylocation(Location mylocation) {
    this.mylocation = mylocation;
}
}

Location.java

package com.harsh.springframework.domain;


import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


@Embeddable
public class Location {

private String city;
private String zip;



@ManyToOne
@JoinColumn(name="countryId")
private Country country;


/**
 * @return the city
 */
public String getCity() {
    return city;
}
/**
 * @param city the city to set
 */
public void setCity(String city) {
    this.city = city;
}
/**
 * @return the zip
 */
public String getZip() {
    return zip;
}
/**
 * @param zip the zip to set
 */
public void setZip(String zip) {
    this.zip = zip;
}
/**
 * @return the country
 */
public Country getCountry() {
    return country;
}
/**
 * @param country the country to set
 */
public void setCountry(Country country) {
    this.country = country;
}


}

Country.java

package com.harsh.springframework.domain;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class Country {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
/**
 * @return the id
 */
public Long getId() {
    return id;
}
/**
 * @param id the id to set
 */
public void setId(Long id) {
    this.id = id;
}
/**
 * @return the name
 */
public String getName() {
    return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}
}

ProductServiceImpl

package com.harsh.springframework.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.harsh.springframework.domain.Address;
import com.harsh.springframework.domain.Country;
import com.harsh.springframework.domain.Location;
import com.harsh.springframework.domain.Product;
import com.harsh.springframework.repositories.ProductRepository;

@Service
public class ProductServiceImpl implements ProductService {

@Autowired
private ProductRepository productRepository;

@Override
public Page<Product> searchProductWithError() {
    Pageable pageable = new PageRequest(0, 4);

    Product product = new Product();
    product.setDescription("top");
    Address adress = new Address();
    Location location = new Location();

    Country country = new Country();
    location.setCountry(country);
    country.setName("france");
    adress.setMylocation(location);
    product.setAddress(adress);

    //product.setDescription("top");
    ExampleMatcher exampleMatcher = ExampleMatcher.matching();


    exampleMatcher = ExampleMatcher.matching()
            .withIgnoreNullValues()
            .withIgnoreCase()
            .withMatcher("address.mylocation.country.name", match -> 
match.contains());  

    exampleMatcher = exampleMatcher.withIgnoreNullValues()
            .withIgnoreCase()
            .withMatcher("description", match -> match.contains()); 



    Example<Product> example = Example.of(product, exampleMatcher);

    Page<Product> page = productRepository.findAll(example, pageable);



    return page;
}
}

ProductRepository.java

package com.harsh.springframework.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import com.harsh.springframework.domain.Product;


public interface ProductRepository extends JpaRepository<Product, Long> {
}

0 个答案:

没有答案