Guest User

Untitled

a guest
Aug 22nd, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. @Entity
  2. @Table(name = "student")
  3. public class Student {
  4.  
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_generator")
  7. @SequenceGenerator(name = "student_generator", sequenceName = "student_sequence", allocationSize = 10)
  8. @Column(name = "student_id")
  9. private long id;
  10.  
  11. @Column(name = "student_name")
  12. private String name;
  13.  
  14. @Column(name = "student_age")
  15. private int age;
  16.  
  17. @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
  18. @JoinColumn(name = "book_id")
  19. private Set<Books> books = new HashSet<Books>();
  20.  
  21. public Student() {
  22.  
  23. }
  24.  
  25. public Student(long id) {
  26. this.id = id;
  27. }
  28.  
  29. public void setId(long id) {
  30. this.id = id;
  31. }
  32.  
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36.  
  37. public void setAge(int age) {
  38. this.age = age;
  39. }
  40.  
  41. public void setBooks(Set<Books> books) {
  42. this.books = books;
  43. }
  44.  
  45. public Set<Books> getBooks() {
  46. return books;
  47. }
  48.  
  49. public long getId() {
  50. return id;
  51. }
  52.  
  53. public String getName() {
  54. return name;
  55. }
  56.  
  57. public int getAge() {
  58. return age;
  59. }
  60.  
  61. public void addBook(Books book) {
  62. books.add(book);
  63. book.setStudent(this);
  64. }
  65.  
  66. public void removeBook(Books book) {
  67. books.remove(book);
  68. book.setStudent(null);
  69. }
  70.  
  71. @Override
  72. public String toString() {
  73. return "Student N: " + id + " name: " + name + " age: " + age;
  74. }
  75.  
  76. }
  77.  
  78. @Entity
  79. @Table(name = "book")
  80. public class Books {
  81.  
  82. @Id
  83. @GeneratedValue(strategy = GenerationType.IDENTITY)
  84. @Column(name = "book_id")
  85. private long id;
  86.  
  87. @Column(name = "bookName")
  88. private String bookName;
  89.  
  90. @Column(name = "genre")
  91. private String genre;
  92.  
  93. @ManyToOne(fetch = FetchType.LAZY)
  94. @JoinColumn(name = "student_id")
  95. private Student student;
  96.  
  97. public Books() {
  98. }
  99.  
  100. public void setId(long id) {
  101. this.id = id;
  102. }
  103.  
  104. public void setBookName(String bookName) {
  105. this.bookName = bookName;
  106. }
  107.  
  108. public void setGenre(String genre) {
  109. this.genre = genre;
  110. }
  111.  
  112. public long getId() {
  113. return id;
  114. }
  115.  
  116. public String getBookName() {
  117. return bookName;
  118. }
  119.  
  120. public String getGenre() {
  121. return genre;
  122. }
  123.  
  124. public void setStudent(Student student) {
  125. this.student = student;
  126. }
  127.  
  128. public Student getStudent() {
  129. return student;
  130. }
  131.  
  132.  
  133. @Override
  134. public boolean equals(Object obj) {
  135. if (this == obj) return true;
  136. if (!(obj instanceof Books)) return false;
  137. return id == ((Books) obj).id;
  138. }
  139.  
  140. @Override
  141. public int hashCode() {
  142. return 31;
  143. }
  144.  
  145.  
  146. }
  147.  
  148. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  149. <dependency>
  150. <groupId>mysql</groupId>
  151. <artifactId>mysql-connector-java</artifactId>
  152. <version>8.0.12</version>
  153. </dependency>
  154.  
  155. <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
  156. <dependency>
  157. <groupId>org.hibernate</groupId>
  158. <artifactId>hibernate-core</artifactId>
  159. <version>5.3.4.Final</version>
  160. </dependency>
  161.  
  162. <?xml version="1.0" encoding="UTF-8"?>
  163. <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  164. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  165. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  166. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  167. <persistence-unit name="persistence">
  168.  
  169. <properties>
  170. <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
  171. <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/javahelps"/>
  172. <property name="hibernate.connection.autocommit" value="false"/>
  173. <property name="hibernate.connection.username" value="root"/>
  174. <property name="hibernate.connection.password" value="12345"/>
  175. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
  176. <property name="hibernate.connection.CharSet" value="utf8"/>
  177. <property name="hibernate.connection.characterEncoding" value="utf8"/>
  178. <property name="hibernate.connection.useUnicode" value="true"/>
  179. <property name="hibernate.show_sql" value="true"/>
  180. <property name="hibernate.hbm2ddl.auto" value="update"/>
  181. </properties>
  182. </persistence-unit>
  183. </persistence>
  184.  
  185. public class Main {
  186.  
  187. public static void main(String... main) {
  188. EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence");
  189. EntityManager entityManager = entityManagerFactory.createEntityManager();
  190.  
  191. Student student1 = new Student();
  192. student1.setName("Lucas");
  193. student1.setAge(21);
  194.  
  195. Books book1 = new Books();
  196. book1.setBookName("Dark Tower");
  197. book1.setGenre("Dark Fantasy");
  198.  
  199. Books book2 = new Books();
  200. book1.setBookName("It");
  201. book1.setGenre("Horror");
  202.  
  203. student1.addBook(book1);
  204. student1.addBook(book2);
  205.  
  206.  
  207. entityManager.getTransaction().begin();
  208.  
  209. // Произвожу транзакцию и пытаюсь создать в таблице student одну запись, а в book две записи
  210. entityManager.persist(student1);
  211.  
  212. entityManager.getTransaction().commit();
  213.  
  214.  
  215. entityManager.close();
  216.  
  217.  
  218. }
  219. }
  220.  
  221. mysql> select * from student;
  222. +------------+-------------+--------------+
  223. | student_id | student_age | student_name |
  224. +------------+-------------+--------------+
  225. | 52 | 21 | Lucas |
  226. +------------+-------------+--------------+
  227. 1 row in set (0,14 sec)
  228.  
  229. mysql> select * from book;
  230. +---------+----------+--------+------------+
  231. | book_id | bookName | genre | student_id |
  232. +---------+----------+--------+------------+
  233. | 52 | It | Horror | 52 |
  234. +---------+----------+--------+------------+
  235. 1 row in set (0,04 sec)
  236.  
  237. Student student1 = new Student();
  238. student1.setName("Lucas");
  239. student1.setAge(21);
  240.  
  241. Books book1 = new Books();
  242. book1.setBookName("Dark Tower");
  243. book1.setGenre("Dark Fantasy");
  244.  
  245. Books book2 = new Books();
  246. book1.setBookName("It");
  247. book1.setGenre("Horror");
  248.  
  249. entityManager.getTransaction().begin();
  250.  
  251. // Вот так можно создать в таблице student одну запись, а в book две записи
  252. entityManager.persist(student1);
  253. book1.setSudent(student1);
  254. entityManager.persist(book1);
  255. book2.setSudent(student1);
  256. entityManager.persist(book2);
  257.  
  258.  
  259. entityManager.getTransaction().commit();
  260.  
  261.  
  262. entityManager.close();
Add Comment
Please, Sign In to add comment