Skip to content

Allow to use Java DTOs matching the Hibernate field mapping in query parameters #778

@victornoel

Description

@victornoel

Hi, as discussed in #774, the following test does not pass:

@Test
    public void test() {

        doInJPA(entityManager -> {
            entityManager.persist(
                new Book()
                    .setIsbn("978-9730228236")
                    .setProperty("my", "property")
            );
        });

        doInJPA(entityManager -> {
            Book book = entityManager.unwrap(Session.class)
                .createSelectionQuery("SELECT b from Book b WHERE property = :b", Book.class)
                .setParameter(
                    "b",
                    new Property("my", "property")
                )
                .getSingleResult();

            assertEquals(
                "978-9730228236",
                book.getIsbn()
            );
        });
    }

    @Entity(name = "Book")
    @Table(name = "book")
    public static class Book {
        // ....
        @Type(JsonType.class)
        private Property property;
        // ....
    }

Two relevant points IMO:

  • I shouldn't have Property to implement Serializable for this to work
    • jackson knows how to convert this to json so that it can be sent to the db, I don't see why the execution path should go through java serialization
    • also I don't always have the control on the DTOs (well in my very particular case I do, but it could happen that someone don't?)
  • More implementation-oriented and performance related
    • this use case is very similar to the support of List/Map as they were recently implemented: a List, a Map or a random Java class have the same semantics in this context, they are just a DTO that needs to be converted to json before being sent to the db
    • I don't see why the execution path, that goes through JsonJavaTypeDescriptor.wrap() requires an extra conversion through string before reconstructing the original object. It's basically doing a deep copy and that seems useless.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions