为什么我遇到此错误:java.lang.ExceptionInInitializerError

时间:2019-01-05 17:58:27

标签: java hibernate jsf

我正在使用IntelliJ中的JSF和Hibernate创建一个简单的Laptop-info表单。当我执行项目时,我会遇到此错误。

javax.servlet.ServletException: java.lang.ExceptionInInitializerError
javax.faces.webapp.FacesServlet.service(FacesServlet.java:667)

我在这里放置了所需的文件和目录结构。

1)LaptopDAO.java

package Dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import pojo.Laptop;
import util.HibernateUtil;
public class LaptopDAO {

public void SaveLaptop(Laptop laptop)
{         Session session= HibernateUtil.getSessionFactory().openSession();
    Transaction t=session.beginTransaction();
    session.save(laptop);
    t.commit();
    session.close();
}
}

2)Laptop.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="pojo.Laptop" table="laptopdb">
    <id name="id"><generator class="increment"></generator></id>
    <property name="company">
        <column name="laptopcompany"></column>
    </property>
    <property name="model">
        <column name="laptopmodel"></column>
    </property>
    <property name="ram">
        <column name="laptopram"></column>
    </property>
    <property name="hd">
        <column name="laptophd"></column>
    </property>

</class> </hibernate-mapping>

3)LaptopBean

package LaptopMBean;


import Dao.LaptopDAO;
import pojo.Laptop;
import javax.faces.bean.ManagedBean;
@ManagedBean(name = "laptopBean")
public class LaptopBean {     Laptop l;
public LaptopBean()
{
    l=new Laptop();
}
public Laptop getL()
{
    return l;
}
public void setL(Laptop l)
{         this.l = l;
}
public void savetoDB()
{         LaptopDAO ld=new LaptopDAO();
    ld.SaveLaptop(l);
    System.out.print("Success");
}
}

4)笔记本电脑

package pojo;


public class Laptop {
int id;
String company;
String model;
String ram;
String hd;


public int getId() {
    return id;
}

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

public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

public String getModel() {
    return model;
}

public void setModel(String model) {
    this.model = model;
}

public String getRam() {
    return ram;
}

public void setRam(String ram) {
    this.ram = ram;
}

public String getHd() {
    return hd;
}

public void setHd(String hd) {
    this.hd = hd;
}
}

5)HibernateUtil

package util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {
private static final SessionFactory sessionFactory=buildSessionFactory();
public static SessionFactory buildSessionFactory()
{
    try {
        return new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex)
    {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}        public static SessionFactory getSessionFactory()
{         return sessionFactory;
}
}

6)hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property 
name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property 
name="connection.url">jdbc:mysql://localhost:3306/laptop</property>
    <property 
name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.username">root</property>
    <property name="connection.password">admin</property>
    <property name="connection.pool_size">1</property>

    <mapping resource="hbm/laptop.hbm.xml"/>
</session-factory>
</hibernate-configuration>

6)StackTrace

23:44:51,700 INFO  [org.hibernate.validator.internal.util.Version] (default 
task-4) HV000001: Hibernate Validator 5.1.3.Final
23:44:52,080 INFO  [org.hibernate.Version] (default task-4) HHH000412: 
Hibernate Core {5.4.0.Final}
23:44:52,084 INFO  [org.hibernate.cfg.Environment] (default task-4) 
HHH000206: hibernate.properties not found
23:44:52,091 ERROR [stderr] (default task-4) Initial SessionFactory creation 
failed.java.lang.NoClassDefFoundError: 
net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver
23:44:52,092 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] 
(default task-4) #{laptopBean.savetoDB()}: 
java.lang.ExceptionInInitializerError: javax.faces.FacesException: # 
{laptopBean.savetoDB()}: 

我已经添加了所有库。请帮助我找出问题所在。谢谢

0 个答案:

没有答案