Guest User

Untitled

a guest
Feb 10th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  3. <hibernate-configuration>
  4. <session-factory>
  5. <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
  6. <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
  7. <property name="hibernate.connection.url">jdbc:derby://localhost:1527/XE</property>
  8. <property name="hibernate.connection.username">username</property>
  9. <property name="hibernate.connection.password">password</property>
  10. </session-factory>
  11. </hibernate-configuration>
  12.  
  13. <property name="hibernateProperties">
  14. <props>
  15. <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
  16. <prop key="hibernate.hbm2ddl.auto">update</prop>
  17. </props>
  18. </property>
  19.  
  20. <property name="hibernateProperties">
  21. <props>
  22. <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
  23. <prop key="hibernate.show_sql"></prop>
  24. <prop key="hibernate.use_outer_join">true</prop>
  25. </props>
  26. </property>
  27.  
  28. <bean id="mySessionFactory"
  29. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  30. <property name="hibernateProperties">
  31. <props>
  32. <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
  33. <prop key="hibernate.show_sql"></prop>
  34. <prop key="hibernate.use_outer_join">true</prop>
  35. <prop key="hibernate.jdbc.batch_size" >30</prop>
  36. <prop key="hibernate.connection.SetBigStringTryClob">true</prop>
  37. </props>
  38. </property>
  39. <property name="packagesToScan">
  40. <list>
  41. <value>mypackage</value>
  42. </list>
  43. </property>
  44. </bean>
  45.  
  46. public class TestHibernate {
  47.  
  48. public static void main(String arg[]) {
  49. Properties prop= new Properties();
  50.  
  51. prop.setProperty("hibernate.connection.url", "jdbc:mysql://<your-host>:<your-port>/<your-dbname>");
  52.  
  53. //You can use any database you want, I had it configured for Postgres
  54. prop.setProperty("dialect", "org.hibernate.dialect.PostgresSQL");
  55.  
  56. prop.setProperty("hibernate.connection.username", "<your-user>");
  57. prop.setProperty("hibernate.connection.password", "<your-password>");
  58. prop.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver");
  59. prop.setProperty("show_sql", true); //If you wish to see the generated sql query
  60.  
  61. SessionFactory sessionFactory = new Configuration().addProperties(prop).buildSessionFactory();
  62. Session session = sessionFactory.openSession();
  63. session.beginTransaction();
  64. Customer user = new Customer(); //Note customer is a POJO maps to the customer table in the database.
  65.  
  66. user.setName("test");
  67. user.setisActive(true);
  68. session.save(user);
  69. session.getTransaction().commit();
  70. session.close();
  71.  
  72. }
  73.  
  74. }
  75.  
  76.  
  77. @Entity
  78. @Table(name = "customer", uniqueConstraints = {
  79. @UniqueConstraint(columnNames = "customerid")})
  80. public class Customer implements Serializable{
  81.  
  82. private String name;
  83. private int customerid;
  84. private boolean isActive;
  85.  
  86. public Customer() {
  87. }
  88.  
  89. public Customer(String name, int customerId, boolean isActive) {
  90. this.name = name;
  91. this.customerid = customerId;
  92. this.isActive = isActive;
  93. }
  94.  
  95. /**
  96. * GETTERS
  97. */
  98.  
  99. @Column(name = "name", unique = false, nullable = false, length = 100)
  100. public String getname() {
  101. return name;
  102. }
  103.  
  104. @Id
  105. @GeneratedValue(strategy = IDENTITY)
  106. @Column(name = "customerid", unique = true, nullable = false)
  107. public int getcustomerid() {
  108. return customerid;
  109. }
  110.  
  111. @Column(name = "isactive", unique = false, nullable = false)
  112. public boolean getisactive() {
  113. return isActive;
  114. }
  115.  
  116.  
  117. /**
  118. * SETTERS
  119. */
  120. public void setname(String name) {
  121. this.name = name;
  122. }
  123.  
  124. public void setisactive(boolean isActive) {
  125. this.isActive = isActive;
  126. }
  127. }
Add Comment
Please, Sign In to add comment