Advertisement
Guest User

HibernateTest.java

a guest
Apr 21st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. // DEP: org.hibernate hibernate-core 5.2.10.final
  2. // DEP: org.hsqldb hsqldb 2.4.0
  3. // DEP: dom4j dom4j 1.6.1
  4. // DEP: org.codehaus.woodstox woodstox-core-asl 4.4.1
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. import javax.persistence.Entity;
  13. import javax.persistence.GeneratedValue;
  14. import javax.persistence.GenerationType;
  15. import javax.persistence.Id;
  16.  
  17. import org.hibernate.Session;
  18. import org.hibernate.SessionFactory;
  19. import org.hibernate.boot.MetadataSources;
  20. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  21.  
  22. @Entity
  23. class A {
  24. public int id;
  25. public int x;
  26. public int y;
  27.  
  28. @Id
  29. @GeneratedValue(strategy=GenerationType.IDENTITY)
  30. public int getId() {
  31. return id;
  32. }
  33.  
  34. public void setId(int id) {
  35. this.id = id;
  36. }
  37.  
  38. public int getX() {
  39. return x;
  40. }
  41.  
  42. public void setX(int x) {
  43. this.x = x;
  44. }
  45.  
  46. public int getY() {
  47. return y;
  48. }
  49.  
  50. public void setY(int y) {
  51. this.y = y;
  52. }
  53.  
  54. public String toString() {
  55. return String.format("[%s, %s, %s]", id, x, y);
  56. }
  57. }
  58.  
  59. public class Main {
  60. public static void main(String[] args) throws Exception {
  61. runSql("drop schema public cascade");
  62. runSql("create table a ( id int generated by default as identity, x int, y int );");
  63. runSql("insert into a values ( null, 1, 2 )");
  64. runSql("insert into a values ( null, 3, 4 )");
  65.  
  66. SessionFactory sessionFactory = getSessionFactory();
  67. try {
  68. Session session = sessionFactory.openSession();
  69. try {
  70. System.out.println(session.createQuery("from A").list());
  71. } finally {
  72. session.close();
  73. }
  74. } finally {
  75. sessionFactory.close();
  76. }
  77. }
  78.  
  79. private static SessionFactory getSessionFactory() {
  80. Map<String, String> config = new HashMap<>();
  81.  
  82. config.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");
  83. config.put("hibernate.connection.username", "root");
  84. config.put("hibernate.connection.password", "");
  85. config.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
  86.  
  87. return new MetadataSources(new StandardServiceRegistryBuilder().applySettings(config).build())
  88. .addAnnotatedClass(A.class)
  89. .buildMetadata()
  90. .buildSessionFactory();
  91. }
  92.  
  93. private static void runSql(String sql) throws Exception {
  94. Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:test", "root", "");
  95. try {
  96. PreparedStatement statement = connection.prepareStatement(sql);
  97. try {
  98. statement.execute();
  99. } finally {
  100. statement.close();
  101. }
  102. } finally {
  103. connection.close();
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement