spring-data-sqlite icon indicating copy to clipboard operation
spring-data-sqlite copied to clipboard

Issue with Boolean column

Open benjamin-dreux opened this issue 1 year ago • 10 comments

I've create a project with the following entity

public record Contact(
    @Id Long id,
    Boolean isOrganization
){};

I'm using it with a standard repository

@Component
public interface ContactRepository extends ListCrudRepository<Contact, Long> {

    public Optional<Contact> findById(Long id);
}

But then when fetching entites with this repository, contact have a null for the field, isOrganization.

benjamin-dreux avatar Nov 03 '24 12:11 benjamin-dreux

Given the way sqlite support boolean, I also assumed that the query the repo will natively return an integer. Which is the case, so I've added a converter with the two following classes

@Configuration
class JdbcConfiguration extends AbstractJdbcConfiguration {

    @Override
    protected List<?> userConverters() {
    	return List.of(new IntegerToBooleanConverter());
    }

}
@ReadingConverter
@Component
public class IntegerToBooleanConverter implements Converter<Integer, Boolean> {

  public Boolean convert(Integer source) {
    System.out.println("USING CONVERTER");
    System.out.println("converting %s to %s".formatted(source, Boolean.valueOf(Integer.valueOf(1).equals(source))));
    return Boolean.valueOf(Integer.valueOf(1).equals(source));
  }
}

As you can see, I've added log message to ensure my converter is called, and what it returns. It turns ou that my converter is called as expected, still the returned entity contains a null for isOrgnanization

benjamin-dreux avatar Nov 03 '24 12:11 benjamin-dreux

@benjamin-dreux I tried your code in the example project and it worked well. See https://github.com/komamitsu/spring-data-sqlite/blob/example-converter/example/src/main/java/org/komamitsu.springdata.sqlite.example/Main.java#L34-L39 and related code for details.

komamitsu avatar Nov 04 '24 14:11 komamitsu

In my case I was using record instead of class for my model Also I was using list repository instead of SQLite repos Maybe any of those are the root cause.

On November 4, 2024, Miguel Gamboa de Carvalho @.***> wrote:

@benjamin-dreux https://github.com/benjamin-dreux I tried your code in the example project and it worked well. See <https://github.com/komamitsu/spring-data-sqlite/blob/example- converter/example/src/main/java/org/komamitsu.springdata.sqlite.example/Main.java#L34- L39> and related code for details.

— Reply to this email directly, view it on GitHub <https://github.com/komamitsu/spring-data-sqlite/issues/53#issuecomment- 2454858957>, or unsubscribe <https://github.com/notifications/unsubscribe- auth/BL25EAJJ4LJMD3O6MDWLJJLZ6574PAVCNFSM6AAAAABRCVVXTGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJUHA2TQOJVG4>. You are receiving this because you were mentioned.Message ID: @.***>

benjamin-dreux avatar Nov 04 '24 23:11 benjamin-dreux

public interface ContactRepository extends ListCrudRepository<Contact, Long> {

According to this code, you didn't use SqliteRepository ?

It would be great if you isolate the cause by checking if your code works with the original Spring Data JDBC.

komamitsu avatar Nov 06 '24 13:11 komamitsu

ListCrudRepository is part of spring data jdbc now

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/ListCrudRepository.html

benjamin-dreux avatar Nov 09 '24 11:11 benjamin-dreux

I meant your code didn't seem to use Spring Data JDBC integration for SQLite. SqliteRepository needs to be extended by Repository interfaces to use the feature.

komamitsu avatar Nov 09 '24 14:11 komamitsu

I tryed and I still see an error in my case. The example you've build is using spring boot 2.7.6. In my cas I'm using 3.3.4 so we use distinct spring data jdbc version, maybe this is the root case of my issue.

benjamin-dreux avatar Nov 09 '24 21:11 benjamin-dreux

In the same spirit I've notied that I can't use LocalDate, since text storage will make something like this

SELECT request_deposit_date from file;
1730955600000

Seams like epoch with ms

I'm not sure if it's an issue with the driver or the dialect

benjamin-dreux avatar Nov 10 '24 05:11 benjamin-dreux

As I mentioned above, I don't think you encountered those issues with using Spring Data JDBC for SQLite, since your code didn't use SqliteRepository.

komamitsu avatar Nov 10 '24 06:11 komamitsu

I was using SqlRepository, not at first. What I was missing is the registration of the Converters.

Sorry for the false signal. Thanks for you patient and help.

Have a great day

benjamin-dreux avatar Nov 10 '24 12:11 benjamin-dreux