Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. public class Main {
  2. public static void main(String[] args) {
  3. SessionFactory sessionFactory = new HibernateUtil().getSessionFactory();
  4. Session session = sessionFactory.openSession();
  5. session.beginTransaction();
  6.  
  7. Role role = new Role();
  8. role.setTitle("ex-president");
  9.  
  10. User user1 = new User(100,"Barack","Obama");
  11. User user2 = new User(100,"Ronald","Reagan");
  12.  
  13. user1.setRole(role);
  14. user2.setRole(role);
  15.  
  16. session.save(user1);
  17. session.save(user2);
  18.  
  19. role.getUsers().add(user1);
  20. role.getUsers().add(user2);
  21.  
  22. session.save(role);
  23.  
  24. session.getTransaction().commit();
  25. session.close();
  26. }}
  27.  
  28. public class HibernateUtil {
  29. private static SessionFactory sessionFactory;
  30.  
  31. static {
  32. Configuration configuration = new Configuration();
  33. configuration.configure("hibernate.cfg.xml");
  34. StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
  35. sessionFactory = configuration.buildSessionFactory(ssrb.build());
  36. }
  37.  
  38. public SessionFactory getSessionFactory(){
  39. return sessionFactory;
  40. }}
  41.  
  42. @Entity
  43. public class Role {
  44. private Long role_id;
  45.  
  46. private String title;
  47.  
  48. private Set<User> users = new HashSet<User>();
  49.  
  50. public Role(){}
  51.  
  52. public Set<User> getUsers() {
  53. return users;
  54. }
  55.  
  56. public void addUser(User user){
  57. users.add(user);
  58. }
  59.  
  60. public void setUsers(Set<User> users) {
  61. this.users = users;
  62. }
  63.  
  64. public Long getRole_id() {
  65. return role_id;
  66. }
  67.  
  68. public void setRole_id(Long id) {
  69. this.role_id = id;
  70. }
  71.  
  72. public String getTitle() {
  73. return title;
  74. }
  75.  
  76. public void setTitle(String title) {
  77. this.title = title;
  78. }}
  79.  
  80. @Entity
  81. public class User {
  82.  
  83. private long user_id;
  84.  
  85. private int age;
  86.  
  87. private String firstname;
  88.  
  89. private String lastname;
  90.  
  91. private Role role;
  92.  
  93. public User(){
  94.  
  95. }
  96.  
  97. public Role getRole() {
  98. return role;
  99. }
  100.  
  101. public void setRole(Role role) {
  102. this.role = role;
  103. }
  104.  
  105. public User(int age, String firstname, String lastname) {
  106. this.age = age;
  107. this.firstname = firstname;
  108. this.lastname = lastname;
  109. }
  110.  
  111. public long getUser_id() {
  112. return user_id;
  113. }
  114.  
  115. public void setUser_id(long id) {
  116. this.user_id = id;
  117. }
  118.  
  119. public int getAge() {
  120. return age;
  121. }
  122.  
  123. public void setAge(int age) {
  124. this.age = age;
  125. }
  126.  
  127. public String getFirstname() {
  128. return firstname;
  129. }
  130.  
  131. public void setFirstname(String firstname) {
  132. this.firstname = firstname;
  133. }
  134.  
  135. public String getLastname() {
  136. return lastname;
  137. }
  138.  
  139. public void setLastname(String lastname) {
  140. this.lastname = lastname;
  141. }}
  142.  
  143. <?xml version='1.0' encoding='utf-8'?>
  144. <!DOCTYPE hibernate-configuration PUBLIC
  145. "-//Hibernate/Hibernate Configuration DTD//EN"
  146. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  147. <hibernate-configuration>
  148. <session-factory>
  149. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  150. <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  151. <property name="connection.username">root</property>
  152. <property name="connection.password">root</property>
  153. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  154. <property name="hbm2ddl.auto">create</property>
  155. <property name="show_sql">true</property>
  156. <property name="format_sql">true</property>
  157. <mapping resource="user.cfg.xml"/>
  158. <mapping resource="role.cfg.xml"/>
  159. </session-factory>
  160. </hibernate-configuration>
  161.  
  162. <?xml version="1.0" encoding="utf-8"?>
  163. <!DOCTYPE hibernate-mapping PUBLIC
  164. "-//Hibernate/Hibernate Mapping DTD//EN"
  165. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  166. <hibernate-mapping>
  167. <class name="Role" table="role">
  168. <id name="role_id" column="id">
  169. <generator class="native"/>
  170. </id>
  171. <property name="title" column="TITLE"/>
  172. <set name="users" cascade="all"
  173. inverse="true" lazy="true" fetch="select">
  174. <key column="id" not-null="true"/>
  175. <one-to-many class="User"/>
  176. </set>
  177. </class>
  178. </hibernate-mapping>
  179.  
  180. <?xml version="1.0" encoding="utf-8"?>
  181. <!DOCTYPE hibernate-mapping PUBLIC
  182. "-//Hibernate/Hibernate Mapping DTD//EN"
  183. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  184. <hibernate-mapping>
  185. <class name="User" table="user">
  186. <id name="user_id" column="user_id">
  187. <generator class="native"/>
  188. </id>
  189. <property name="firstname" column="firstname"/>
  190. <property name="lastname" column="lastname"/>
  191. <property name="age" column="age"/>
  192. <many-to-one name="role" class="Role" fetch="select">
  193. <column name="id" not-null="true"/>
  194. </many-to-one>
  195. </class>
  196. </hibernate-mapping>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement