Rebuild the sessionfactory method of hibernate (do not restart the Service)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/sunyujia/

Disclaimer: This method has not been tested except for the following tests, and does not guarantee its stability. Rebuilding sessionfactory is very dangerous. This article only studies and learns.

I have not thoroughly studied the hibernate background mechanism. I just tried to help the netizens on the Forum solve a practical problem. I checked the source code of the configuration class and found it was protected, I wrote a class to inherit it. After I changed it to public, the test passed. I didn't expect it to be so simple, but it is really simple for the time being. Sorry to post this article. Since any network resources will be beneficial to people, it is still sent. A total of programmers Baidu souzhi.

This method is suitable for dynamically modifying the database table structure. For example, user-defined forms are provided.

  1. Package com. syj. domain. test;
  2. Import java. Io. file;
  3. Import org. hibernate. Session;
  4. Import org. hibernate. sessionfactory;
  5. Import org. hibernate. cfg. configuration;
  6. Import com. syj. domain. user;
  7. /**
  8. * <P>
  9. * Title: re-create the sessionfactory test class
  10. * </P>
  11. *
  12. * <P>
  13. * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
  14. * </P>
  15. *
  16. * @ Author Sun Xiaojia
  17. * @ Main sunyujia@yahoo.cn
  18. * @ Date Oct 6, 2008 8:38:11
  19. */
  20. Public class resethbmtest {
  21. /**
  22. * Description: Test Case
  23. *
  24. * @ Param ARGs
  25. * @ Throws exception
  26. * @ Blog ht tp: // blog.csdn.net/sunyujia/
  27. * @ Mail sunyujia@yahoo.cn
  28. * @ Since: Oct 6, 2008 8:38:11
  29. */
  30. Public static void main (string [] ARGs) throws exception {
  31. Hibernateconfiguration configuration = new hibernateconfiguration ();
  32. Configuration. Configure ("/hibernate. cfg. xml ");
  33. Sessionfactory = configuration. buildsessionfactory ();
  34. Session session = sessionfactory. opensession ();
  35. Session. begintransaction ();
  36. User U1 = new user ();
  37. Session. Save (U1 );
  38. Session. gettransaction (). Commit ();
  39. Session. Refresh (U1 );
  40. System. Out. println ("User ID:" + u1.getid () + "name:" + u1.getname ());
  41. Sessionfactory. Close ();
  42. Testreplacexml (); // change the pojo XML file
  43. Configuration. Reset (); // Reset
  44. Configuration. Configure ("/hibernate. cfg. xml ");
  45. Sessionfactory = configuration. buildsessionfactory (); // buildsessionfactory
  46. Session = sessionfactory. opensession ();
  47. Session. begintransaction ();
  48. User U2 = new user ();
  49. Session. Save (U2 );
  50. Session. gettransaction (). Commit ();
  51. Session. Refresh (U2 );
  52. System. Out. println ("User ID:" + u2.getid () + "name:" + u2.getname ());
  53. }
  54. /**
  55. *
  56. * Description: test the modification of user. HBM. xml. Too many simple replicas are not considered for simplicity.
  57. *
  58. * @ Throws exception
  59. * @ Mail sunyujia@yahoo.cn
  60. * @ Since: Oct 6, 2008 8:55:28
  61. */
  62. Public static void testreplacexml () throws exception {
  63. File file = new file (user. Class. getresource ("user. HBM. xml"). GetFile ());
  64. String userhbmxml = org. Apache. commons. Io. fileutils. readfiletostring (
  65. File, "UTF-8 ");
  66. Userhbmxml = userhbmxml. replaceall ("(select 1)", "(select 2 )");
  67. Org. Apache. commons. Io. fileutils. writestringtofile (file, userhbmxml );
  68. }
  69. }
  70. Class hibernateconfiguration extends configuration {
  71. Public hibernateconfiguration (){
  72. Super ();
  73. }
  74. Public void reset (){
  75. Super. Reset (); // The key here is that the method of protected is inherited and converted to public
  76. }
  77. }

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
  3. Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
  4. <Hibernate-mapping>
  5. <! -- Ht tp: // blog.csdn.net/sunyujia/ -->
  6. <Class name = "com. syj. domain. User" table = "syj_user" schema = "DBO" catalog = "pubs">
  7. <ID name = "ID" type = "Java. Lang. Integer">
  8. <Column name = "ID"/>
  9. <Generator class = "Identity"/>
  10. </ID>
  11. <Property name = "name" type = "Java. Lang. String">
  12. <Formula> (select 1) </formula>
  13. </Property>
  14. </Class>
  15. </Hibernate-mapping>

The test results are as follows:

User ID: 38 name: 1

User ID: 39 name: 2

It can be seen that it has taken effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.