项目作者: nfriaa

项目描述 :
Hibernate tutorial 3 : Hibernate "Many to One" association
高级语言: Java
项目地址: git://github.com/nfriaa/hibernate-tutorial4.git
创建时间: 2017-10-09T15:43:01Z
项目社区:https://github.com/nfriaa/hibernate-tutorial4

开源协议:MIT License

下载


hibernate-tutorial4

Hibernate tutorial 4 : “Many To One” association

contributions welcome Travis license

Description

A sample code to learn how to map “Many To One” relationship between two entities using the Hibernate ORM.

  • JavaSE 8
  • Hibernate 5 / Annotations
  • Hibernate “Many To One” association
  • Maven 4
  • MySQL 5

1. Database

Create only database, don’t create tables (tables will be created by Hibernate)

  • database name : persist_db
    1. CREATE DATABASE `persist_db` /*!40100 DEFAULT CHARACTER SET utf8 */

2. Maven “pom.xml” dependencies

  1. <dependencies>
  2. <!-- MySQL connector -->
  3. <dependency>
  4. <groupId>mysql</groupId>
  5. <artifactId>mysql-connector-java</artifactId>
  6. <version>6.0.6</version>
  7. </dependency>
  8. <!-- Hibernate -->
  9. <dependency>
  10. <groupId>org.hibernate</groupId>
  11. <artifactId>hibernate-core</artifactId>
  12. <version>5.2.11.Final</version>
  13. </dependency>
  14. </dependencies>

3. Hibernate configuration file “hibernate.cfg.xml”

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  3. <hibernate-configuration>
  4. <session-factory>
  5. <!-- MySQL -->
  6. <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  7. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/persist_db?useTimezone=true&serverTimezone=UTC</property>
  8. <property name="hibernate.connection.username">root</property>
  9. <property name="hibernate.connection.password"></property>
  10. <!-- Hibernate -->
  11. <property name="show_sql">true</property>
  12. <property name="format_sql">false</property>
  13. <property name="hibernate.hbm2ddl.auto">create</property>
  14. <!-- Entities -->
  15. <mapping class="net.isetjb.hibernatetutorial4.Product"></mapping>
  16. <mapping class="net.isetjb.hibernatetutorial4.Category"></mapping>
  17. </session-factory>
  18. </hibernate-configuration>
  • hibernate.hbm2ddl.auto : “create” => creates the schema necessary for defined entities, destroying any previous data
  • don’t forget to map the two entities in this XML config file (Product and Category)

4. “Many To One” association

Source entity : Product.java

  1. import javax.persistence.Column;
  2. import javax.persistence.Entity;
  3. import javax.persistence.ForeignKey;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.JoinColumn;
  8. import javax.persistence.ManyToOne;
  9. import javax.persistence.Table;
  10. @Entity
  11. @Table(name = "product")
  12. public class Product
  13. {
  14. @Id
  15. @GeneratedValue(strategy = GenerationType.IDENTITY)
  16. @Column(name = "id", nullable = false)
  17. private int id;
  18. @Column(name = "name", length = 255, nullable = true)
  19. private String name;
  20. @Column(name = "price", nullable = true)
  21. private int price;
  22. @ManyToOne
  23. @JoinColumn(name = "category_id", foreignKey = @ForeignKey(name = "CATEGORY_ID_FK"))
  24. private Category category;
  25. // Getters and Setters here...
  26. }
  • @ManyToOne : is equivalent to foreign key relationship in a database
  • @JoinColumn : name of the foreign key column in the source entity
  • @ForeignKey : name of the constraint (foreign key) in the destination entity

Destination entity : Category.java

  1. import javax.persistence.Column;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7. @Entity
  8. @Table(name = "category")
  9. public class Category
  10. {
  11. @Id
  12. @GeneratedValue(strategy = GenerationType.IDENTITY)
  13. @Column(name = "id", nullable = false)
  14. private int id;
  15. @Column(name = "name", length = 255, nullable = true)
  16. private String name;
  17. // Getters and Setters here...
  18. }

5. Main Class “Application.java”

  • create main class to test the code
  • example :

    1. // new category
    2. Category category_a = new Category();
    3. category_a.setName("Cat a");
    4. session.save(category_a);
    5. // new product
    6. Product product_x = new Product();
    7. product_x.setName("Prod x");
    8. product_x.setPrice(456);
    9. product_x.setCategory(category_a);
    10. session.save(product_x);