1、Annotation 注解版

1.1、在多的一方加外键

1.2、创建Customer类和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_customer")
public class Customer {//顾客 (“一”的一方)
private Integer id;
private String name;
private Integer age; @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;
}
}

Order类

 package com.shore.model;

 import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
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;
private Customer customer; //映射外键【看他的getCustomer()方法处】 @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;
} @ManyToOne //多对一
@JoinColumn(name="customerId") //外键
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}

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" />
</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() {//简单测试,只创建表,不插入数据
new SchemaExport(new AnnotationConfiguration().configure()).create(
false, true);
}
}

测试结果图:

    

2、XML版 的实现

2.1、创建Customer类和Order类

 package com.shore.model;

 /**
* @author DSHORE/2019-9-19
* 多对一,单向关联(xml版)
*/
public class Customer {//顾客 (“一”的一方)
private Integer id;
private String name;
private Integer age; 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;
}
}

Order类

 package com.shore.model;

 /**
* @author DSHORE/2019-9-19
* 多对一,单向关联(xml版)
*/
public class Order {//订单 (“多”的一方),在多的一方加外键
private Integer id;
private String number;
private Float sum;
private Customer customer; //映射外键【看他的Order.hbm.xml配置文件对应的字段customer处】 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;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}

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"/>
</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"/> <!-- 多对一 -->
<many-to-one name="customer" column="customerId"/>
</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/11553213.html

版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

Java进阶知识08 Hibernate多对一单向关联(Annotation+XML实现)的更多相关文章

  1. Java进阶知识11 Hibernate多对多单向关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.应用场景(Student-Teacher):当学生知道有哪些老师教,但是老师不知道自己教哪些学生时,可用单向关联 1.2.创建Teacher类和Student ...

  2. Java进阶知识12 Hibernate多对多双向关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.应用场景(Student-Teacher):当学生知道有哪些老师教,老师也知道自己教哪些学生时,可用双向关联 1.2.创建Teacher类和Student类 ...

  3. Java进阶知识09 Hibernate一对多单向关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.在一的一方加Set 1.2.创建Customer类和Order类 package com.shore.model; import java.util.Hash ...

  4. Java进阶知识05 Hibernate联合主键之Annotation(注解)和XML实现方式

    1.Hibernate联合主键(Annotation实现) 1.1.单列主键 1.1.1.为什么要有主键? //唯一确定一条记录    1.1.2.一个表能否有多个主键? //不能    1.1.3. ...

  5. Java进阶知识10 Hibernate一对多_多对一双向关联(Annotation+XML实现)

    本文知识点(目录): 1.Annotation 注解版(只是测试建表)    2.XML版 的实现(只是测试建表)    3.附录(Annotation 注解版CRUD操作)[注解版有个问题:插入值时 ...

  6. Java进阶知识06 Hibernate一对一单向外键关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...

  7. Java进阶知识07 Hibernate一对一双向外键关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.创建Husband类和Wife类 package com.shore.model; import javax.persistence.Entity; impo ...

  8. hibernate多对一单向关联

    关联是类(类的实例)之间的关系,表示有意义和值得关注的连接. 本系列将介绍Hibernate中主要的几种关联映射 Hibernate一对一主键单向关联Hibernate一对一主键双向关联Hiberna ...

  9. Hibernate多对多单向关联和双向关联 --Hibernate框架

    Hibernate关联关系中相对比较特殊的就是多对多关联,多对多关联与一对一关联和一对多关联不同,多对多关联需要另外一张映射表用于保存多对多映射信息.本例介绍多对多单向关联和双向关联.单向关联 :指具 ...

随机推荐

  1. 三分钟搞定Python中的装饰器

    python的装饰器是python的特色高级功能之一,言简意赅得说,其作用是在不改变其原有函数和类的定义的基础上,给他们增添新的功能. 装饰器存在的意义是什么呢?我们知道,在python中函数可以调用 ...

  2. shell习题第16题:查用户

    [题目要求] 写个shell,看看你的Linux系统中是否有自定义的用户(普通用户),如有有的话统计个数 [核心要点] CentOS6,uid>=500 CentOS7,uid>=1000 ...

  3. sentinel与hystrix对比

    近期有同事再提要不要使用sentinel.所以我就对现在已经用hystrix.先看两者的线程模型.大部分对比项是sentinel开源工程对比的,本人做了一些修改以及增加了一些对比项和说明. 从线程模型 ...

  4. 批量Insert

    oracle INSERT ALL ,) ,) ,) FROM DUAL

  5. JSP读取properties文件变量

    1.jsp代码 <%ResourceBundle res = ResourceBundle.getBundle("properties文件名"); %> 2.js代码 ...

  6. OneinStack – 一键PHP/JAVA安装工具

    https://oneinstack.com/ OneinStack包含以下组合:lnmp(Linux + Nginx+ MySQL+ PHP) lamp(Linux + Apache+ MySQL+ ...

  7. scala的泛型浅析

    1. scala泛型浅析 package com.dtspark.scala.basics /** * 1,scala的类和方法.函数都可以是泛型. * * 2,关于对类型边界的限定分为上边界和下边界 ...

  8. NLog Helpper日志帮助类配置和使用

    1.帮助类  (首先需要引入NLog.dll) using System; namespace XXXXXX { /// <summary> /// 用法实例 : NLogTest.Nlo ...

  9. 学习前端第二天之css层叠样式

    一.设置样式公式 选择器 {属性:值:} 二.font 设置四大操作 font-size:字体大小 (以像素为单位) font-weight:字体粗细 font-family:字体    ( 可直接跟 ...

  10. css3控制字体动态变换颜色

    css3控制字体动态变换颜色 <!doctype html> <html> <head> <meta charset="utf-8"> ...