`
默翁1
  • 浏览: 25218 次
社区版块
存档分类
最新评论

配置多对1双向关联

 
阅读更多

同样以商品和厂商为例,商品对厂商为多对1,则反过来,厂商对商品为1对多关系

public class Product {
	private Integer id;
	private String name;
	private double price;
	private Factory factory;
	public Product(){}
	public Product(Integer id){
		this.id=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 double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public Factory getFactory() {
		return factory;
	}
	public void setFactory(Factory factory) {
		this.factory = factory;
	}
	
}

 

public class TestApplication7 {
	public static void main(String[] args) {
		Configuration configuration=new Configuration().configure();
		SessionFactory sessionFactory=configuration.buildSessionFactory();
		Session session=sessionFactory.openSession();
		Transaction transaction=session.beginTransaction();
		Integer(3));
		System.out.println(product.getName()+"===="+product.getFactory().getFactoryname());
		transaction.commit();*/	
		Factory factory=(Factory)session.get(Factory.class, new Integer(1));
		Set<Product>products =factory.getProducts();
		Iterator<Product> iterator=products.iterator();
		while(iterator.hasNext()){
			Product product=iterator.next();
			System.out.println(product.getName());
		}
		session.close();
	}

 

<hibernate-mapping>
	<!-- 每一个class类对应一个持久化对象 -->
	<class name="model.Factory" table="mw_factory">
		<id name="id">
		<!-- 主键的生成策略 -->
			<generator class="identity"></generator>
		</id>
		<property name="Factoryname" type="string" length="45">
			<column name="factoryName"></column>
		</property>
		<set name="products" inverse="true">
			<key column="factoryid"></key>
			<one-to-many class="model.Product"/>
		</set>
	</class>
</hibernate-mapping>

 

public class Factory {
	private int id;
	private String Factoryname;
	private Set<Product> products; 
	public Factory(){}
	
	public Factory(Integer id){
		this.id=this.id;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFactoryname() {
		return Factoryname;
	}
	public void setFactoryname(String factoryname) {
		Factoryname = factoryname;
	}

	public Set<Product> getProducts() {
		return products;
	}

	public void setProducts(Set<Product> products) {
		this.products = products;
	}
	
	
	
}

 

<hibernate-mapping>
	<!-- 每一个class类对应一个持久化对象 -->
	<class name="model.Product" table="mw_product">
		<id name="id">
		<!-- 主键的生成策略 -->
			<generator class="identity"></generator>
		</id>
		<property name="name" type="string" length="45">
			<column name="name"></column>
		</property>
		<property name="price" type="double">
			<column name="price"></column>
		</property>
		<many-to-one name="factory" class="model.Factory" cascade="all">
			<column name="factoryid"></column>
		</many-to-one>
	</class>
</hibernate-mapping>

 

分享到:
评论

相关推荐

    Hibernate ORM - 一对多双向关联关系

    NULL 博文链接:https://dreamzhong.iteye.com/blog/1200915

    Hibernate双向一对一关联映射(注解版)

    Hibernate双向一对一关联映射(注解版)

    10_JPA详解_JPA中的一对多双向关联与级联操作.zip

    10_JPA详解_JPA中的一对多双向关联与级联操作.zip

    hibernate one-to-many 单/双向关联映射配置心得

    NULL 博文链接:https://yinxiaoyong.iteye.com/blog/426752

    多对多关联关系的使用

    学习多对多关联关系的使用 SQL基础知识 Hibernate基础知识 1.将数据库eshopdb中的数据清空,添加初始化数据 2.运行MyEclipse,新建一个“Java Project”,加入mysql-connector-java-3.1.12-...4、配置多对多双向关系

    eshop1.0(ssh电子商城)

    8.配置会员到订单的一对多双向关联关系 9.配置会员到留言的一对多单向关联关系 10.配置管理员到留言的一对多单向关联关系 11.配置会员到购物车的一对多单向关联关系 12.配置订单到购物车的一对一单向关联关系 13....

    Hibernate_Annotation关联映射

    多对一的配置方式有两种:(1)通过@JoinColoumn映像(2)通过关联表的方式来映像 (1) 通过@JoinColoumn映射 SRD Framework中Company,Category例子: Company: @ManyToOne @JoinColumn(name = "CATEGORY_OPTION_ID")...

    Java的Hibernate框架中的双向主键关联与双向外键关联

    Hibernate想要实现双向的关联就必须在映射文件的两端同时配置,另外还要在主映射的一端采用foreign外键关联属性,下面我们就一起来看一下Java的Hibernate框架中的双向主键关联与双向外键关联方法:

    华为 NE20E-S V800R010C10SPC500 配置指南 - MPLS OAM配置

    介绍了MPLS OAM(Multiprotocol Label Switching Operation Administration &Maintenance)的...3 配置双向关联LSP的MPLS OAM检测功能 4 配置双向共路LSP的MPLS OAM检测功能 5 配置PW的MPLS OAM检测功能 6 维护MPLS OAM

    hibernate配置文件

    该文档总结了hibernate各种关联模式,单向多对一,单向一对多,双向一对多,双向多对多,自身一对多关联,以及oracle,mysql的相关数据库连接配置,希望对您学习有帮助。

    NHibernate中文文档

    值集合和多对多关联(Collections of Values and Many-To-Many Associations) 14 一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 ...

    hibernate 体系结构与配置 参考文档(html)

    使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的...

    EJB葵花宝典(题集)

    5.8.3多对多双向关联 68 1.1.1.7 表1 68 1.1.1.8 表2 70 1.1.1.9 插入数据 72 1.1.1.10 查找数据 73 1.1.1.11 删除数据 74 1.1.1.12 增删关联表中的数据 75 5.9Persistence继承策略 76 5.9.1每个子类一张表 76 5.9.2...

    hibernate+中文api

    1.3.5. 双向关联 1.3.6. 使双向连起来 1.4. 第三部分 - EventManager web应用程序 1.4.1. 编写基本的servlet 1.4.2. 处理与渲染 1.4.3. 部署与测试 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview...

    Hibernate3的帮助文档

    8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many to one) 8.5.2. 一对一(one to one) 8.5.3. 多对多(many to many) 9. 组件...

    hibernate教程资源

    双向一对多: 前面说到了单向一对多,单向多对一。希望大家都有了一定的了解。现在我们说下双向一对多。 什么是双向一对多:同时配置了单向一对多,和单向多对一,就成了双向一对多关联。

    Hibernate+中文文档

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    HibernateAPI中文版.chm

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

    hibernate3.2中文文档(chm格式)

    7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂...

Global site tag (gtag.js) - Google Analytics