I have problem with "selecting" values using CrudRepository.
For example:
I have Client class:
@Entity
public class Client {
@Id
private Long id;
@ManyToMany
Map<AttributeType, Attribute> map = new HashMap<>();
}
and AttributeType class:
@Entity
public class AttributeType {
@Id
private Long id;
@Column(unique = true)
private String name;
}
"Attribute" is abstract entity which have "subtypes" of String and Boolean (AttributeBoolean, AttributeString), and both have 2 fields ID, and VAL (which is String val or Boolean val depends on "className").
So, if I want to select client list which have "AttributeType" in map I can use:
public interface ClientRepository extends CrudRepository<Client, Long> {
@Query("select cli from Client cli where KEY(cli.map) = :attributeType ")
List<Client> selectByAttributeType(@Param("attributeType ") AttributeType attributeType );
}
But I have problem with "selecting clients which AttributeType equals x and that Attribute equals y".
I was trying to use:
@Query("select cli from Client cli \n" +
"where KEY(cli.map) = :attributeType \n" +
"and VALUE(cli.map).val = :val")
List<Client> selectByAttributeTypeAndParam(
@Param("attributeType") AttributeType attributeType, @Param("val") String val);
But it throws exception: Caused by: java.lang.IllegalArgumentException: Parameter value [xxxxxxxx] did not match expected type [java.lang.Boolean (n/a)].
So, the question is: Do you have any idea, how can I select Every Client where: AttributeType = x and value from map (for that AttributeType) = y ?
//edit... To better understund:
We have situation like this:
AttributeType at1 =... //does not matter what is it
AttributeType at2 =... //does not matter what is it
AttributeType at3 =... //does not matter what is it
Client c1 = new Client();
Map<AttributeType, Attribute> map1 = c1.getMap();
map1.put(at1, new AttributeString("123456789");
map1.put(at2, new AttributeBoolean(true);
Client c2 = new Client();
Map<AttributeType, Attribute> map2 = c2.getMap();
map2.put(at1, new AttributeString("111111111");
map2.put(at3, new AttributeBoolean(true);
So, i want CrudRepository function which take 2 parameters.
1) AttributeType 2) SomeValue (can be String or Boolean)
If this function looks like:
@Query("some query - no idea how to write it")
List<Client> selectByAttributeTypeAndParam(
@Param("attributeType") AttributeType attributeType, @Param("val") String val);
and if i run:
List<Client> someList = selectByAttributeTypeAndParam(at1, "12345679");
I want that someList have 1 record which is c1.
And if i have equivalent function for Boolean search and i run:
List<Client> someList = selectByAttributeTypeAndParam(at2, true);
Than i want that someList have both c1 and c2 records.