Hibernate5 映射,单向关联问题
程序员文章站
2024-03-04 08:18:41
...
上篇博客介绍了Hibernate相关配置问题,这片博客介绍Hibernate关联映射问题
一:单向映射,关联关系如图,可以看出tab_product表建立的外键factoryid,关联表tab_factory中的主键factoryid
用我上篇博客中用的tb_books表,添加字段factoryid,插入数据,关于mysql添加字段以及插入数据就不多说了,最后结果如图
创建tb_factory表,插入数据,结果如图:
二:建立Factory类:
package com.book.web3;
public class Factory {
private int factoryid;
private String name;
public int getFactoryid(){
return factoryid;
}
public void setFactoryid(int factoryid){
this.factoryid = factoryid;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
在Book类中添加factory字段
package com.book.web3;
public class Book {
public static int PAGE_SIZE = 2;
private int id;
private String name;
private double price;
private int bookCount;
private String author;
private Factory factory;
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public double getPrice(){
return price;
}
public void setPrice(double price){
this.price = price;
}
public int getBookCount(){
return bookCount;
}
public void setBookCount(int bookCount){
this.bookCount = bookCount;
}
public String getAuthor(){
return author;
}
public void setAuthor(String author){
this.author = author;
}
public Factory getFactory(){
return factory;
}
public void setFactory(Factory factory){
this.factory = factory;
}
}
三:在tb_books.hbm.xml中添加映射关系:
<?xml version="1.0" encoding="UTF-8"?>
<!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.book.web3">
<class name="Book" table="tb_books">
<id name="id" column="id" type="int" >
<generator class="assigned" /> <!--generator这个坑,如果指定class为native会导致外部程序设置的主键id无效,且根据数据库自行判断采用自增长式 -->
</id>
<property name="name" type="string">
<column name="name"/>
</property>
<property name="price" type="double" column="price"></property>
<property name="bookCount" type="int" column="bookCount"></property>
<property name="author" type="string" column="author"></property>
<many-to-one name="factory" class="com.book.web3.Factory">
<column name="factoryid"></column>
</many-to-one>
</class>
</hibernate-mapping>
创建tb_factory.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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.book.web3">
<class name="Factory" table="tb_factory">
<id name="factoryid" column="factoryid" type="int" >
<generator class="assigned" /> <!--generator这个坑,如果指定class为native会导致外部程序设置的主键id无效,且根据数据库自行判断采用自增长式 -->
</id>
<property name="name" type="string">
<column name="name"/>
</property>
</class>
</hibernate-mapping>
最后在hibernate.cfg.xml中配置tb_factory.hbm.xml这个文件
测试类中添加测试代码
public static void main(String[] args) {
// addData();
// queryData();
// deleteData();
// modifyData();
// twoCahce();
Relation();
}
private static void Relation(){
Session session = null;
try {
session = HibernateUtil.getSession();
session.beginTransaction();
Book book = session.get(Book.class, new Integer("99"));
System.out.print("产品ID:" + book.getId());
System.out.print("产品名称:" + book.getName());
System.out.print("产品价格:" + book.getPrice());
System.out.print("产品数量:" + book.getBookCount());
System.out.print("作者:" + book.getAuthor());
System.out.print("factory:" + book.getFactory().getName());
session.getTransaction().commit();
} catch (Exception e) {
System.out.print("关联失败");
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
}
结果如图所示
上一篇: [二叉树]面试题 04.08. 首个共同祖先(medium)
下一篇: 类与继承
推荐阅读