1、Annotation 注解版
1.1、在多的一方加外键
1.2、创建Customer类和Order类
1 package com.shore.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.Id; 5 import javax.persistence.Table; 6 7 /** 8 * @author DSHORE/2019-9-19 9 * 多对一,单向关联(注解版) 10 */ 11 @Entity 12 @Table(name="anno_customer") 13 public class Customer {//顾客 (“一”的一方) 14 private Integer id; 15 private String name; 16 private Integer age; 17 18 @Id 19 public Integer getId() { 20 return id; 21 } 22 public void setId(Integer id) { 23 this.id = id; 24 } 25 public String getName() { 26 return name; 27 } 28 public void setName(String name) { 29 this.name = name; 30 } 31 public Integer getAge() { 32 return age; 33 } 34 public void setAge(Integer age) { 35 this.age = age; 36 } 37 }
Order类
1 package com.shore.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.Id; 5 import javax.persistence.JoinColumn; 6 import javax.persistence.ManyToOne; 7 import javax.persistence.Table; 8 9 /** 10 * @author DSHORE/2019-9-19 11 * 多对一,单向关联(注解版) 12 */ 13 @Entity 14 @Table(name="anno_order") //Order是MySQL数据库关键字。需重新定义表名 15 public class Order {//订单 (“多”的一方),在多的一方加外键 16 private Integer id; 17 private String number; 18 private Float sum; 19 private Customer customer; //映射外键【看他的getCustomer()方法处】 20 21 @Id 22 public Integer getId() { 23 return id; 24 } 25 public void setId(Integer id) { 26 this.id = id; 27 } 28 public String getNumber() { 29 return number; 30 } 31 public void setNumber(String number) { 32 this.number = number; 33 } 34 public Float getSum() { 35 return sum; 36 } 37 public void setSum(Float sum) { 38 this.sum = sum; 39 } 40 41 @ManyToOne //多对一 42 @JoinColumn(name="customerId") //外键 43 public Customer getCustomer() { 44 return customer; 45 } 46 public void setCustomer(Customer customer) { 47 this.customer = customer; 48 } 49 }
1.3、创建hibernate.cfg.xml核心配置文件
1 xml version='1.0' encoding='utf-8'?> 2 DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 9 <property name="connection.driver_class">com.mysql.jdbc.Driverproperty> 10 <property name="connection.url">jdbc:mysql://localhost:3306/hibernateproperty> 11 <property name="connection.username">rootproperty> 12 <property name="connection.password">rootproperty> 13 14 <property name="dialect">org.hibernate.dialect.MySQLDialectproperty> 15 <property name="cache.provider_class">org.hibernate.cache.NoCacheProviderproperty> 16 <property name="show_sql">trueproperty> 17 <property name="hbm2ddl.auto">createproperty> 18 19 <mapping class="com.shore.model.Customer" /> 20 <mapping class="com.shore.model.Order" /> 21 session-factory> 22 hibernate-configuration>
1.4、开始测试
1 package com.shore.test; 2 3 import org.hibernate.cfg.AnnotationConfiguration; 4 import org.hibernate.tool.hbm2ddl.SchemaExport; 5 import org.junit.Test; 6 7 /** 8 * @author DSHORE/2019-9-19 9 * 10 */ 11 public class AnnotationTest { 12 @Test 13 public void test() {//简单测试,只创建表,不插入数据 14 new SchemaExport(new AnnotationConfiguration().configure()).create( 15 false, true); 16 } 17 }
测试结果图:
2、XML版 的实现
2.1、创建Customer类和Order类
1 package com.shore.model; 2 3 /** 4 * @author DSHORE/2019-9-19 5 * 多对一,单向关联(xml版) 6 */ 7 public class Customer {//顾客 (“一”的一方) 8 private Integer id; 9 private String name; 10 private Integer age; 11 12 public Integer getId() { 13 return id; 14 } 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 public String getName() { 19 return name; 20 } 21 public void setName(String name) { 22 this.name = name; 23 } 24 public Integer getAge() { 25 return age; 26 } 27 public void setAge(Integer age) { 28 this.age = age; 29 } 30 }
Order类
1 package com.shore.model; 2 3 /** 4 * @author DSHORE/2019-9-19 5 * 多对一,单向关联(xml版) 6 */ 7 public class Order {//订单 (“多”的一方),在多的一方加外键 8 private Integer id; 9 private String number; 10 private Float sum; 11 private Customer customer; //映射外键【看他的Order.hbm.xml配置文件对应的字段customer处】 12 13 public Integer getId() { 14 return id; 15 } 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 public String getNumber() { 20 return number; 21 } 22 public void setNumber(String number) { 23 this.number = number; 24 } 25 public Float getSum() { 26 return sum; 27 } 28 public void setSum(Float sum) { 29 this.sum = sum; 30 } 31 public Customer getCustomer() { 32 return customer; 33 } 34 public void setCustomer(Customer customer) { 35 this.customer = customer; 36 } 37 }
2.2、创建 Customer.hbm.xml 配置文件和 Order.hbm.xml 配置文件
1 xml version="1.0"?> 2 DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.shore.model"> 7 <class name="Customer" table="customer_xml"> 8 <id name="id"> 9 <generator class="native"/> 10 id> 11 <property name="name" type="java.lang.String"/> 12 <property name="age" type="java.lang.Integer"/> 13 class> 14 hibernate-mapping>
Order.hbm.xml 配置文件
1 xml version="1.0"?> 2 DOCTYPE hibernate-mapping PUBLIC 3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 5 6 <hibernate-mapping package="com.shore.model"> 7 <class name="Order" table="order_xml"> 8 <id name="id"> 9 <generator class="native"/> 10 id> 11 <property name="number" type="java.lang.String"/> 12 <property name="sum" type="java.lang.Float"/> 13 14 15 <many-to-one name="customer" column="customerId"/> 16 class> 17 hibernate-mapping>
2.3、创建hibernate.cfg.xml 核心配置文件
1 xml version='1.0' encoding='utf-8'?> 2 DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 9 <property name="connection.driver_class">com.mysql.jdbc.Driverproperty> 10 <property name="connection.url">jdbc:mysql://localhost:3306/hibernateproperty> 11 <property name="connection.username">rootproperty> 12 <property name="connection.password">rootproperty> 13 14 <property name="dialect">org.hibernate.dialect.MySQLDialectproperty> 15 <property name="cache.provider_class">org.hibernate.cache.NoCacheProviderproperty> 16 <property name="show_sql">trueproperty> 17 <property name="hbm2ddl.auto">createproperty> 18 19 21 <mapping resource="com/shore/model/Customer.hbm.xml" /> 22 <mapping resource="com/shore/model/Order.hbm.xml" /> 23 session-factory> 24 hibernate-configuration>
2.4、开始测试
1 package com.shore.test; 2 3 import org.hibernate.cfg.Configuration; 4 import org.hibernate.tool.hbm2ddl.SchemaExport; 5 import org.junit.Test; 6 7 /** 8 * @author DSHORE/2019-9-19 9 * 10 */ 11 public class XMLTest { 12 @Test 13 public void test() {//简单测试,只创建表,不插入数据 14 //xml版,此处用Configuration()方法 15 new SchemaExport(new Configuration().configure()).create( 16 false, true); 17 } 18 }
测试结果图:
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/11553213.html 欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |