1

I have an entity with @embeded object in spring data jpa,

While querying example matcher I am getting bellow error:

java.lang.IllegalArgumentException: Unexpected path type for address.mylocation.country. Found org.hibernate.query.criteria.internal.path.SingularAttributePath@2d0c22c6 where From.class was expected. at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicates(QueryByExamplePredicateBuilder.java:142) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]

My Source code is available here : 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> {
}
4

0 回答 0