1、Annotation 注解版
1.1、在一的一方加Set
1.2、创建Customer类和Order类
package com.shore.model; import java.util.HashSet;
import java.util.Set; import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table; /**
* @author DSHORE/2019-9-19
* 一对多,单向关联(注解版)
*/
@Entity
@Table(name="anno_customer")
public class Customer {//顾客 (“一”的一方),在“一”的一方加Set
private Integer id;
private String name;
private Integer age;
private Set<Order> orders = new HashSet<Order>(); //在“一”的一方加Set
/**
* List 有序,可重复,可以用index取值(get(index))
* Set 无序,不可重复
*/ @Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
} @OneToMany //一对多
@JoinColumn(name="customerId")
public Set<Order> getOrders() {//在“一”的一方加Set
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
Order类
package com.shore.model; import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; /**
* @author DSHORE/2019-9-19
* 一对多,单向关联(注解版)
*/
@Entity
@Table(name="anno_order") //Order是MySQL数据库关键字。需重新定义表名
public class Order {//订单 (“多”的一方)
private Integer id;
private String number;
private Float sum; @Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Float getSum() {
return sum;
}
public void setSum(Float sum) {
this.sum = sum;
}
}
1.3、创建hibernate.cfg.xml核心配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property> <mapping class="com.shore.model.Customer" />
<mapping class="com.shore.model.Order" />
<!-- <mapping resource="com/shore/model/Customer.hbm.xml" />
<mapping resource="com/shore/model/Order.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
1.4、开始测试
package com.shore.test; import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test; /**
* @author DSHORE/2019-9-19
*
*/
public class AnnotationTest {
@Test
public void test() {//简单测试,只创建表,不插入数据
//注解版,用AnnotationConfiguration()方法
new SchemaExport(new AnnotationConfiguration().configure()).create(
false, true);
}
}
测试结果图:
2、XML版 的实现
2.1、创建Customer类和Order类
package com.shore.model; import java.util.HashSet;
import java.util.Set; /**
* @author DSHORE/2019-9-19
* 一对多,单向关联(xml版)
*/
public class Customer {//顾客 (“一”的一方),在一的一方加Set
private Integer id;
private String name;
private Integer age;
private Set<Order> orders = new HashSet<Order>(); //在“一”的一方加Set public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
}
Order类
package com.shore.model; /**
* @author DSHORE/2019-9-19
* 一对多,单向关联(xml版)
*/
public class Order {//订单 (“多”的一方)
private Integer id;
private String number;
private Float sum; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Float getSum() {
return sum;
}
public void setSum(Float sum) {
this.sum = sum;
}
}
2.2、创建 Customer.hbm.xml 配置文件和 Order.hbm.xml 配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.shore.model">
<class name="Customer" table="customer_xml">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="java.lang.String"/>
<property name="age" type="java.lang.Integer"/> <!-- 一对多,在“一”的一方的配置文件中用set标签 -->
<set name="orders">
<key column="customerId"></key>
<one-to-many class="com.shore.model.Order"/>
</set>
</class>
</hibernate-mapping>
Order.hbm.xml 配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.shore.model">
<class name="Order" table="order_xml">
<id name="id">
<generator class="native"/>
</id>
<property name="number" type="java.lang.String"/>
<property name="sum" type="java.lang.Float"/>
</class>
</hibernate-mapping>
2.3、创建hibernate.cfg.xml 核心配置文件
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property> <!-- <mapping class="com.shore.model.Customer" />
<mapping class="com.shore.model.Order" /> -->
<mapping resource="com/shore/model/Customer.hbm.xml" />
<mapping resource="com/shore/model/Order.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.4、开始测试
package com.shore.test; import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test; /**
* @author DSHORE/2019-9-19
*
*/
public class XMLTest {
@Test
public void test() {//简单测试,只创建表,不插入数据
//xml版,此处用Configuration()方法
new SchemaExport(new Configuration().configure()).create(
false, true);
}
}
测试结果图:
Hibernate一对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545058.html
Hibernate一对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11545077.html
Hibernate多对一单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553213.html
Hibernate一对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11553215.html
Hibernate一对多和多对一双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11560433.html
Hibernate多对多单向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568536.html
Hibernate多对多双向关联映射(Annotation+XML实现):https://www.cnblogs.com/dshore123/p/11568963.html
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/11553215.html 版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |