JSF Eclipselink - I can’t write information to Mysql database

Asked

Viewed 101 times

0

I am using JSF in a college subject and the teacher has passed an activity. But I am not able to record information from a form. The connection is made, I can even perform JPQL queries straight from Netbeans, but when I try to save information entered by a form the following error appears:

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.lang.Classcastexception: java.math.Biginteger cannot be cast to java.lang.Long Error Code: 0

I am using the Java connector v5.1.47. When I try to use a more up-to-date connector I cannot connect to the database. Even with Mysql configured not to use root password encryption. Because when I used, the connection wasn’t made because of this encryption.

Updating

Here is my persistence.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="RestManagerPU" transaction-type="JTA">
    <jta-data-source>java:app/RestManager</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

And here is Daohelper, where the error occurs:

*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author leand
 */
public class DAOHelper {
    public EntityManager getEM(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("RestManagerPU");
        return emf.createEntityManager();
    }
}

Error occurs on line:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("RestManagerPU");

2 answers

0


Finally I found out what the problem is. For some reason the JDBC driver does not work with Mysql 8, even the 8.0.12 driver. I installed Mysql 5.7 and for the connection I used the driver 5.1.47, now it is working.

0

By logging your error seems to be a type error (type).

Java does not seem to be able to transform a type variable BigInteger for type Long automatically. It is probably some form field, but the log does not show what it would be.

You will have to check the data you are entering into the database and transform (cast) to the appropriate types.

If you are using Mysql, you should probably use java.math.Bigdecimal to transform BigInteger in Long.

See table 5.1 at Java, JDBC and Mysql Types for more information on possible conversions.
inserir a descrição da imagem aqui

  • But the Usuarios object has all String attributes, I created them by backup engineering by Netbeans itself. So the attribute types match the database columns.

  • I looked here at the debugging and the error does not occur when writing the data to the database, occurs when trying to create the connection

  • 1

    Yes, he says he couldn’t connect WHY he couldn’t cast the variable. " Connection could not be allocated because: java.lang.Classcastexception: java.math.Biginteger cannot be cast to java.lang.Long". I would need to see how you process the form data and try to insert it into the BD. That’s where the problem should be.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.