0

Im in the process of migration a small application from TomCat to WebLogic. When trying to deploy to weblogic I get the error shown in the title. I have looked at similar questions on SO and based on them I have updated the mysql-connector and springboot versions. For reference this is what I have used. Return type of JPA Repository 'getOne(id)' Method java.math.BigInteger cannot be cast to java.lang.Integer

I will attach some code to help pinpoint the issue.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.14.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>


    <artifactId>timeoff</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>timeoff</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.1.RELEASE</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
                <exclusion>
                    <artifactId>tomcat-embed-el</artifactId>
                    <groupId>org.apache.tomcat.embed</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>tomcat-embed-core</artifactId>
                    <groupId>org.apache.tomcat.embed</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>tomcat-embed-websocket</artifactId>
                    <groupId>org.apache.tomcat.embed</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.2.1.RELEASE</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>false</addDefaultImplementationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Dao.java

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Controller;

@Controller
public interface EmployeeRequestDao extends JpaRepository <EmployeeRequest, Long> {

}

Application.java

@ComponentScan 
@SpringBootApplication
public class TimeoffApplication extends SpringBootServletInitializer implements WebApplicationInitializer{

    @Autowired
    EmployeeRequestDao employeeRequestDao;
    public static void main(String[] args) {
        SpringApplication.run(TimeoffApplication.class, args);
    }

    @Override
       protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
          return builder.sources(TimeoffApplication.class);




}

}

EmployeeRequest.java


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "requestdetails") 
public class EmployeeRequest {

    @Id
    @Column(name ="id")
    @GeneratedValue(strategy =GenerationType.AUTO)
    private Long id ;

    public EmployeeRequest(String selectedSupervisor2, String selectedLeave2, String name, String fromDate, String fromTime, String toDate, String toTime) {

        this.selectedSupervisor2 = selectedSupervisor2;
        this.selectedLeave2 = selectedLeave2;
        this.name = name;
        this.fromDate = fromDate;
        this.fromTime = fromTime;
        this.toDate = toDate;
        this.toTime = toTime;

    }



    public EmployeeRequest() {

    }


    @Column(name ="selectedSupervisor2")
    private String selectedSupervisor2;

    @Column(name ="selectedLeave2")
    private String selectedLeave2;

    @Column(name ="name")
    private String name;

    @Column(name ="fromDate")
    private String fromDate;

    @Column(name ="fromTime")
    private String fromTime;

    @Column(name ="toDate")
    private String toDate;


    @Column(name ="toTime")
    private String toTime;


    public String getSelectedSupervisor2() {
        return selectedSupervisor2;
    }

    public void setSelectedSupervisor2(String selectedSupervisor2) {
        this.selectedSupervisor2 = selectedSupervisor2;
    }

    public String getselectedLeave2() {
        return selectedLeave2;
    }

    public void setselectedLeave2(String selectedLeave2) {
        this.selectedLeave2 = selectedLeave2;
    }

    public Long getId() {
        return id;
    }


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


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getFromDate() {
        return fromDate;
    }


    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }


    public String getFromTime() {
        return fromTime;
    }


    public void setFromTime(String fromTime) {
        this.fromTime = fromTime;
    }


    public String getToDate() {
        return toDate;
    }


    public void setToDate(String toDate) {
        this.toDate = toDate;
    }


    public String getToTime() {
        return toTime;
    }


    public void setToTime(String toTime) {
        this.toTime = toTime;
    }

}

Controller.java



import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import EmployeeRequest;
import EmployeeRequestDao;


@RestController
@RequestMapping("api")
@CrossOrigin(origins = "http://localhost:4200", allowedHeaders = "*")
public class EmployeeController {

    @Autowired
    EmployeeRequestDao employeeRequestDao;

    /**
     * This is used to get all requests in Employee leave
     * 
     * @return
     */
    @GetMapping("items")
    public List<EmployeeRequest> getItems() {
        return employeeRequestDao.findAll();
    }

    /**
     * This method returns the requests by ID
     * 
     * @param id
     * @return
     */

    @GetMapping("item/{id}")
    public EmployeeRequest getItem(@PathVariable("id") Long id) {
        return employeeRequestDao.getOne(id);
    }

    /**
     * This method is used to add requests in the database
     * 
     * @param employeeRequest
     * @return
     */

