我正在尝试使用hibernate在数据库中保存数据但是遇到严重错误任何人都可以帮助我吗?我正在使用mysql数据库 以下是我的DTO和DAO课程: 注意 - 在DAO类中,我只使用SessionFactoryUtil.getFactory()方法获取“SessionFactory”类型的对象,方法是添加我创建的jar文件,只使用单例获取SessionFactory的一个对象class“SessionFactoryUtil”。
----------------------------------------------------
*This is DTO class*
package com.om.hibernate.dto;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="army_table")
public class ArmyDTO implements Serializable{
@Id
//here name attribute is user defined and strategy attribute is predefined
@GenericGenerator(name="om",strategy="increment")
@GeneratedValue(generator="om")
@Column(name = "armyd_id")
private int aid;
@Column(name = "army_country_name")
private String countryName;
@Column(name = "army_type")
private String type;
@Column(name = "army_no_of_rec")
private String noOfRec;
@Column(name = "army_batchno")
private String batchNo;
public ArmyDTO() {
System.out.println("ArmyDTO created");
}
public int getAid() {
return aid;
}
public void setAid(int aid) {
this.aid = aid;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNoOfRec() {
return noOfRec;
}
public void setNoOfRec(String noOfRec) {
this.noOfRec = noOfRec;
}
public String getBatchNo() {
return batchNo;
}
public void setBatchNo(String batchNo) {
this.batchNo = batchNo;
}
}
----------------------------
*This is DAO class*
package com.om.hibernate.dao;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.om.hibernate.dto.ArmyDTO;
import com.om.hibernate.util.SessionFactoryUtil;
public class ArmyDAO {
//getting object of SessionFactory type
SessionFactory factory = SessionFactoryUtil.getFactory();
public void anythingButSave(ArmyDTO armyDTO) {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
try {
session.save(armyDTO);
tx.commit();
}
catch(HibernateException he) {
he.printStackTrace();
tx.rollback();
}
finally {
session.close();
}
}
}
---------------------------
This is the class to get only and only one object of
**SessionFactory** type
package com.om.hibernate.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/*This Singleton class is created to get the single object of SessionFactory type, we are creating jar file of this project to make use of SessionFactory type object in hibernate project*/
public final class SessionFactoryUtil {
private static SessionFactory factory;
static{
System.out.println("Starting to init SF through util jar");
Configuration configuration = new Configuration();
configuration.configure();
factory = configuration.buildSessionFactory();
//in case of real world project never use System.out.println("");
System.out.println("SessionFactory(SF) created through util jar");
}
public static SessionFactory getFactory() {
System.out.println("calling getFactory() from util jar");
return factory;
}
public static void closeFactory() {
System.out.println("We are closing factory , your codewill not work from now ...msg from util jar");
factory.close();
}
}
----------------------------------------------
----------------------------------------------
Below is the error appeared in Console
----------------------------------------------
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.om.hibernate.dao.ArmyDAO.<init>(ArmyDAO.java:14)
at com.om.hibernate.util.SaveTester.main(SaveTester.java:18)
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:244)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:208)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.om.hibernate.util.SessionFactoryUtil.<clinit>(SessionFactoryUtil.java:15)
... 2 more
Caused by: org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator$1$1.convert(BasicConnectionCreator.java:101)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.convertSqlException(BasicConnectionCreator.java:123)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:41)
at org.hibernate.engine.jdbc.connections.internal.BasicConnectionCreator.createConnection(BasicConnectionCreator.java:58)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.addConnections(PooledConnections.java:106)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:40)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections.<init>(PooledConnections.java:19)
at org.hibernate.engine.jdbc.connections.internal.PooledConnections$Builder.build(PooledConnections.java:138)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:110)
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:74)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:217)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:189)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:234)
... 16 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 1 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2104)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
... 31 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
... 41 more
-------------------------------------------------------------
My hibernate configuration file is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">92*csoni</property>
</session-factory>
</hibernate-configuration>
-----------------------------------------
Total jars I am using are:
1.antlr-3.1.1
2.dom4j-1.6.1
3.hibernate-common-annotations..
4.hibernate-core-5.0.4 Final
5.hiberante-jpa-2.1-api-1.0.0Final
6.javaee
7.javaassit-3.17.1-GA
8.jboss-logging-3.0.0Final
9.mysql-connector-java-5.1.5-bin
and one more jar that I created to get SessionFactory type object only once.