Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. package org.hibenates.dto;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5.  
  6. @Entity
  7. public class UserDetails {
  8. @Id
  9. private int userId;
  10. private String userName;
  11.  
  12. public int getUserId() {
  13. return userId;
  14. }
  15.  
  16. public void setUserId(int userId) {
  17. this.userId = userId;
  18. }
  19.  
  20. public String getUserName() {
  21. return userName;
  22. }
  23.  
  24. public void setUserName(String userName) {
  25. this.userName = userName;
  26. }
  27. }
  28.  
  29. package org.hibenates;
  30.  
  31. import org.hibenates.dto.UserDetails;
  32. import org.hibernate.Session;
  33. import org.hibernate.SessionFactory;
  34. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  35. import org.hibernate.cfg.Configuration;
  36. import org.hibernate.service.ServiceRegistry;
  37.  
  38. public class HibernateTest {
  39.  
  40. public static void main(String[] args) {
  41. UserDetails details = new UserDetails();
  42. details.setUserId(1);
  43. details.setUserName("First User");
  44. Session session = null;
  45. try {
  46. /* Create a Session factory */
  47. Configuration configuration = new Configuration();
  48. configuration.configure();
  49. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  50. .applySettings(configuration.getProperties()).build();
  51. SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  52. /* Create sessions from session factory */
  53. session = sessionFactory.openSession();
  54. session.beginTransaction();
  55. /* Use the session to do the operations on model objects */
  56. session.save(details);
  57. } catch (Exception e) {
  58. System.out.println(e);
  59. } finally {
  60. session.getTransaction().commit();
  61. }
  62.  
  63. /* close session */
  64.  
  65. }
  66.  
  67. }
  68.  
  69. <?xml version='1.0' encoding='utf-8'?>
  70. <!DOCTYPE hibernate-configuration PUBLIC
  71. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  72. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  73.  
  74. <hibernate-configuration>
  75.  
  76. <session-factory>
  77.  
  78. <!-- Database connection settings -->
  79. <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
  80. <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->
  81.  
  82. <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  83. <property name="connection.url">jdbc:sqlserver://U0138039-TPD-ASQLEXPRESS:1433;databaseName=HibernatesDataBase</property>
  84. <property name="connection.username">sa</property>
  85. <property name="connection.password">T!ger123</property>
  86.  
  87. <!-- JDBC connection pool (use the built-in) -->
  88. <property name="connection.pool_size">1</property>
  89.  
  90. <!-- SQL dialect -->
  91. <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
  92.  
  93. <!-- Enable Hibernate's automatic session context management -->
  94. <property name="current_session_context_class">thread</property>
  95.  
  96. <!-- Disable the second-level cache -->
  97. <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
  98.  
  99. <!-- Echo all executed SQL to stdout -->
  100. <property name="show_sql">true</property>
  101.  
  102. <!-- Drop and re-create the database schema on startup -->
  103. <property name="hbm2ddl.auto">Create</property>
  104.  
  105. <!-- Names the annotated entity Class -->
  106. <mapping class="org.hibenates.dto.UserDetails" />
  107.  
  108.  
  109. </session-factory>
  110.  
  111. </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement