hibernate优点缺点.doc_第1页
hibernate优点缺点.doc_第2页
hibernate优点缺点.doc_第3页
hibernate优点缺点.doc_第4页
hibernate优点缺点.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一、hibernate是jdbc的轻量级的对象封装,它是一个独立的对象持久层框架,和app server,和ejb没有什么必然的联系。hibernate可以用在任何jdbc可以使用的场合,例如java应用程序的数据库访问代码,dao接口的实现类,甚至可以是bmp里面的访问数据库的代码。从这个意义上来说,hibernate和eb不是一个范畴的东西,也不存在非此即彼的关系。二、hibernate是一个和jdbc密切关联的框架,所以hibernate的兼容性和jdbc驱动,和数据库都有一定的关系,但是和使用它的java程序,和app server没有任何关系,也不存在兼容性问题。三、hibernate不能用来直接和entity bean做对比,只有放在整个j2ee项目的框架中才能比较。并且即使是放在软件整体框架中来看,hibernate也是做为jdbc的替代者出现的,而不是entity bean的替代者出现的,让我再列一次我已经列n次的框架结构:传统的架构:1) session bean entity bean db为了解决性能障碍的替代架构:2) session bean dao jdbc db使用hibernate来提高上面架构的开发效率的架构:3) session bean dao hibernate db 就上面3个架构来分析:1、内存消耗:采用jdbc的架构2无疑是最省内存的,hibernate的架构3次之,eb的架构1最差。2、运行效率:如果jdbc的代码写的非常优化,那么jdbc架构运行效率最高,但是实际项目中,这一点几乎做不到,这需要程序员非常精通jdbc,运用batch语句,调整preapredstatement的batch size和fetch size等参数,以及在必要的情况下采用结果集cache等等。而一般情况下程序员是做不到这一点的。因此hibernate架构表现出最快的运行效率。eb的架构效率会差的很远。3、开发效率:在有jbuilder的支持下以及简单的项目,eb架构开发效率最高,jdbc次之,hibernate最差。但是在大的项目,特别是持久层关系映射很复杂的情况下,hibernate效率高的惊人,jdbc次之,而eb架构很可能会失败。4、分布式,安全检查,集群,负载均衡的支持由于有sb做为facade,3个架构没有区别。四、eb和hibernate学习难度在哪里?eb的难度在哪里?不在复杂的xml配置文件上,而在于eb运用稍微不慎,就有严重的性能障碍。所以难在你需要学习很多ejb设计模式来避开性能问题,需要学习app server和eb的配置来优化eb的运行效率。做eb的开发工作,程序员的大部分精力都被放到了eb的性能问题上了,反而没有更多的精力关注本身就主要投入精力去考虑的对象持久层的设计上来。hibernate难在哪里?不在hibernate本身的复杂,实际上hibernate非常的简单,难在hibernate太灵活了。当你用eb来实现持久层的时候,你会发现eb实在是太笨拙了,笨拙到你根本没有什么可以选择的余地,所以你根本就不用花费精力去设计方案,去平衡方案的好坏,去费脑筋考虑选择哪个方案,因为只有唯一的方案摆在你面前,你只能这么做,没得选择。hibernate相反,它太灵活了,相同的问题,你至少可以设计出十几种方案来解决,所以特别的犯难,究竟用这个,还是用那个呢?这些方案之间到底有什么区别呢?他们的运行原理有什么不同?运行效率哪个比较好?光是主键生成,就有七八种方案供你选择,你为难不为难?集合属性可以用set,可以用list,还可以用bag,到底哪个效率高,你为难不为难?查询可以用iterator,可以用list,哪个好,有什么区别?你为难不为难?复合主键你可以直接在hbm里面配置,也可以自定义customertype,哪种比较好些?你为难不为难?对于一个表,你可以选择单一映射一个对象,也可以映射成父子对象,还可以映射成两个1:1的对象,在什么情况下用哪种方案比较好,你为难不为难?这个列表可以一直开列下去,直到你不想再看下去为止。当你面前摆着无数的眼花缭乱的方案的时候,你会觉得幸福呢?还是悲哀呢?如果你是一个负责的程序员,那么你一定会仔细研究每种方案的区别,每种方案的效率,每种方案的适用场合,你会觉得你已经陷入进去拔不出来了。如果是用eb,你第一秒种就已经做出了决定,根本没得选择,比如说集合属性,你只能用collection,如果是hibernate,你会在bag,list和set之间来回犹豫不决,甚至搞不清楚的话,程序都没有办法写。the ejb components are managed by ejb container, so who manage the hibernate objects?in framework 1, for bmp, the jdbc is wrapped by ejb container as ds, they are transparent to clients, now the hibernate is going to replace jdbc, where i deploy them to?i do appreciate your help.thanks.他并不需要容器manage象一般的容器就可以 tomcat你可以搞个简单的hibernate 例子 试试看if you need tomcat, that means the hibernate objects still need container.i confused the tomcat was used for holding servlets, i just guess they dont manage hibernate objects.for example, i have a table called test with one colume: col1 datatype is varchar2(100).the database is in a remote node.currently i have a node, this is my ejb container, i have a stateless session bean with remote hello, and the bean class is hellobean. i need to implement a business method named: public void hello (string s) inside hellobean and exposed to hello, when client invoke hello(hello world.);, i want to insert one row of data hello world. into the table test. my first question is: the hibernate has hundreds classes, where i put those library classes? if the ejb container have to know all the classes.my second question is: if i need jdbc connection from public void hello(string s) / jdbc needed here to insert.how i get the connection object?thanks. i just interested to know how the links are implemented:3) session bean dao hibernate dbi only need the idea where to find the hibernate objects from the bean.thanks.灵活的东西,不适合于工业生产.楼主转载请说明,hibernate的优势是显而易见的,如果有任何问题,请参看hibernate的文档,ejb3.0的也是吸取了hibernate的很多的东西in the 3) session bean dao hibernate dbwho is going to implement the dao?really need someones help?didongusa wrote:in the 3) session bean dao hibernate dbwho is going to implement the dao?really need someones help?firstly,youd better to bind a sessionfactory to your app server,then in your session bean can get the sessionfactory ,then you can put it in your dao.you dao can do all the crud operation.session beans bussness method is your daos caller.looks like you have to write some startup files to create the sessionfactory objects and bind them at the server startup.if shutdown and restart the server is impossible, how can you create and bind the object?thanks.using the t3startupdef to trigger the binding at startup could hurt the portability, i think this is no no. another way?thanks.okay.then you can use spring with hibernate.咋都说英文了?用spring+hibernate 吧.this container is too cold, you need a warmer one?hibernate means sleep inside the ejb container?hibernate会困难到让你这么难选择?我表示惊讶。实际上是实体bean让我们很难选择的。“这个列表可以一直开列下去,直到你不想再看下去为止。当你面前摆着无数的眼花缭乱的方案的时候,你会觉得幸福呢?还是悲哀呢?如果你是一个负责的程序员,那么你一定会仔细研究每种方案的区别,每种方案的效率,每种方案的适用场合,你会觉得你已经陷入进去拔不出来了。如果是用eb,你第一秒种就已经做出了决定,根本没得选择,比如说集合属性,你只能用collection,如果是hibernate,你会在bag,list和set之间来回犹豫不决,甚至搞不清楚的话,程序都没有办法写。 ”bag,list,set的选择很容易,甚至不需要用一分钟。在不同的场合需要不同的解决问题的方式,这不好嘛?相反,ejb面对所有问题的all-in-one方式似乎是让你不用选择,但也让你无法选择合适的解决方案。如果楼主愿意浪费你三天的宝贵时间看完hibernate到底是什么?我相信你会喜欢它而不是ejb。dao是什么啊,the book expert one-one-one j2ee development without ejb by rod johnson covers all the topics above.david linj2ee consultant melbourne有的事情不能说的那么绝对!多方面看问题。其实类似的争论在很多方面出现了,你们彼此间的争论和讨论java和c+孰优孰劣有什么区别呢?一种设计模式存在必然有他的存在理由,争个半天还不于自己去体验下2、运行效率:如果jdbc的代码写的非常优化,那么jdbc架构运行效率最高但是实际项目中,这一点几乎做不到,这需要程序员非常精通jdbc,运用batc语句,调整preapredstatement的 batch size和fetch size等参数,以及在必的情况下采用结果集cache等等。而一般情况下程序员是做不到这一点的。因此hibernate架构表现出最快的运行效率。 eb的架构效率会差的很远。不明白为什么preparedstatement的效率会高?preparedstatement pstmt = conn.preparedstatement(.);for (int i=0;ipstmt.setstring(1,.);pstmt.addbatch();pstmt.executebatch();用preparedstatement.addbatch()有什么用?在mysql的驱动中只是把循环放在了pstmt.executebatch()而已,原来要往数据庫端发送多少次封包,用了batch还是要多少次,性能一点改变也没有。我还不如果动态生成语句,然后向数据庫端发送一次封包。大言不惭!你自己写个简单的例子测试一下两种方式的插入1000条数据的效率就会明白你现在说的这些话多么可笑了。没试过我不会乱说,我插入10万条,两者差距只有3秒。你有试过吗?没有吧。你有看过mysql jdbc的驱动吗,看过再说。这是我的测试mysql4.0.18ntmysql-connector-java-3.1.4-beta-bin.jarwindow server 2003代码:import java.sql.connection;import java.sql.preparedstatement;import java.sql.statement;import java.sql.drivermanager;import java.sql.sqlexception;import perties;import com.mysql.jdbc.driver;public class batchupdatetest private final static string clear_db =truncate sales_rep;private final static string insert_into_db =insert into sales_rep values(?,?,?);public final static void main(string args) if (args.length != 2) system.out.println(usage:please enter two args);return;string type = args0;int num = 0;try num = integer.parseint(args1, 10); catch (numberformatexception nume) throw new illegalargumentexception(the second arg must be a number);if (!(update.equals(args0) | updatebatch.equals(args0) | updatestmt.equals(args0) throw new illegalargumentexception(the first arg must be update or updatebatch or updatestmt);/drivermanager.setlogstream(system.out);connection conn = null;try new driver();properties prop = new properties();prop.setproperty(user, root);prop.setproperty(useunicode, true);prop.setproperty(characterencoding, gb2312);conn = drivermanager.getconnection(jdbc:mysql:/localhost:3306/test, prop);if (args0.equals(update)update(conn, num);else if (args0.equals(updatebatch) updatebatch(conn, num); else updatestmt(conn, num); catch(sqlexception sqle) sqle.printstacktrace(); finally try if (conn != null) conn.close(); catch (sqlexception sqle) system.err.println(sqle.tostring();private static void update(connection conn, int num) throws sqlexception preparedstatement pstmt = conn.preparestatement(clear_db);pstmt.executeupdate();pstmt.close();pstmt = conn.preparestatement(insert_into_db);long starttime = system.currenttimemillis();for (int i = 0;ipstmt.setint(1, i);pstmt.setstring(2, test);pstmt.setstring(3, testdata);pstmt.executeupdate();pstmt.close();system.out.println(system.currenttimemillis() - starttime);private static void updatebatch(connection conn, int num) throws sqlexception preparedstatement pstmt = conn.preparestatement(clear_db);pstmt.executeupdate();pstmt.close();pstmt = conn.preparestatement(insert_into_db);long starttime = system.currenttimemillis();for (int i = 0;ipstmt.setint(1, i);pstmt.setstring(2, test);pstmt.setstring(3, testdata);pstmt.addbatch();pstmt.executebatch();pstmt.close();system.out.println(system.currenttimemillis() - starttime);private static void updatestmt(connection conn, int num) throws sqlexception preparedstatement ps

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论