0

I have the following entity and I'm trying to use ExampleMatcher for simple queries:

@Entity(name="UserAccount")
@Table(name = "useraccount", catalog = "useraccount")
@Data
public class UserAccount implements Serializable
{

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

@Column(nullable = false, unique = true)
private String username;

@Column(nullable = false, unique = true)
private String mail;

private String password;

private boolean isEnabled;

private Timestamp credentialExpire;

private boolean isAccountNonLocked;

private boolean isSuspended;

private Timestamp accountExpire;
    
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}, fetch=FetchType.LAZY)
@JoinTable(name = "user_to_privileges", catalog = "useraccount",
        joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)},
        inverseJoinColumns = {@JoinColumn(name = "privilege_id",  referencedColumnName = "id", nullable = false)})
private Set<Privilege> privileges= new HashSet<>();


@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH}, fetch=FetchType.LAZY)
@JoinTable(name = "user_to_organizations", catalog = "useraccount",
        joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)},
        inverseJoinColumns = {@JoinColumn(name = "organization_id",  referencedColumnName = "id", nullable = false)})
private Set<Organization> organizations= new HashSet<>();


@OneToOne(mappedBy ="user", cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.REFRESH}, fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
@Setter(AccessLevel.NONE)
private UserRegister register;   

@OneToMany(mappedBy ="tokenId.user", cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.REFRESH}, fetch=FetchType.LAZY)
private Set<SecureToken> tokens = new HashSet<>();


@OneToOne(mappedBy ="user", cascade = {CascadeType.REMOVE, CascadeType.MERGE, CascadeType.REFRESH}, fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
private UserLogin login;   
//all the methods omitted from brevity

}

I create the Example matcher as follows:

UserAccount account= new UserAccount();
account.setUsername("John");
ExampleMatcher matcher = ExampleMatcher.matching()
                                           .withIgnoreCase()
                                           .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
   
Example<UserAccount> regExample = Example.of(account, matcher);
List<UserAccount> out =  repository.findAll(regExample);

Consider that a user with username "John" exists, but the output is always empty, no matter what parameter I fill.

Edit: this helps to find the solution: Are there any possible ways to ignore all paths of JPA Example Matcher. There is no way to automatically ignore primitive fields when not used?

Edit: Notice that I want to find all the UserAccount containing the specified strings in the selected fields. With other entities the configuration of ExampleMatcher works.

Discipulos
  • 423
  • 8
  • 23

0 Answers0