Hibernate多对一关系代码示例(Many-To-One)
2018-03-26 20:16
645 查看
1、创建测试表两个//没多少字段,就不写了,后面一对多和多对多都用的上
// category类型表,根据实体类的属性创建字段
// product产品表,根据实体类的属性创建字段
2、创建两个表的实体类package com.entity;
import java.util.Set;
/**
* 类型表实体
* Created by lvjun on 2018-03-26.
*/
public class CategoryEntity {
int id;
String name;
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;
}
}
1、CategoryEntity.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.entity">
<class name="CategoryEntity" table="category">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" />
</class>
</hibernate-mapping>2、ProductEntity.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.entity">
<class name="ProductEntity" table="product">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" />
<property name="price" />
<!--
设置类型表多对一关系:
name =对应ProductEntity实体类中CategoryEntity的属性
class= 对应CategoryEntity实体类
column=指向 category表的外键 ,在product表中会新增cid字段
-->
<many-to-one name="categoryEntity" class="CategoryEntity" column="cid" />
</class>
</hibernate-mapping>4、创建hibernate.cfg.xml核心配置文件src路径下,别进包里去了<?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/school?characterEncoding=GBK</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!--实体映射-->
<mapping resource="com/entity/Category.hbm.xml"/>
<mapping resource="com/entity/ProductEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>5、创建测试类package com.test;
import com.entity.CategoryEntity;
import com.entity.ProductEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 多对一关系
* 多个产品对应一个类型
* Created by lvjun on 2018-03-26.
*/
public class ManyToOne {
public static void main(String[] args) {
SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
Session session =sessionFactory.openSession();
//开启事物
session.beginTransaction();
//添加一个安卓的类型
CategoryEntity categoryEntity =new CategoryEntity();
categoryEntity.setName("安卓");
session.save(categoryEntity);
//产品表中id为8的产品,将其类型改成类型表中的id,在配置文件里面有一个新增cid字段
ProductEntity productEntity= (ProductEntity) session.get(ProductEntity.class,8);
productEntity.setCategoryEntity(categoryEntity);
session.update(productEntity);
//提交并关闭
session.beginTransaction().commit();
session.close();
sessionFactory.close();
}
}
// category类型表,根据实体类的属性创建字段
// product产品表,根据实体类的属性创建字段
2、创建两个表的实体类package com.entity;
import java.util.Set;
/**
* 类型表实体
* Created by lvjun on 2018-03-26.
*/
public class CategoryEntity {
int id;
String name;
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;
}
}
package com.entity; import java.util.Set; /** * 产品表实体 * Created by lvjun on 2018-03-26. */ public class ProductEntity { int id; String name ; float price; // 添加类型CategoryEntity属性 CategoryEntity categoryEntity; public CategoryEntity getCategoryEntity() { return categoryEntity; } public void setCategoryEntity(CategoryEntity categoryEntity) { this.categoryEntity = categoryEntity; } //产品表构建参数 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 float getPrice() { return price; } public void setPrice(float price) { this.price = price; } }3、创建映射配置文件,包含数据库配置
1、CategoryEntity.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.entity">
<class name="CategoryEntity" table="category">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" />
</class>
</hibernate-mapping>2、ProductEntity.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.entity">
<class name="ProductEntity" table="product">
<id name="id" column="id">
<generator class="native"></generator>
</id>
<property name="name" />
<property name="price" />
<!--
设置类型表多对一关系:
name =对应ProductEntity实体类中CategoryEntity的属性
class= 对应CategoryEntity实体类
column=指向 category表的外键 ,在product表中会新增cid字段
-->
<many-to-one name="categoryEntity" class="CategoryEntity" column="cid" />
</class>
</hibernate-mapping>4、创建hibernate.cfg.xml核心配置文件src路径下,别进包里去了<?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/school?characterEncoding=GBK</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!--实体映射-->
<mapping resource="com/entity/Category.hbm.xml"/>
<mapping resource="com/entity/ProductEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>5、创建测试类package com.test;
import com.entity.CategoryEntity;
import com.entity.ProductEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* 多对一关系
* 多个产品对应一个类型
* Created by lvjun on 2018-03-26.
*/
public class ManyToOne {
public static void main(String[] args) {
SessionFactory sessionFactory =new Configuration().configure().buildSessionFactory();
Session session =sessionFactory.openSession();
//开启事物
session.beginTransaction();
//添加一个安卓的类型
CategoryEntity categoryEntity =new CategoryEntity();
categoryEntity.setName("安卓");
session.save(categoryEntity);
//产品表中id为8的产品,将其类型改成类型表中的id,在配置文件里面有一个新增cid字段
ProductEntity productEntity= (ProductEntity) session.get(ProductEntity.class,8);
productEntity.setCategoryEntity(categoryEntity);
session.update(productEntity);
//提交并关闭
session.beginTransaction().commit();
session.close();
sessionFactory.close();
}
}
相关文章推荐
- Hibernate 、多表关联映射-多对一关系(many-to-one)
- Hibernate关系映射(四)一对多单向关联@OneToMany Annotation方式
- Hibernate一对多关系代码示例(One-To-Many)
- Hibernate 、多表关联映射-多对一关系(many-to-one)
- Hibernate 、多表关联映射-多对一关系(many-to-one)
- Hibernate中many-to-one关系的编写
- Hibernate 之many-to-one 单向多对一关联示例
- Hibernate 、多表关联映射-多对一关系(many-to-one)
- Hibernate中many-to-one关系的编写
- Hibernate多对多关系代码示例(Many-To-Many)
- Hibernate 表关系描述之OneToMany
- Hibernate使用property-ref属性解决遗留数据库One To Many关系的问题。
- Hibernate中many-to-one关系的编写
- Hibernate 表关系描述之OneToMany
- Hibernate映射关系解析(三)--Unidirectional associations--one-to-many
- Hibernate实体关系映射(OneToMany单边)——完整实例
- Hibernate使用property-ref属性解决遗留数据库One To Many关系的问题。
- Hibernate 表关系描述之OneToMany
- Rhythmk 学习 Hibernate 05 - Hibernate 表间关系 [ManyToOne,OneToMany]
- Hibernate实体关系映射(OneToMany、ManyToOne双边)——完整实例