要解决我的问题,请使用以下代码。但我不认为这是对的......
HibernateFactory.buildSessionFactory();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
HibernateFactory.closeFactory();
}
});
这就是我的依赖 Jar files
我的hibernate.cfg
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/ClinicaVet?UseTimezone=true&serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">jailbreak</property>
<property name="current_session_context_class">thread</property>
<!-- Disable the second level cache -->
<property
name="cache.provider_class">org.hibernate.cache.internall.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2dd1">update</property>
<mapping resource="com/arthurpdj/entity/Agendamento.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Animal.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Cliente.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Examinacao.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Exame.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Vacinacao.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Notificacoes.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Vacina.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Atendimento.hbm.xml"/>
<mapping resource="com/arthurpdj/entity/Pagamento.hbm.xml"/>
</session-factory> </hibernate-configuration>
我的HibernateUtil类(我已经尝试过没有ServiceRegistry ..同样的问题..)
public class HibernateUtil {
private static SessionFactory factory = null;
private static Configuration conf;
private static ServiceRegistry serviceRegistry;
private static SessionFactory buildSessionFactory() {
try{
conf = new Configuration();
conf.configure("com/arthurpdj/database/hibernate.cfg.xml");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
conf.getProperties()).build();
System.out.println("Configurou");
factory = conf.buildSessionFactory(serviceRegistry);
System.out.println("construi");
return factory;
} catch(Throwable ex) {
ex.printStackTrace();
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory(){
if(factory ==null) {
factory = buildSessionFactory();
}
return factory;
}
}
记录
fev 02, 2017 3:05:14 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.7.Final}
fev 02, 2017 3:05:14 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
fev 02, 2017 3:05:14 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
fev 02, 2017 3:05:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
fev 02, 2017 3:05:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/ClinicaVet?UseTimezone=true&serverTimezone=UTC]
fev 02, 2017 3:05:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
fev 02, 2017 3:05:15 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
fev 02, 2017 3:05:15 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Thu Feb 02 15:05:16 BRST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
fev 02, 2017 3:05:16 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
主类
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("view/FXMLLogin.fxml"));
Scene scene = new Scene(root, 424, 331);
primaryStage.setMaximized(false);
primaryStage.setResizable(false);
primaryStage.setTitle("Clinica Veterinária BarraVet");
primaryStage.setScene(scene);
primaryStage.show();
HibernateFactory.buildSessionFactory();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
HibernateFactory.closeFactory();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
答案 0 :(得分:0)
使用此初始化:
{
//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//creating seession factory object
SessionFactory factory=cfg.buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
//Your code of reading/writing to database here.....
t.commit();//transaction is committed
session.close();
System.out.println("successful transaction");
}
}
这是最安全的方法......希望这有帮助
答案 1 :(得分:0)
@Akshay,谢谢你的努力。我使用c3p0连接池解决了我的问题