    @PostMapping("items")
    public EmployeeRequest addItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This method is used to update requests.
     * 
     * ??? Can potentially implement this further for supervisor access to approve
     * or deny requests ???
     * 
     * @param employeeRequest
     * @return
     */

    @PutMapping("item")
    public EmployeeRequest saveOrUpdateItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This Method updates the requests by ID
     * 
     * @param id
     * @param employeeRequestDetails
     * @return
     */

    @PutMapping("item/{id}")
    public EmployeeRequest updateItemById(@PathVariable Long id,
            @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);

        employeeRequest.setName(employeeRequest.getName());
        employeeRequest.setFromDate(employeeRequest.getFromDate());
        employeeRequest.setFromTime(employeeRequest.getFromTime());
        employeeRequest.setToDate(employeeRequest.getToDate());
        employeeRequest.setToTime(employeeRequest.getToTime());

        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }

    /**
     * This method is used to delete all requests from db.
     * 
     * @param employeeRequest
     */

    @DeleteMapping("items")
    public void deleteAllItems(EmployeeRequest employeeRequest) {
        employeeRequestDao.deleteAll();
    }

    /**
     * This method deletes a request from db.
     * 
     * @param id
     * @return
     */
    @DeleteMapping("items/{id}")
    public String deleteItem(@PathVariable Long id) {
        employeeRequestDao.delete(id);
        return "Deleted";
    }

    /**
     * This method patch updates the Date and Time of requests.
     * 
     * @param partialUpdate
     * @param id
     * @return
     */
    @PatchMapping("item/{id}")
    public EmployeeRequest patchUpdateItemById(@PathVariable Long id, @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);
        employeeRequest.setFromDate(employeeRequestDetails.getFromDate());
        employeeRequest.setFromTime(employeeRequestDetails.getFromDate());
        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }




}


import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import EmployeeRequest;
import EmployeeRequestDao;


@RestController
@RequestMapping("api")
@CrossOrigin(origins = "http://localhost:4200", allowedHeaders = "*")
public class EmployeeController {

    @Autowired
    EmployeeRequestDao employeeRequestDao;

    /**
     * This is used to get all requests in Employee leave
     * 
     * @return
     */
    @GetMapping("items")
    public List<EmployeeRequest> getItems() {
        return employeeRequestDao.findAll();
    }

    /**
     * This method returns the requests by ID
     * 
     * @param id
     * @return
     */

    @GetMapping("item/{id}")
    public EmployeeRequest getItem(@PathVariable("id") Long id) {
        return employeeRequestDao.getOne(id);
    }

    /**
     * This method is used to add requests in the database
     * 
     * @param employeeRequest
     * @return
     */

    @PostMapping("items")
    public EmployeeRequest addItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This method is used to update requests.
     * 
     * ??? Can potentially implement this further for supervisor access to approve
     * or deny requests ???
     * 
     * @param employeeRequest
     * @return
     */

    @PutMapping("item")
    public EmployeeRequest saveOrUpdateItem(@RequestBody EmployeeRequest employeeRequest) {
        return employeeRequestDao.save(employeeRequest);
    }

    /**
     * This Method updates the requests by ID
     * 
     * @param id
     * @param employeeRequestDetails
     * @return
     */

    @PutMapping("item/{id}")
    public EmployeeRequest updateItemById(@PathVariable Long id,
            @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);

        employeeRequest.setName(employeeRequest.getName());
        employeeRequest.setFromDate(employeeRequest.getFromDate());
        employeeRequest.setFromTime(employeeRequest.getFromTime());
        employeeRequest.setToDate(employeeRequest.getToDate());
        employeeRequest.setToTime(employeeRequest.getToTime());

        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }

    /**
     * This method is used to delete all requests from db.
     * 
     * @param employeeRequest
     */

    @DeleteMapping("items")
    public void deleteAllItems(EmployeeRequest employeeRequest) {
        employeeRequestDao.deleteAll();
    }

    /**
     * This method deletes a request from db.
     * 
     * @param id
     * @return
     */
    @DeleteMapping("items/{id}")
    public String deleteItem(@PathVariable Long id) {
        employeeRequestDao.delete(id);
        return "Deleted";
    }

    /**
     * This method patch updates the Date and Time of requests.
     * 
     * @param partialUpdate
     * @param id
     * @return
     */
    @PatchMapping("item/{id}")
    public EmployeeRequest patchUpdateItemById(@PathVariable Long id, @Valid @RequestBody EmployeeRequest employeeRequestDetails) {
        EmployeeRequest employeeRequest = employeeRequestDao.getOne(id);
        employeeRequest.setFromDate(employeeRequestDetails.getFromDate());
        employeeRequest.setFromTime(employeeRequestDetails.getFromDate());
        EmployeeRequest updatedItem = employeeRequestDao.save(employeeRequest);

        return updatedItem;
    }




}

