Guest User

Untitled

a guest
Aug 29th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. Single class Composite Pattern mapping with Hibernate
  2. @Entity
  3. public class Human implements Serializable
  4. {
  5. private Human parent;
  6.  
  7. @ManyToOne
  8. @JoinColumn(name = "parent_id")
  9. public Human getParent()
  10. {
  11. return parent;
  12. }
  13.  
  14. public void setParent(Human parent)
  15. {
  16. this.parent = parent;
  17. }
  18.  
  19. private List<Human> children = new ArrayList<Human>();
  20.  
  21. @OneToMany(mappedBy = "parent", targetEntity = Human.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
  22. public List<Human> getChildren()
  23. {
  24. return children;
  25. }
  26.  
  27. @Id
  28. @GeneratedValue
  29. private Long id = null;
  30.  
  31. private int version = 0;
  32. private String name;
  33. private int age;
  34.  
  35. public String getName()
  36. {
  37. return name;
  38. }
  39.  
  40. public void setName(String name)
  41. {
  42. this.name = name;
  43. }
  44.  
  45. public int getAge()
  46. {
  47. return age;
  48. }
  49.  
  50. public void setAge(int age)
  51. {
  52. this.age = age;
  53. }
  54.  
  55. @OneToMany(mappedBy = "parent", targetEntity = Human.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
  56. public void addChild(Human child)
  57. {
  58. child.setParent(this);
  59. children.add(child);
  60. }
  61. }
  62.  
  63. public class Main
  64. {
  65.  
  66. /**
  67. * @param args
  68. */
  69. public static void main(String[] args)
  70. {
  71. System.out.println("start");
  72. Main main = new Main();
  73. main.run();
  74. main.run();
  75. System.out.println("stop");
  76. }
  77.  
  78. public void run()
  79. {
  80. Session session = HibernateUtil.getSessionFactory().getCurrentSession();//.openSession();
  81. Transaction tx = session.beginTransaction();
  82.  
  83. int len = 2;
  84. for (int i = 0; i < len; i++)
  85. {
  86. Human human = new Human();
  87. human.setName("name" + i);
  88. human.setAge(i);
  89.  
  90. session.save(human);
  91.  
  92. int clen = 2;
  93. for (int j = 0; j < clen; j++)
  94. {
  95. Human child = new Human();
  96. child.setName("cname" + j);
  97. child.setAge(j);
  98.  
  99. human.addChild(child);
  100. session.save(child);
  101. }
  102. }
  103.  
  104. tx.commit();
  105. }
  106. }
  107.  
  108. INFO: Running hbm2ddl schema export
  109. Jun 8, 2011 6:02:30 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
  110. INFO: exporting generated schema to database
  111. Jun 8, 2011 6:02:30 PM org.hibernate.tool.hbm2ddl.SchemaExport create
  112. SEVERE: Unsuccessful: create table human (id bigint not null auto_increment, age integer, name varchar(255), parent tinyblob, version integer, primary key (id))
  113. Jun 8, 2011 6:02:30 PM org.hibernate.tool.hbm2ddl.SchemaExport create
  114. SEVERE: Table 'human' already exists
  115. Jun 8, 2011 6:02:30 PM org.hibernate.tool.hbm2ddl.SchemaExport create
  116. SEVERE: Unsuccessful: alter table human add index FK5F0612DEE744FD6 (id), add constraint FK5F0612DEE744FD6 foreign key (id) references human (id)
  117. Jun 8, 2011 6:02:30 PM org.hibernate.tool.hbm2ddl.SchemaExport create
  118. SEVERE: Duplicate key name 'FK5F0612DEE744FD6'
  119. Jun 8, 2011 6:02:30 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
  120. INFO: schema export complete
  121. Hibernate: insert into human (age, name, parent, version) values (?, ?, ?, ?)
  122. Jun 8, 2011 6:02:30 PM org.hibernate.util.JDBCExceptionReporter logExceptions
  123. WARNING: SQL Error: 1054, SQLState: 42S22
  124. Jun 8, 2011 6:02:30 PM org.hibernate.util.JDBCExceptionReporter logExceptions
  125. SEVERE: Unknown column 'parent' in 'field list'
  126. Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [human.humanoid.Human]
  127. at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
  128. at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
  129. at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
  130.  
  131. public class HibernateUtil
  132. {
  133. private static final SessionFactory sessionFactory;
  134.  
  135. static
  136. {
  137. try
  138. {
  139. AnnotationConfiguration config = new AnnotationConfiguration();
  140. config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  141. config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
  142. config.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test");
  143. config.setProperty("hibernate.connection.username", "root");
  144. config.setProperty("hibernate.connection.password", "");
  145. config.setProperty("hibernate.connection.pool_size", "1");
  146. config.setProperty("hibernate.connection.autocommit", "true");
  147. config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
  148. config.setProperty("hibernate.hbm2ddl.auto", "create");
  149. config.setProperty("hibernate.show_sql", "true");
  150. config.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
  151. config.setProperty("hibernate.current_session_context_class", "thread");
  152.  
  153. config.addAnnotatedClass(Human.class);
  154.  
  155. sessionFactory = config.buildSessionFactory();
  156. }
  157. catch (Throwable ex)
  158. {
  159. System.err.println("Initial SessionFactory creation failed." + ex);
  160. throw new ExceptionInInitializerError(ex);
  161. }
  162. }
  163.  
  164. public static SessionFactory getSessionFactory()
  165. {
  166. return sessionFactory;
  167. }
  168. }
Add Comment
Please, Sign In to add comment