我正在使用Netbeans连接数据库。我想使用Java持久性从数据库中检索数据,但我收到了这些错误:
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
这就是我所说的:
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();
}
}
这是我的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>
我的项目:
答案 0 :(得分:5)
您使用的是Java 8吗?如果你是,你可能想要使用最新的Javassist。至少我发现当我使用3.12 GA的Javassist版本时,我遇到了这个问题。所以在你的Maven POM中:
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>
答案 1 :(得分:0)
关于Fly的回答,就像他说的那样,这似乎与java 8有关。 实际上,hibernate-entitymanager和hibernate-core都带有自己的javassist版本。
我假设你正在使用maven,所以你可以在命令行中看到它:
mvn dependency:tree
在查看pom.xml时,如果您使用Eclipse,则更容易使用标签Dependency Hierarchy
标签。
您需要查看这些依赖项的来源并将其排除在外:
<exclusions>
<exclusion>
<artifactId>javassist</artifactId>
<groupId>org.javassist</groupId>
</exclusion>
</exclusions>
在此之后,您可以添加Fly指示的依赖项,并且所有依赖项都可以正常工作。
顺便通过3.18.1-GA测试。