Home > Back-end >  Why is my JPA derived query case insensitive by default?
Why is my JPA derived query case insensitive by default?

Time:08-04

All materials I have read so far indicate that case sensitive is the default behavior for a JPA query, and you need to add IgnoreCase to add that feature to the query. However, when I run my findByName(String name) derived query, it is able to identify the "checking" and "savings" accounts without case. Do I have something setup that would cause this? How can I switch to case sensitive?

@Entity
@Table(name = "gl_account")
public class Account extends Entry {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @Column(name = "number", unique = true, nullable = false)
    @NotBlank
    private String number;

    @Column(name = "name", unique = true, nullable = false)
    @NotBlank
    private String name;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(
            name = "element_id",
            referencedColumnName = "id",
            foreignKey = @ForeignKey(name = "fk__gl_account__element_id"),
            nullable = false
    )
    @NotNull
    private Element element;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(
            name = "player_id",
            referencedColumnName = "id",
            foreignKey = @ForeignKey(name = "fk__gl_account__player_id")
    )
    private Player player;

    // constructors, setters, getters, equals, hashcode

}

@Repository
public interface AccountRepository extends PagingAndSortingRepository<Account,Integer> {

    Optional<Account> findByName(String name);
}
@Service
public class AccountService {

    private final AccountRepository REPO;

    @Autowired
    public AccountService(AccountRepository repo) {
        REPO = repo;
    }

    public Account findByName(String name) {
        return REPO.findByName(name).orElse(null);
    }
}
@DisplayName("can find one by name")
@ParameterizedTest
@ValueSource(strings = {
    "cHeCkInG",
    "sAvInGs",
    "Accounts Payable"
})
public void findByName_test1(String name) {
    Account account = svc.findByName(name);
    assertNotNull(account);
}
INSERT INTO gl_account
(number, name, element_id, player_id)
VALUES
('1000', 'Checking', 1, 1),
('1001', 'Savings', 1, 2),
('2000', 'Accounts Payable', 2, NULL),
('3000', 'Retained Earnings', 3, NULL),
('4000', 'Salary', 4, NULL),
('4001', 'Interest Income', 4, NULL),
('5000', 'Food', 5, NULL),
('5001', 'Entertainment', 5, NULL),
('6000', 'Unrealized Gain/Loss', 6, NULL),
('99', 'NINETY-NINE', 6, NULL);

CodePudding user response:

There are databases that compare case insensitive by default.

https://www.oreilly.com/library/view/mysql-cookbook/0596001452/ch04s10.html#:~:text=In summary, comparisons are case,, SET , or TEXT columns.

  • Related