stacktrace

```Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.jdbc.support.JdbcUtils.extractDatabaseMetaData(JdbcUtils.java:336) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 104 common frames omitted
Caused by: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1058) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:972) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:958) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:903) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:1025) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3480) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2444) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2214) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:797) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:31) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at sun.reflect.GeneratedConstructorAccessor417.newInstance(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_221]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_221]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:395) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:383) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131) ~[tomcat-jdbc-8.5.31.jar:na]
    at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) ~[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 105 common frames omitted
Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
    at com.mysql.jdbc.ConnectionImpl.buildCollationMapping(ConnectionImpl.java:976) ~[mysql-connector-java-commercial-5.1.22-bin.jar:na]
    ... 128 common frames omitted
3
  • Please post the whole stacktrace Commented Nov 15, 2019 at 16:46
  • @SimonMartinelli I have posted it. thanks Commented Nov 15, 2019 at 17:35
  • Can you also turn on the SQL logging? I need to see the generated select statement Commented Nov 16, 2019 at 10:22

3 Answers 3

1

Looks like the issue is happening on casting.

so could be mismatch between the mysql version you have and your version of mysql-connector.jar. can you try with a newer version of MySQL Connector?

Also i see that you are using mysql-connector-jar:8.0.18. But if you see the stacktrace it is showing mysql-connector-java-commercial-5.1.22-bin.jar. so the conflicting jar could be an issue as well.

Sign up to request clarification or add additional context in comments.

Comments

0

you can use BigInteger.intValue. But beware as you may lose information if your BigInteger is out of int's bounds

2 Comments

sorry im not sure, but where exactly in the code would I use that ?
@yabs you would use it where you are getting your classcastexception
0

In your EmployeeRequest class try changing the type of field id to BigInteger :

@Entity
@Table(name = "requestdetails") 
public class EmployeeRequest {

@Id
@Column(name ="id")
@GeneratedValue(strategy =GenerationType.AUTO)
private BigInteger id ;

public EmployeeRequest(String selectedSupervisor2, String selectedLeave2, String name, String fromDate, String fromTime, String toDate, String toTime) {

    this.selectedSupervisor2 = selectedSupervisor2;
    this.selectedLeave2 = selectedLeave2;
    this.name = name;
    this.fromDate = fromDate;
    this.fromTime = fromTime;
    this.toDate = toDate;
    this.toTime = toTime;

}



public EmployeeRequest() {

}


@Column(name ="selectedSupervisor2")
private String selectedSupervisor2;

@Column(name ="selectedLeave2")
private String selectedLeave2;

@Column(name ="name")
private String name;

@Column(name ="fromDate")
private String fromDate;

@Column(name ="fromTime")
private String fromTime;

@Column(name ="toDate")
private String toDate;


@Column(name ="toTime")
private String toTime;


public String getSelectedSupervisor2() {
    return selectedSupervisor2;
}

public void setSelectedSupervisor2(String selectedSupervisor2) {
    this.selectedSupervisor2 = selectedSupervisor2;
}

public String getselectedLeave2() {
    return selectedLeave2;
}

public void setselectedLeave2(String selectedLeave2) {
    this.selectedLeave2 = selectedLeave2;
}

public BigInteger getId() {
    return id;
}


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


public String getName() {
    return name;
}


public void setName(String name) {
    this.name = name;
}


public String getFromDate() {
    return fromDate;
}


public void setFromDate(String fromDate) {
    this.fromDate = fromDate;
}


public String getFromTime() {
    return fromTime;
}


public void setFromTime(String fromTime) {
    this.fromTime = fromTime;
}


public String getToDate() {
    return toDate;
}


public void setToDate(String toDate) {
    this.toDate = toDate;
}


public String getToTime() {
    return toTime;
}


public void setToTime(String toTime) {
    this.toTime = toTime;
}

}

Also update JpaRepository as follows :

public interface EmployeeRequestDao extends JpaRepository <EmployeeRequest, BigInteger> {

}

1 Comment

this did not work. I am still getting the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.