2

I'm using Netbeans to connect to a database. I want to use Java persistence to retrieve data from the database but I'm getting these errors:

jun 18, 2014 9:41:44 AM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
jun 18, 2014 9:41:44 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.6.Final}
jun 18, 2014 9:41:44 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
jun 18, 2014 9:41:44 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
    at controle_entregas.controleEntregasUI.jMenuItemNovaEntregaActionPerformed(controleEntregasUI.java:199)
    at controle_entregas.controleEntregasUI.access$100(controleEntregasUI.java:18)
    at controle_entregas.controleEntregasUI$2.actionPerformed(controleEntregasUI.java:85)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6527)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6292)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4883)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4705)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: javax.persistence.PersistenceException: Unable to configure EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:387)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:58)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at controle_entregas.CadEditEntrega.<clinit>(CadEditEntrega.java:33)
    ... 41 more
Caused by: java.lang.RuntimeException: Error while reading file:/C:/Users/Huberto/Documents/NetBeansProjects/Controle_Entregas/build/classes/
    at org.hibernate.ejb.packaging.NativeScanner.getClassesInJar(NativeScanner.java:131)
    at org.hibernate.ejb.Ejb3Configuration.addScannedEntries(Ejb3Configuration.java:487)
    at org.hibernate.ejb.Ejb3Configuration.addMetadataFromScan(Ejb3Configuration.java:477)
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:363)
    ... 45 more
Caused by: java.io.IOException: invalid constant type: 18
    at javassist.bytecode.ConstPool.readOne(ConstPool.java:1113)
    at javassist.bytecode.ConstPool.read(ConstPool.java:1056)
    at javassist.bytecode.ConstPool.<init>(ConstPool.java:150)
    at javassist.bytecode.ClassFile.read(ClassFile.java:765)
    at javassist.bytecode.ClassFile.<init>(ClassFile.java:109)
    at org.hibernate.ejb.packaging.AbstractJarVisitor.checkAnnotationMatching(AbstractJarVisitor.java:246)
    at org.hibernate.ejb.packaging.AbstractJarVisitor.executeJavaElementFilter(AbstractJarVisitor.java:212)
    at org.hibernate.ejb.packaging.AbstractJarVisitor.addElement(AbstractJarVisitor.java:173)
    at org.hibernate.ejb.packaging.ExplodedJarVisitor.getClassNamesInTree(ExplodedJarVisitor.java:126)
    at org.hibernate.ejb.packaging.ExplodedJarVisitor.getClassNamesInTree(ExplodedJarVisitor.java:134)
    at org.hibernate.ejb.packaging.ExplodedJarVisitor.doProcessElements(ExplodedJarVisitor.java:92)
    at org.hibernate.ejb.packaging.AbstractJarVisitor.getMatchingEntries(AbstractJarVisitor.java:149)
    at org.hibernate.ejb.packaging.NativeScanner.getClassesInJar(NativeScanner.java:128)
    ... 48 more

This is how I call it:

public class CadEditEntrega extends javax.swing.JFrame {
    static DBConnect connection = null;
    private static final String tabela = "clientes";    
    private Integer id = null;
    private Date date = null;

    @PersistenceContext
    static EntityManagerFactory emf = Persistence.createEntityManagerFactory("Controle_EntregasPU");
    public EntityManager em = emf.createEntityManager();

    /**
     * Creates new form CadEditEntrega
     * @param connection
     * @throws java.sql.SQLException
     * @throws java.lang.ClassNotFoundException
     */

    public CadEditEntrega(DBConnect connection) throws SQLException, ClassNotFoundException {
        CadEditEntrega.connection = connection;
        this.date = new Date();
        initComponents();
        jDateChooser.setDate(date);
        preencheComboCliente();
        preencheComboProduto();
    }

    private void preencheComboCliente() throws SQLException{
        /*ResultSet result = connection.dbSelect("*", "clientes");
        while(result.next()){
            jComboBoxCliente.addItem(result.getString("cliente_nome"));
        }*/
        em.getTransaction().begin();
        List<Clientes> list = em.createNamedQuery("Clientes.findAll", Clientes.class).getResultList();
        em.getTransaction().commit();
        em.close();
    }
}

And this is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="Controle_EntregasPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>controle_entregas.Clientes</class>
    <class>controle_entregas.Movimentacao</class>
    <class>controle_entregas.MovimentacaoProdutos</class>
    <class>controle_entregas.Produtos</class>
    <class>controle_entregas.TiposMovimentacao</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://mysql22.******:3306/******?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.user" value="******"/>
      <property name="javax.persistence.jdbc.driver" value="******"/>
      <property name="javax.persistence.jdbc.password" value="******"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>

My project:

https://i.stack.imgur.com/YuAYl.png

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
Hubertokf
  • 308
  • 1
  • 4
  • 12
  • Hard to say, but `Caused by: java.io.IOException: invalid constant type: 18` may point to a damaged file. Check if the jars are really .jar and class files really .class. – PeterMmm Jun 18 '14 at 12:59
  • This may help: http://stackoverflow.com/questions/3026956/javassist-failure-in-hibernate-invalid-constant-type-60 – PeterMmm Jun 19 '14 at 05:43
  • Try also to set project to source/binary/platform to JDK1.7. – PeterMmm Jun 19 '14 at 05:46

2 Answers2

5

Are you using Java 8? If you are you might want to use the latest Javassist. At least I found when I was using version of 3.12 GA of Javassist I had this problem. So in your Maven POM:

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.18.2-GA</version>
</dependency>
dur
  • 15,689
  • 25
  • 79
  • 125
Fly
  • 51
  • 1
  • 2
  • 1
    I tried that but I am still facing the following exception: java.io.IOException: invalid constant type: 18 – Akshay Apr 18 '15 at 05:19
0

following on Fly answer, like he said, this seems to be related to java 8. Actually hibernate-entitymanager and hibernate-core both bring their own versions of javassist.

I assume you're using maven, so you can actually see it in the command line with:

mvn dependency:tree

Even easier, in case you use Eclipse is the tab Dependency Hierarchy tab when viewing the pom.xml.

You'll need to see where these dependencies are being brought from and exclude them as such:

<exclusions>
    <exclusion>
        <artifactId>javassist</artifactId>
        <groupId>org.javassist</groupId>
    </exclusion>
</exclusions>

After this you can add the dependency as Fly indicated and all should work.

Tested with 3.18.1-GA by the way.

Mário Fernandes
  • 1,822
  • 1
  • 21
  • 21