Enums using toString()?

125 views
Skip to first unread message

Patrick Lightbody

unread,
Sep 20, 2019, 2:35:26 PM9/20/19
to jd...@googlegroups.com
I'm finding that in some cases my enums are being inserted into the database using their toString() value rather than their name or ordinal. The docs don't talk about this much and as I'm debugging the code it seems like it might be only for enums that are fields on on a bean that I passed in with bindBean(). It does this even when I explicitly added @EnumByName to those fields on the bean's class.

Does that sound plausible? Any suggestions for how to resolve this?

Patrick

Steven Schlansker

unread,
Sep 20, 2019, 7:46:01 PM9/20/19
to jd...@googlegroups.com
Hi Patrick,
I'm not able to reproduce this behavior with a simple test case:

@Test
public void enumBinding() {
handle.execute("create table bean (value varchar)");
handle.createUpdate("insert into bean(value) values(:value)")
.bindBean(new EnumBean())
.execute();
assertThat(handle.createQuery("select value from bean").mapTo(String.class).one())
.isEqualTo(EnumType.NAME.name());
}

public class EnumBean {
public EnumType getValue() {
return EnumType.NAME;
}
}

public enum EnumType {
NAME {
@Override
public String toString() {
return "toString";
}
}
}

Can you share a simple reproduction test case, or some more detail on exactly when this happens?
Thanks.
> --
> You received this message because you are subscribed to the Google Groups "jDBI" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jdbi/CALzbjpZ4QbJah%3DRvOsHsFhDeukN5_GwJgd01PFHmJ%2B6RAF44_w%40mail.gmail.com.

Patrick Lightbody

unread,
Sep 20, 2019, 7:49:07 PM9/20/19
to jd...@googlegroups.com
I'll try to get you something soon. For now I'm working around it by specifically binding the enum like so:

bindBean(bean).bind("enum, bean.getEnum())

But that's a bit silly =P

One thing I'll note: I'm using Postgres + Hikari. Based on my debugging, it looks like the enum gets passed all the way down to the JDBC driver's Statement class, so perhaps it's a database-specific issue?

Patrick


Steven Schlansker

unread,
Sep 20, 2019, 7:57:04 PM9/20/19
to jd...@googlegroups.com
Indeed, the PostgresPlugin installs the TypedEnumArgumentFactory which binds enums directly and lets the driver handle it.
I think this behavior predates the pluggable enum handling in core, and we probably just didn't re-consider it when we extended the core enum handling.

I can see how that behavior is more than a bit surprising... probably worth filing an issue to figure out the appropriate fix.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jdbi/CALzbjpZnDYo_c5VssPNWpUfoc0ts-GBKjvKzrMGsTuFU4ehPcw%40mail.gmail.com.

Patrick Lightbody

unread,
Sep 20, 2019, 8:00:33 PM9/20/19
to jd...@googlegroups.com
The docs for the Postgres module's TypedEnumArgumentFactory say:

/**
 * Default {@code jdbi} behavior is to bind {@code Enum} subclasses as
 * a string, which Postgres won't implicitly convert to an enum type.
 * If instead you bind it as {@code java.sql.Types.OTHER}, Postgres will
 * autodetect the enum correctly.
 */

Which suggests that it believes it should be converted using name() instead of toString()?

Reply all
Reply to author
Forward
0 new messages