Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ColumnName just working on val, not var properties intentional? #1207

Closed
jmbremer opened this issue Aug 8, 2018 · 8 comments
Closed

@ColumnName just working on val, not var properties intentional? #1207

jmbremer opened this issue Aug 8, 2018 · 8 comments
Labels

Comments

@jmbremer
Copy link

jmbremer commented Aug 8, 2018

As the title says: Am I missing something in finding @ColumnName to work fine on val properties (within a data class in this case), but being completely ignored when switching the same property over to var? What's the hint in the documentation that I am noobishly missing? Thank you!

@qualidafial
Copy link
Member

Just to be clear, we're talking about the Kotlin mapper, correct?

@jmbremer
Copy link
Author

jmbremer commented Aug 8, 2018

Yes, all Kotlin including the mapping. The example being a

data class Xyz(
  ...
  @ColumnName("surchargepct") var surchargePercent: BigDecimal,
  ...
)

The column name in the (PostgreSQL 9.6) database is surchargePct. I tried various name variations on the database side and in the column mapping. Nothing fixes this...

java.lang.IllegalArgumentException: Member 'surchargePercent' of class 'Contract has no column in the result set. Verify that your result set has the columns expected, or annotate the property explicitly with @ColumnName

..except turning the var into a val.

The odd thing is that a separate test setup within the same project has this working with either val or var:

data class Foo(@ColumnName("f_no") var no: Int, var name: String)

If this is not supposed to happen, can it have to do with the BigDecimal type?

@jmbremer
Copy link
Author

jmbremer commented Aug 9, 2018

IGNORE THIS COMMENT, IT'S OBSOLETE, SEE @Nunobpinto'S COMMENT BELOW.

Notice the test I set up within the same project as above.

CREATE TABLE Foo(
  no    Int PRIMARY KEY,
  name  Varchar);

CREATE TABLE Bar(
  fooNo Int,
  no    Int,
  note  Varchar,
  PRIMARY KEY (fooNo, no),
  FOREIGN KEY (fooNo) REFERENCES Foo);

INSERT INTO Foo VALUES
    (1, 'Steve'),
    (2, 'Bob');

INSERT INTO Bar VALUES
    (1, 1, 'Apple'),
    (1, 2, 'Turtle'),
    (2, 1, 'Banana');
data class Foo(@ColumnName("f_no") var no: Int, var name: String)

data class Bar(val no: Int, var note: String)

interface BarDAO {

    @RegisterJoinRowMapper(Foo::class, Bar::class)
    @SqlQuery("""
        SELECT f.no AS f_no, f.name, b.*
        FROM Foo f JOIN Bar b ON b.fooNo = f.no
        WHERE b.fooNo = :fooNo AND b.no = :no
        """)
    fun findBarWithFoo(fooNo: Int, no: Int): JoinRow
}

This works fine:

val b = barDAO.findBarWithFoo(1, 2)
val foo = b.get(Foo::class.java)
val bar = b[Bar::class.java]
logger.debug { "Foo = $foo, bar = $bar" }

Versions:

  • Kotlin: 1.2.51
  • JDBI: 3.3.0
  • PostgreSQL: 42.1.4
  • (Dropwizard: 1.3.5)

@Nunobpinto
Copy link

I have the exact same problem. Regarding why there's no error when you try to map this class:

data class Foo(@ColumnName("f_no") var no: Int, var name: String)

That's because the property no has the same name as the column no of your Foo table, so the @ColumnName is ignored but it's still successfully mapped because the names are the same, I tested this as well.

@ivanfeli
Copy link

ivanfeli commented Nov 14, 2018

I fixed most of my cases by registering my row mappers with FieldMapper.factory(...) and using @field:ColumnName("...") in my data classes. Works fine with both var and val for me.

However this is only when I am creating queries within code and not in a Dao interface, I've had weird results when using Dao's.

@leaumar leaumar added the bug label Nov 23, 2018
@leaumar
Copy link

leaumar commented Jan 27, 2019

@ivanfeli so could you provide a code sample where jdbi fails or is there no more issue?

@qualidafial
Copy link
Member

I've tried this in local testing and have yet to find a failing case.

@jmbremer Could you provide a standalone example that demonstrates the problem?

@qualidafial
Copy link
Member

I can't reproduce the error reported and #1506 adds test validating those findings.

Please feel free to open a new issue if this is still a problem--preferably with a test case that we can use to reproduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

5 participants