chk.buddi
chk.buddi

Reputation: 603

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property:

In my spring-boot application, I have used 'Brand' and 'Family' entity objects which have One to many relationship.

 @Entity
@Table(name = "family")
public class Family extends UserDateAudit {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @NotBlank
        @Size(max = 140)
        private String name;

        @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
        @ManyToOne
        @JoinColumn(name = "brand_id")
        private Brand brand;

I need to create a method 'findByBrandIdIn(). So I used following code in my repository class.

@Query("SELECT a FROM Family a WHERE a.brand_id=:brandId")
    Page<Family> findByBrandIdIn(@Param("brandId") String brand_id, Pageable pageable);

However, it doesn't allow me to add brand_id as it does not contain in 'Family' entity class.

How do I use both 'private Brand brand' and private Long brand_id in my 'Family' class? Or is there a way to bind brand_id to

@Query("SELECT a FROM Family a WHERE a.brand_id=:brandId")

Upvotes: 0

Views: 5091

Answers (1)

Raghavendra Prasad
Raghavendra Prasad

Reputation: 36

You have to traverse the entity using the object reference like below

@Query("SELECT a FROM Family a WHERE a.brand.brandId=:brandId")

Assuming you have below mapping on your Brand Entity for the Id mapping

@Id
@Column("brand_id")
private long brandId;

In JPA we are querying the objects and use the instance variable (brandId) for referencing using . not the mapped column name (brand_id)

Upvotes: 1

Related Questions