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.
- Package com. syj. domain. test;
- Import java. Io. file;
- Import org. hibernate. Session;
- Import org. hibernate. sessionfactory;
- Import org. hibernate. cfg. configuration;
- Import com. syj. domain. user;
- /**
- * <P>
- * Title: re-create the sessionfactory test class
- * </P>
- *
- * <P>
- * Copyright: Reprinted please indicate the source http://blog.csdn.net/sunyujia/
- * </P>
- *
- * @ Author Sun Xiaojia
- * @ Main sunyujia@yahoo.cn
- * @ Date Oct 6, 2008 8:38:11
- */
- Public class resethbmtest {
- /**
- * Description: Test Case
- *
- * @ Param ARGs
- * @ Throws exception
- * @ Blog ht tp: // blog.csdn.net/sunyujia/
- * @ Mail sunyujia@yahoo.cn
- * @ Since: Oct 6, 2008 8:38:11
- */
- Public static void main (string [] ARGs) throws exception {
- Hibernateconfiguration configuration = new hibernateconfiguration ();
- Configuration. Configure ("/hibernate. cfg. xml ");
- Sessionfactory = configuration. buildsessionfactory ();
- Session session = sessionfactory. opensession ();
- Session. begintransaction ();
- User U1 = new user ();
- Session. Save (U1 );
- Session. gettransaction (). Commit ();
- Session. Refresh (U1 );
- System. Out. println ("User ID:" + u1.getid () + "name:" + u1.getname ());
- Sessionfactory. Close ();
- Testreplacexml (); // change the pojo XML file
- Configuration. Reset (); // Reset
- Configuration. Configure ("/hibernate. cfg. xml ");
- Sessionfactory = configuration. buildsessionfactory (); // buildsessionfactory
- Session = sessionfactory. opensession ();
- Session. begintransaction ();
- User U2 = new user ();
- Session. Save (U2 );
- Session. gettransaction (). Commit ();
- Session. Refresh (U2 );
- System. Out. println ("User ID:" + u2.getid () + "name:" + u2.getname ());
- }
- /**
- *
- * Description: test the modification of user. HBM. xml. Too many simple replicas are not considered for simplicity.
- *
- * @ Throws exception
- * @ Mail sunyujia@yahoo.cn
- * @ Since: Oct 6, 2008 8:55:28
- */
- Public static void testreplacexml () throws exception {
- File file = new file (user. Class. getresource ("user. HBM. xml"). GetFile ());
- String userhbmxml = org. Apache. commons. Io. fileutils. readfiletostring (
- File, "UTF-8 ");
- Userhbmxml = userhbmxml. replaceall ("(select 1)", "(select 2 )");
- Org. Apache. commons. Io. fileutils. writestringtofile (file, userhbmxml );
- }
- }
- Class hibernateconfiguration extends configuration {
- Public hibernateconfiguration (){
- Super ();
- }
- Public void reset (){
- Super. Reset (); // The key here is that the method of protected is inherited and converted to public
- }
- }
- <? XML version = "1.0" encoding = "UTF-8"?>
- <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
- Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
- <Hibernate-mapping>
- <! -- Ht tp: // blog.csdn.net/sunyujia/ -->
- <Class name = "com. syj. domain. User" table = "syj_user" schema = "DBO" catalog = "pubs">
- <ID name = "ID" type = "Java. Lang. Integer">
- <Column name = "ID"/>
- <Generator class = "Identity"/>
- </ID>
- <Property name = "name" type = "Java. Lang. String">
- <Formula> (select 1) </formula>
- </Property>
- </Class>
- </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.