我正在尝试运行一个基本的hibernate hello world程序,该程序将从数据库中获取员工详细信息并打印“hello employee\u name”。但我不知道哪里出了问题,它不断地向我显示无效的配置错误。
Aug 30, 2018 6:19:35 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Aug 30, 2018 6:19:35 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.5.Final}
Aug 30, 2018 6:19:35 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Aug 30, 2018 6:19:35 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Aug 30, 2018 6:19:36 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Aug 30, 2018 6:19:36 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Initial SessionFactory creation failedorg.hibernate.MappingException: invalid configuration
Exception in thread "main" java.lang.ExceptionInInitializerError
at hibernatePack.hibernateUtil.<clinit>(hibernateUtil.java:28)
at hibernatePack.model.hibernateHelloWorld.main(hibernateHelloWorld.java:17)
Caused by: org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at hibernatePack.hibernateUtil.<clinit>(hibernateUtil.java:16)
... 1 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 26; Element type "hibernate-configuration" must be declared.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
... 3 more
我已经把代码贴在下面了。如有任何建议,我们将不胜感激。谢谢。
hibernateutil.java版本
package hibernatePack;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class hibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration();
configuration.configure();
StandardServiceRegistryBuilder sb = (StandardServiceRegistryBuilder) new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
StandardServiceRegistry standardServiceRegistry = sb.build();
sessionFactory = configuration.buildSessionFactory(standardServiceRegistry);
}
catch (Throwable th) {
System.err.println("Initial SessionFactory creation failed" + th);
throw new ExceptionInInitializerError(th);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
hibernatehelloworld.java类
package hibernatePack.model;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import hibernatePack.hibernateUtil;
import hibernatePack.model.Employee;
public class hibernateHelloWorld {
public static void main(String[] args) throws Exception {
SessionFactory sessionFactory = hibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
org.hibernate.Transaction tr = session.beginTransaction();
String strSql ="from Employee o";
Query query = session.createQuery(strSql);
List lst = query.list();
for(Iterator it=lst.iterator();it.hasNext();){
Employee emp=(Employee)it.next();
System.out.println("Hello: " + emp.getEmpName());
}
tr.commit();
System.out.println("Data displayed");
sessionFactory.close();
}
}
hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/springdata</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pooja31</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="hibernatePack.Employee" />
</session-factory>
</hibernate-configuration>
员工.java
package hibernatePack.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "emp")
public class Employee implements Serializable{
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name="name")
private String empName;
@Column(name="salary")
private int empSalary;
@Column(name="designation")
private String empDesignation;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpSalary() {
return empSalary;
}
public void setEmpSalary(int empSalary) {
this.empSalary = empSalary;
}
public String getEmpDesignation() {
return empDesignation;
}
public void setEmpDesignation(String empDesignation) {
this.empDesignation = empDesignation;
}
}
2条答案
按热度按时间b4qexyjb1#
在hibernate.cfg.xml中,定义的Map类无效(包名称不正确)。改变
至
此外,还需要指定错误中所述的“hibernate配置”类型。尝试使用以下配置类型。
请尝试修复此问题,您可以参考:https://www.javatpoint.com/steps-to-create-first-hibernate-application
gj3fmq9x2#
我认为您的hibernate.cfg.xml有一些配置问题,因为hibernate无法创建sessionfactory示例。
使用下面的类并删除hibernate.cfg.xml文件。