1

I'm creating the REST Web API services, and I have some problem with @BindBean annotation when try to execute sql. So, I have mysql table:

DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `email` NVARCHAR(120) NOT NULL,
    `password` NVARCHAR(45) NOT NULL,
    `salt` NVARCHAR(24) NOT NULL,    
    `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
    CONSTRAINT `PK_AdminID` PRIMARY KEY (`id`)
);

Java class:

import lombok.Getter;
import lombok.Setter;
import org.joda.time.*;

public class Admin {
    @Getter
    @Setter
    public long id;
    @Getter
    @Setter
    public String email;
    @Getter
    @Setter
    public String password;
    @Getter
    @Setter
    public String salt;
    @Getter
    @Setter
    public DateTime created;

    public Admin() {

    }
}

Interface for manage admins:

import com.ShopMarketBuilder.Model.Entities.SuperAdmin.*;
import org.skife.jdbi.v2.sqlobject.*;
import org.skife.jdbi.v2.sqlobject.customizers.*;
import org.skife.jdbi.v2.sqlobject.stringtemplate.*;
import org.skife.jdbi.v2.tweak.*;

@UseStringTemplate3StatementLocator
@RegisterMapperFactory(BeanMapperFactory.class)
public interface IAdmin {

    @SqlQuery("select * from `shopmarket`.`admin` where `email` = :email and `password` = md5(concat(:password,`salt`));")
    Admin validate(@Bind("email") String email, @Bind("password") String password);

    @SqlUpdate("insert into `shopmarket`.`admin` (`email`,`password`,`salt`) VALUES (:email, md5(concat(:password, :salt)), :salt);")
    @GetGeneratedKeys
    long createAdmin(@BindBean Admin admin);
}

When I try to call "validate" function, it's working fine and I get Admin entity, but when I try to call "createAdmin" function, I have get the next error:

HTTP Status 500 - org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: Unable to execute, no named parameter matches "email" and no positional param for place 0 (which is 1 in the JDBC 'start at 1' scheme) has been set. [statement:"insert into shopmarket.admin (email,password,salt) VALUES (:email, md5(concat(:password, :salt)), :salt);", located:"insert into shopmarket.admin (email,password,salt) VALUES (:email, md5(concat(:password, :salt)), :salt);", rewritten:"insert into shopmarket.admin (email,password,salt) VALUES (?, md5(concat(?, ?)), ?);", arguments:{ positional:{}, named:{class:class com.ShopMarketBuilder.Model.Entities.SuperAdmin.Admin}, finder:[]}]

I found on stackoverflow.com similar question, but it wasn't helped me. If I change code as below, it's working properly.

@SqlUpdate("insert into `shopmarket`.`admin` (`email`,`password`,`salt`) VALUES (:email, md5(concat(:password, :salt)), :salt);")
    long createAdmin(@Bind("email") String email, @Bind("password") String password, @Bind("salt") String salt);
Community
  • 1
  • 1
Sanprof
  • 369
  • 1
  • 6
  • 17

1 Answers1

0

That's what I found out myself accidentally. I used type DateTime instead Date and in this there was a problem. I lost so many time to find this stupid solution.

import org.joda.time.*;

...

@Getter @Setter public DateTime created;

the correct class will be:

import java.util.Date;
import lombok.Getter;
import lombok.Setter;

public class Admin {

    @Getter
    @Setter
    public long id;
    @Getter
    @Setter
    public String email;
    @Getter
    @Setter
    public String password;
    @Getter
    @Setter
    public boolean rememberme;
    @Getter
    @Setter
    public String salt;
    @Getter
    @Setter
    public Date created;

    public Admin() {

    }
}
Sanprof
  • 369
  • 1
  • 6
  • 17