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

Fixed error when inserting a row in the database with a primary key o… #79

Merged
merged 3 commits into from
Sep 12, 2022

Conversation

EduardoMGP
Copy link

Hello, I discovered the repository yesterday, but when I went to use it following the Readme I came across an error when generating a table with the @id with type long, after the generated table the RETURN_GENERATED_KEYS brings an object of type BigInteger and this triggers an exception
Could not write value into pojo. Property: id method: public void com.dieselpoint.norm.main.Pessoa.setId(long) value: 1 value class: class java.math.BigInteger
using both the method getId() and leaving the attribute public public long id;

    public static void main(String[] args) {
        String jdbc = "jdbc:mysql://localhost:3306/database";
        Database database = new Database();
        database.setPassword("");
        database.setUser("root");
        database.setJdbcUrl(jdbc);


        database.sql("drop table if exists pessoa").execute();
        database.createTable(Pessoa.class);
        Pessoa pessoa = new Pessoa();
        pessoa.peso = 32.00;
        pessoa.idade = 14;
        pessoa.nome = "Joana";
        database.insert(pessoa);

        pessoa = new Pessoa();
        pessoa.peso = 22.00;
        pessoa.idade = 10;
        pessoa.nome = "Pedro";

        database.insert(pessoa);

    }
}
@Table(name = "pessoa")
public class Pessoa {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    public String nome;
    public int idade;
    public double peso;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

}

this was my code that I used to test which triggered the error

I also tried leaving the type public BigInteger id; but i also have another exception
java.sql.SQLSyntaxErrorException: Incorrect column specifier for column 'id'

I'm not sure if this is the best way to fix the problem, like I said, I met the project yesterday, so that's the way I thought, I'm still studying this repository better.

…f type long from a table generated by `database.createTable(Class.class)`
@ccleve
Copy link
Member

ccleve commented Sep 11, 2022

Is this the same error as #62 and #55?

@EduardoMGP
Copy link
Author

yes it is the same error

@ccleve
Copy link
Member

ccleve commented Sep 12, 2022

Your solution is actually pretty good.

I was hoping they'd just fix the bug in the JDBC driver, but there has been no activity on the bug in two years (https://bugs.mysql.com/bug.php?id=101823), so it's time for a workaround.

I haven't time to test your code thoroughly. Have you tried it out?

@EduardoMGP
Copy link
Author

I ran some tests with several different variable types and everything seems to work

Yesterday I make a conditional to check if the type will be long or Long to avoid some kind of error when using int.
The type int and Integer have the same error, but I just fixed it for Long, do you think it would be interesting to do the same for type int?

would be something like this

if (value instanceof BigInteger) {
    if (prop.field.getType().equals(Long.TYPE) || prop.field.getType().equals(Long.class)) {
        value = ((BigInteger) value).longValue();
    } else if (prop.field.getType().equals(Integer.TYPE) || prop.field.getType().equals(Integer.class)) {
        value = ((BigInteger) value).intValue();
    }
}
prop.field.set(pojo, value);

if not needed, in my tests everything worked fine.

@ccleve
Copy link
Member

ccleve commented Sep 12, 2022 via email

@EduardoMGP
Copy link
Author

I'd add the Integer code only if you can produce a test case that fails

it returns the exception but int is a bad type to use as a primary key, so I don't think need Integer

Could not set value into pojo. Field: public int com.dieselpoint.norm.main.Pessoa.id value: 1

@ccleve ccleve merged commit 145947e into dieselpoint:master Sep 12, 2022
@hnwh224
Copy link

hnwh224 commented Nov 18, 2022

Where can I get the latest build with this issue fixed?

@ccleve
Copy link
Member

ccleve commented Nov 18, 2022

@mrmrmystery I just now released 1.0.6 with the fix. It should be up on Maven Central shortly. Sometimes it takes them a while.

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

Successfully merging this pull request may close these issues.

3 participants