Hi Lukas, and thanks for your reply. This helps a lot.

I used jOOQ In the past with code generation and I understand the benefits
especially in terms of type information and type safety.

In my case I'm not having a dynamic schema, but also not an active database
(I need to generate create table statements + insert statements).
After reading your blog post I found about the DDLDatabase which gives me
the possibility to generate classes after all, which is very nice.

So I came up with this:

        Continent continent = new Continent();
        continent.setId("europe");
        continent.setAlphaCode("eu");
        continent.setName("Europe");

        ContinentRecord continentRecord = ctx.newRecord(CONTINENT,
continent);

        String insert = ctx
                .insertInto(CONTINENT)
                .set(continentRecord)
                .getSQL(ParamType.INLINED);


Notice the `alphaCode` in my Continent which is just `code` in the table.

The generated statement then becomes:

insert into continent (id, name) values ('europe', 'Europe')

because it cannot map the alphaCode/code it just omits it...

I also tried with:

        String insert = ctx
                .insertInto(CONTINENT)
                .columns(CONTINENT.fields())
                .values(continentRecord)
                .getSQL(ParamType.INLINED);

but it the just supplies a null value when it cannot find a mapping:

insert into continent (id, code, name) values ('europe', null, 'Europe')

In terms of type safety, would it be possible to fail (exception) when
there is no default mapping available?

And would there be a way to implement a RecordUnmapper where one only needs
to map fields that do not match automatically?
Or would it be better to use another tool - like MapStruct - to map from
Continent POJO to ContinentRecord?


Thanks,
Marcel



On Mon, Mar 18, 2024 at 7:54 AM Lukas Eder <lukas.e...@gmail.com> wrote:

> Hi Marcel,
>
> The SQL DSL can't do that for you, but you can turn any POJO into a Record
> using Record.from() or DSLContext.newRecord(...). You can then use
> .insertInto(table).set(record). There's a RecordUnmapper SPI that governs
> how to "unmap" POJOs into records, with a DefaultRecordUnmapper
> implementation that works just like the DefaultRecordMapper in the other
> way. Use this SPI in case you need to override the defaults.
>
> Note, I always point out the benefits of using the code generator to folks
> who don't:
> https://blog.jooq.org/why-you-should-use-jooq-with-code-generation/
>
> The main reason not to use the code generator is because the schema is
> dynamic. If it isn't, you'll get tons of benefits from providing type
> information to jOOQ (and work around tons of problems).
>
> I hope this helps,
> Lukas
>
> On Mon, Mar 18, 2024 at 7:48 AM Marcel Overdijk <marceloverd...@gmail.com>
> wrote:
>
>> I'm currently using JDBI on a project with a DAO like:
>>
>> @SqlBatch("insert_continent")
>> fun insertContinents(@BindBean continents: List<Continent>)
>>
>> Note this uses the @BindBean to automatically bind bean properties to sql
>> statements.
>>
>> E.g. the insert_continent sql file contains:
>>
>> INSERT INTO continent
>> ( id
>> , code
>> , name
>> )
>> VALUES
>> ( :id
>> , :code
>> , :name
>> );
>>
>> and the bean is Java class like:
>>
>> public class Continent implements Serializable {
>>
>>     private String id;
>>     private String code;
>>     private String name;
>>
>> *    // getters + setters*
>> *}*
>>
>> Now with jOOQ I want to do something similar, e..g having an insert like:
>>
>> val continent = .. // some continent
>> val query = ctx
>>     .insertInto(
>>         table("continent"),
>>         field("id"),
>>         field("code"))
>>         field("name"))
>>     .values(
>>         continent.id,
>>         continent.code,
>>         continent.name
>>     )
>>
>> But then instead of access all properties by hand, just bind the bean.
>>
>> Would that be possible with jOOQ?
>>
>> Note: I do not want to use code generation for this.
>> Also the Continent class is provided and cannot be changed.
>>
>>
>> Thanks,
>> Marcel
>>
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "jOOQ User Group" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to jooq-user+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/jooq-user/6eca1697-beef-4c63-921a-757cdcf0d8a7n%40googlegroups.com
>> <https://groups.google.com/d/msgid/jooq-user/6eca1697-beef-4c63-921a-757cdcf0d8a7n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "jOOQ User Group" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/jooq-user/At5WzUp-1Po/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> jooq-user+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/jooq-user/CAB4ELO5JqTBn1hKXcMVrCzHcFqodY%3D1zF0Xub6XW0J-ReAx_yg%40mail.gmail.com
> <https://groups.google.com/d/msgid/jooq-user/CAB4ELO5JqTBn1hKXcMVrCzHcFqodY%3D1zF0Xub6XW0J-ReAx_yg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups "jOOQ 
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jooq-user+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jooq-user/CAGXs_qR_4hMvbW90Jh1R0%3D25ag57CGwLFYqPT3%2B%3D-tPymbYPew%40mail.gmail.com.

Reply via email to