我正在研究一个需要与数据库进行交互的Web应用程序项目(本例中为Oracle 12c):Hibernate有什么用处?它太慢?开发者缺少什么?
为了理解和测试Hibernate的功能(因为我是第一次使用Hibernate):我编写了一个调用Web服务的小型Web应用程序。该Web服务的后端反过来使用hibernate从Oracle数据库获取数据,(它执行只读查询(通过CreateSQLQuery存储过程))。
它给了以下的结果,(在那里我的地方(的tomcat 8),以及数据库运行Web服务是在远程(AWS)):
520记录获取:21020毫秒(约21秒,以获取)
3846记录获取:172383毫秒(172秒〜3分钟)
我有几个问题:
我想知道什么是使用Hibernate?
2.在我的第一个练习中它太慢了,我搜索了所有的网络,但没有找到任何直接的答案或方向?
3.如果你是一个谁在使用休眠,然后可以请你让我知道你为什么采用这种休眠,什么业务问题解决了(比如是它谁可以只运行基本的web应用程序的初学者?)
4.请让我知道Hibernate的开发人员缺少什么?
参考文献: simple-hibernate-query-returning-very-slowly
why-is-the-hibernate-query-list-slow
sql-query-within-hibernate-extremely-slow-while-very-fast-using-sql-cient
下面是我简单的Hibernate工作方面:
查询:
List<Model> modelList = session.createSQLQuery("SELECT * FROM TABLE)
.addEntity(modelList.class).list();
*表 - 我为简单改了名字,我overyall正在运行只有一个简单的选择查询来获取的是,没有什么复杂的,
配置文件
<?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">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@X.X.X.X:1521:orcl</property>
<property name="hibernate.connection.password">*****</property>
<property name="hibernate.connection.username">******</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto"></property>
<mapping class="x.x.x.x.xsModel" />
<mapping class="x.x.x.x.dModel" />
</session-factory> </hibernate-configuration>
冬眠的Util
public class HibernateUtil // private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory sessionFactory = buildSessionFactory();
private static final Logger logger = LoggerFactory.getLogger(HibernateUtil.class);
private static SessionFactory buildSessionFactory()
{
try
{
if (sessionFactory == null) {
// loads configuration and mappings
Configuration configuration = new Configuration().configure()
.setProperty("hibernate.connection.driver_class", "oracle.jdbc.OracleDriver")
.setProperty("hibernate.connection.url", "jdbc:oracle:thin:@X.X.X.X:orcl")
.setProperty("hibernate.connection.password","aaaaa")
.setProperty("hibernate.connection.username","aaaaa")
.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
.setProperty("show_sql","true")
//.setProperty("hibernate.hbm2ddl.auto", "")
// .setProperty("hibernate.temp.use_jdbc_metadata_defaults","false")
// .setProperty("hibernate.jdbc.lob.non_contextual_creation","true")
.addAnnotatedClass(dModel.class)
.addAnnotatedClass(sModel.class);
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
logger.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
我已经使用JDBC并采取了采取由Hibernate ORM或多或少同一时间 - 520个记录获取:21020毫秒(大约21秒内提取),3846条记录来获取: 172383 ms(172 Seconds〜3 Minutes)(在JDBC中,第一次只增加1-2秒,第二次增加20秒)。 Native Oracle Client的含义是什么? –