Basic hibernate instance

Source: Internet
Author: User
Basic hibernate instance:

  1. Package com. javamodel. hibernate;
  2. Import java. Io. ioexception;
  3. Import java. Io. inputstream;
  4. Import java. util. properties;
  5. Import net. SF. hibernate. hibernateexception;
  6. Import net. SF. hibernate. mappingexception;
  7. Import net. SF. hibernate. Session;
  8. Import net. SF. hibernate. sessionfactory;
  9. Import net. SF. hibernate. transaction;
  10. Import net. SF. hibernate. cfg. configuration;
  11. Public class example {
  12. Private Static sessionfactory _ sessions = NULL;
  13. Private Static Properties pops = new properties ();
  14. Static {
  15. Try {
  16. Inputstream stream = example. Class. getresourceasstream ("hibernate. properties ");
  17. Try {
  18. Pops. Load (Stream );
  19. } Catch (ioexception E1 ){
  20. E1.printstacktrace ();
  21. }
  22. Configuration CFG = new configuration ();
  23. Cfg. addclass (person. Class );
  24. Cfg. setproperties (POPs );
  25. _ Sessions = cfg. buildsessionfactory ();
  26. } Catch (mappingexception e ){
  27. E. printstacktrace ();
  28. } Catch (hibernateexception e ){
  29. E. printstacktrace ();
  30. }
  31. }
  32. Public static void main (string [] ARGs) throws hibernateexception {
  33. Person = new person ();
  34. Person. setname ("smallduzi ");
  35. Person. setemail ("smallduzi@sohu.com ");
  36. Session session = _ sessions. opensession ();
  37. Transaction Tx = NULL;
  38. Try {
  39. Tx = session. begintransaction ();
  40. Session. Save (person );
  41. TX. Commit ();
  42. } Catch (hibernateexception he ){
  43. If (TX! = NULL) Tx. rollback ();
  44. Throw he;
  45. }
  46. Finally {
  47. Session. Close ();
  48. }
  49. }
  50. }


Person. Java code

  1. Package com. javamodel. hibernate;
  2. Public class person {
  3. Private string id = NULL;
  4. Private string name = NULL;
  5. Private string email = NULL;
  6. Public Person (){}
  7. /**
  8. * @ Return
  9. */
  10. Public String getemail (){
  11. Return email;
  12. }
  13. /**
  14. * @ Return
  15. */
  16. Public String GETID (){
  17. Return ID;
  18. }
  19. /**
  20. * @ Return
  21. */
  22. Public String getname (){
  23. Return name;
  24. }
  25. /**
  26. * @ Param string
  27. */
  28. Public void setemail (string ){
  29. Email = string;
  30. }
  31. /**
  32. * @ Param string
  33. */
  34. Public void setid (string ){
  35. Id = string;
  36. }
  37. /**
  38. * @ Param string
  39. */
  40. Public void setname (string ){
  41. Name = string;
  42. }
  43. }


Person. HBM. XML Code

  1. <? XML version = "1.0"?>
  2. <! Doctype hibernate-Mapping System "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
  3. <Hibernate-mapping>
  4. <Class name = "com. javamodel. hibernate. Person" table = "person">
  5. <ID name = "ID">
  6. <Column name = "ID" length = "40"/>
  7. <Generator class = "UUID. Hex"/>
  8. </ID>
  9. <Property name = "name" column = "name"/>
  10. <Property name = "email" column = "email"/>
  11. </Class>
  12. </Hibernate-mapping>


Hibernate. properties code

  1. # Oracle
  2. Hibernate. dialect net. SF. hibernate. dialect. oracledialect
  3. Hibernate. Connection. driver_class oracle. JDBC. Driver. oracledriver
  4. Hibernate. Connection. Username xxx
  5. Hibernate. Connection. Password xxx
  6. # Hibernate. Connection. url JDBC: oracle: thin: @ 192.168.0.28: 1521: orcl
  7. Hibernate. Connection. url JDBC: oracle: oci8: @ xxx

 

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.