Question

I have an interface that extends CrudRepository and implements a method with @Query annotation with the attribute nativeQuery setted to true. This method returns a list of an entity.

Example:

public interface MessageTemplateRepository extends CrudRepository<MessageTemplate, Integer> {
  @Query(nativeQuery = true, "select template.* from plan_granted_template granted join license license on granted.fk_plan = license.fk_plan join message_template template on granted.fk_message_template = template.id where license.fk_garage = ?2 and template.message_type = ?1")
  public List<MessageTemplate> findGrantedTemplatesByMessageTypeAndGarage(MessageType messageType, Garage garage);
}

The Garage has one License
The License has one Plan and Garage
The Plan has many MessageTemplate

The class License has a ManyToOne relation with Plan and OneToOne relation with Garage
(table license - columns fk_plan and fk_garage)

The class Plan has a ManyToMany relation with MessageTemplate
(table plan_granted_template - columns fk_plan and fk_message_template)

The class MessageTemplate has the atribute messageType
(table message_template - column message_type)

This method should return all the MessageTemplate entity found on query but always returns an empty list. Executing this query in mysql returns the correct result. Also looking at the Hibernate logs, the query is executed with the correct parameters, but an empty list is returned anyway.

I think that Spring executes the query but it's fail to convert the resultSet into an MessageTemplate instance.

Was it helpful?

Solution

You needed to pass the parameters as String and Integer. The Hibernate logs this way on both cases: [BasicBinder:83]: binding parameter [1] as [INTEGER] - 1 [BasicBinder:83]: binding parameter [2] as [VARCHAR] - WELCOME.

Not sure if your parameters MessageTemplate messageTemplate plays well with template.message_type = ?1, have you tried to pass String messageTemplate, I mean conversion between the messageTemplate and String needed in the query, try to add DEBUG to see the parameters send to the database

UPDATE And the end parameters in the method should be string and int, not the Classes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top