Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "student")
- public class Student {
- @Id
- @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "student_generator")
- @SequenceGenerator(name = "student_generator", sequenceName = "student_sequence", allocationSize = 10)
- @Column(name = "student_id")
- private long id;
- @Column(name = "student_name")
- private String name;
- @Column(name = "student_age")
- private int age;
- @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
- @JoinColumn(name = "book_id")
- private Set<Books> books = new HashSet<Books>();
- public Student() {
- }
- public Student(long id) {
- this.id = id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public void setBooks(Set<Books> books) {
- this.books = books;
- }
- public Set<Books> getBooks() {
- return books;
- }
- public long getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public void addBook(Books book) {
- books.add(book);
- book.setStudent(this);
- }
- public void removeBook(Books book) {
- books.remove(book);
- book.setStudent(null);
- }
- @Override
- public String toString() {
- return "Student N: " + id + " name: " + name + " age: " + age;
- }
- }
- @Entity
- @Table(name = "book")
- public class Books {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "book_id")
- private long id;
- @Column(name = "bookName")
- private String bookName;
- @Column(name = "genre")
- private String genre;
- @ManyToOne(fetch = FetchType.LAZY)
- @JoinColumn(name = "student_id")
- private Student student;
- public Books() {
- }
- public void setId(long id) {
- this.id = id;
- }
- public void setBookName(String bookName) {
- this.bookName = bookName;
- }
- public void setGenre(String genre) {
- this.genre = genre;
- }
- public long getId() {
- return id;
- }
- public String getBookName() {
- return bookName;
- }
- public String getGenre() {
- return genre;
- }
- public void setStudent(Student student) {
- this.student = student;
- }
- public Student getStudent() {
- return student;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) return true;
- if (!(obj instanceof Books)) return false;
- return id == ((Books) obj).id;
- }
- @Override
- public int hashCode() {
- return 31;
- }
- }
- <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.12</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>5.3.4.Final</version>
- </dependency>
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
- http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
- <persistence-unit name="persistence">
- <properties>
- <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
- <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/javahelps"/>
- <property name="hibernate.connection.autocommit" value="false"/>
- <property name="hibernate.connection.username" value="root"/>
- <property name="hibernate.connection.password" value="12345"/>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
- <property name="hibernate.connection.CharSet" value="utf8"/>
- <property name="hibernate.connection.characterEncoding" value="utf8"/>
- <property name="hibernate.connection.useUnicode" value="true"/>
- <property name="hibernate.show_sql" value="true"/>
- <property name="hibernate.hbm2ddl.auto" value="update"/>
- </properties>
- </persistence-unit>
- </persistence>
- public class Main {
- public static void main(String... main) {
- EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistence");
- EntityManager entityManager = entityManagerFactory.createEntityManager();
- Student student1 = new Student();
- student1.setName("Lucas");
- student1.setAge(21);
- Books book1 = new Books();
- book1.setBookName("Dark Tower");
- book1.setGenre("Dark Fantasy");
- Books book2 = new Books();
- book1.setBookName("It");
- book1.setGenre("Horror");
- student1.addBook(book1);
- student1.addBook(book2);
- entityManager.getTransaction().begin();
- // Произвожу транзакцию и пытаюсь создать в таблице student одну запись, а в book две записи
- entityManager.persist(student1);
- entityManager.getTransaction().commit();
- entityManager.close();
- }
- }
- mysql> select * from student;
- +------------+-------------+--------------+
- | student_id | student_age | student_name |
- +------------+-------------+--------------+
- | 52 | 21 | Lucas |
- +------------+-------------+--------------+
- 1 row in set (0,14 sec)
- mysql> select * from book;
- +---------+----------+--------+------------+
- | book_id | bookName | genre | student_id |
- +---------+----------+--------+------------+
- | 52 | It | Horror | 52 |
- +---------+----------+--------+------------+
- 1 row in set (0,04 sec)
- Student student1 = new Student();
- student1.setName("Lucas");
- student1.setAge(21);
- Books book1 = new Books();
- book1.setBookName("Dark Tower");
- book1.setGenre("Dark Fantasy");
- Books book2 = new Books();
- book1.setBookName("It");
- book1.setGenre("Horror");
- entityManager.getTransaction().begin();
- // Вот так можно создать в таблице student одну запись, а в book две записи
- entityManager.persist(student1);
- book1.setSudent(student1);
- entityManager.persist(book1);
- book2.setSudent(student1);
- entityManager.persist(book2);
- entityManager.getTransaction().commit();
- entityManager.close();
- Books book1 = new Books();
- **book1**.setBookName("Dark Tower");
- **book1**.setGenre("Dark Fantasy");
- Books book2 = new Books();
- **book1**.setBookName("It");
- **book1**.setGenre("Horror");
Add Comment
Please, Sign In to add comment