Advertisement
Guest User

Untitled

a guest
Jan 26th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. <?xml version='1.0' encoding='utf-8'?>
  2.  
  3. <!DOCTYPE hibernate-configuration SYSTEM "hibernate-configuration-3.0.dtd">
  4.  
  5. <hibernate-configuration>
  6.  
  7. <session-factory>
  8. <!-- Database connection settings -->
  9. <property name="connection.driver_class">org.postgresql.Driver</property>
  10. <property name="connection.url">jdbc:postgresql://localhost/5432/hibernatedb</property>
  11. <property name="connection.username">postgres</property>
  12. <property name="connection.password">password</property>
  13.  
  14. <!-- JDBC connection pool (use the built-in) -->
  15. <property name="connection.pool_size">1</property>
  16.  
  17. <!-- SQL dialect -->
  18. <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  19.  
  20. <!-- Enable Hibernate's automatic session context management -->
  21. <property name="current_session_context_class">thread</property>
  22.  
  23. <!-- Disable the second-level cache
  24. <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
  25.  
  26. <!-- Echo all executed SQL to stdout -->
  27. <property name="show_sql">true</property>
  28.  
  29. <!-- Drop and re-create the database schema on startup -->
  30. <property name="hbm2ddl.auto">create</property>
  31. <mapping class="org.javabrains.koushik.dto.UserDetails" />
  32. <!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
  33. </session-factory>
  34. </hibernate-configuration>
  35.  
  36. package org.javabrains.koushik.dto;
  37.  
  38. import javax.persistence.Entity;
  39. import javax.persistence.Id;
  40.  
  41. @Entity
  42. public class UserDetails {
  43.  
  44. @Id
  45. private int userID;
  46. private String userName;
  47.  
  48. public int getUserID() {
  49. return userID;
  50. }
  51. public void setUserID(int userID) {
  52. this.userID = userID;
  53. }
  54. public String getUserName() {
  55. return userName;
  56. }
  57. public void setUserName(String userName) {
  58. this.userName = userName;
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65. }
  66.  
  67. package org.koushik.hibernate;
  68.  
  69. import org.hibernate.Session;
  70. import org.hibernate.SessionFactory;
  71. import org.hibernate.cfg.AnnotationConfiguration;
  72.  
  73. import org.javabrains.koushik.dto.UserDetails;
  74.  
  75. public class HibernateTest {
  76.  
  77. public static void main(String[] args) {
  78.  
  79.  
  80. UserDetails user = new UserDetails();
  81. user.setUserID(1);
  82. user.setUserName("First user");
  83.  
  84. SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
  85. Session session =sessionFactory.openSession();
  86. session.beginTransaction();
  87. session.save(user);
  88. session.getTransaction().commit();
  89.  
  90. }
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement