我在使用Connection
时试图在Hibernate中获取SessionFactory.getCurrentSession()
个对象。
源代码
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.internal.SessionImpl;
public class SOExample {
public static void main(String[] args) throws SQLException {
Configuration configuration = new Configuration();
SessionFactory sessionFactory = configuration.buildSessionFactory(new StandardServiceRegistryBuilder().configure().build());
Session session = sessionFactory.getCurrentSession();
Connection connection = ((SessionImpl) session).connection();
// doing operation on connection object as per my requirement
DatabaseMetaData databaseMetaData = connection.getMetaData();
System.out.println(databaseMetaData.getDatabaseProductName());
}
}
堆栈跟踪
Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy24 cannot be cast to org.hibernate.internal.SessionImpl
at com.SOExample.main(SOExample.java:20)
getCurrentSession()
提供Proxy
Session
对象,因此无法将其投放到SessionImpl
,那么获取Connection
对象的其他方法是什么? 。或如何从代理对象SessionImpl
。
我试过的其他选项,但它说找不到getConnectionProvider()
方法。
SessionFactoryImplementor sessionFactoryImplementation = (SessionFactoryImplementor) session.getSessionFactory();
ConnectionProvider connectionProvider = sessionFactoryImplementation.getConnectionProvider();
try {
Connection connection = connectionProvider.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
注意:我使用 hibernate-core-5.0.5.Final.jar
答案 0 :(得分:12)
在 Hibenate 5中,我们需要做一些不同的事情(有关详情,请查看https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html):
<?php
$data_string = array('grant_type'=>'authorization_code',
'orgID'=>'ORG_bbfad6',
'access_token'=>'c309792551930b835a0163835097bfc9ac4e31be',
'fromDate'=>'2016-09-21',
'toDate'=>'2016-09-21');
//Web API URL
$service_url='http://192.168.65.106:8080/test/range';
$ch = curl_init($service_url);
//request header
$headers = array(
//"POST "." HTTP/1.0",
"Content-type:application/x-www-form-urlencoded",
"Accept:application/json",
"Cache-Control: no-cache",
"Connection: Keep-Alive",
"Pragma: no-cache",
);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
$curl_response = curl_exec($ch);
if ($curl_response === false)
{
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
$decoded = json_decode($curl_response);
curl_close($ch);
echo 'response ok!';
print("<pre>");
var_dump($decoded);
print("</pre>");
如果您想测试代码,我的配置为derby db。
[{"--------------------------95901a4b56edd440\r\nContent-Disposition:_form-data;_name":"\"grant_type\"\r\n\r\nauthorization_code\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"orgID\"\r\n\r\nORG_bbfad6\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"access_token\"\r\n\r\nc309792551930b835a0163835097bfc9ac4e31be\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"fromDate\"\r\n\r\n2016-09-21\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"toDate\"\r\n\r\n2016-09-21\r\n--------------------------95901a4b56edd440--\r\n","0":"\"grant_type\"\r\n\r\nauthorization_code\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"orgID\"\r\n\r\nORG_bbfad6\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"access_token\"\r\n\r\nc309792551930b835a0163835097bfc9ac4e31be\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"fromDate\"\r\n\r\n2016-09-21\r\n--------------------------95901a4b56edd440\r\nContent-Disposition: form-data; name=\"toDate\"\r\n\r\n2016-09-21\r\n--------------------------95901a4b56edd440--\r\n"}]
此应用的输出: