hibernate 注解方式,实现一对多映射

省和市的  对应管理 为一对多:

 1.对应pojo (Province.java和City.java):

(1)Province.java

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/** 省
 * 
 * @pdOid 009f5c9b-914c-44ff-b82a-94c41b9d1abb */
@Entity(name="Province")
@Table(name="province")
public class Province implements java.io.Serializable {
   /** 主键
    * 
    * @pdOid 4cdf9132-ed58-4ddc-a02e-7bb33aceb88e */
   public java.lang.Integer id;
   /** 省名称
    * 
    * @pdOid 1d107605-655a-4eb6-b7d9-edd50a9e6908 */
   public java.lang.String name;
   /** 编号
    * 
    * @pdOid 5131c56e-fb66-452a-ab43-aa5321038f40 */
   public java.lang.Integer code;
   private  Set<City> citys = new HashSet<City>();
   @OneToMany(targetEntity =City.class,mappedBy="fatherID", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   public Set<City> getCitys() {
	return citys;
	}
	
	public void setCitys(Set<City> citys) {
		this.citys = citys;
	}

public Province() {
      // TODO Add your own initialization code here.
   }
   
   /**
    * Get value of id
    *
    * @return id 
    */
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="id", nullable=false, insertable=true, updatable=true, length=11)
   public java.lang.Integer getId()
   {
      return id;
   }
   
   /**
    * Set value of id
    *
    * @param newId 
    */
   public void setId(java.lang.Integer newId)
   {
      this.id = newId;
   }
   
   /**
    * Get value of name
    *
    * @return name 
    */
   @Basic(optional=true)
   @Column(name="province", insertable=true, updatable=true, length=20)
   public java.lang.String getName()
   {
      return name;
   }
   
   /**
    * Set value of name
    *
    * @param newName 
    */
   public void setName(java.lang.String newName)
   {
      this.name = newName;
   }
   
   /**
    * Get value of code
    *
    * @return code 
    */
   @Basic(optional=true)
   @Column(name="provinceID", insertable=true, updatable=true, length=11)
   public java.lang.Integer getCode()
   {
      return code;
   }
   
   @Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((code == null) ? 0 : code.hashCode());
	result = prime * result + ((id == null) ? 0 : id.hashCode());
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	return result;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Province other = (Province) obj;
	if (code == null) {
		if (other.code != null)
			return false;
	} else if (!code.equals(other.code))
		return false;
	if (id == null) {
		if (other.id != null)
			return false;
	} else if (!id.equals(other.id))
		return false;
	if (name == null) {
		if (other.name != null)
			return false;
	} else if (!name.equals(other.name))
		return false;
	return true;
}

/**
    * Set value of code
    *
    * @param newCode 
    */
   public void setCode(java.lang.Integer newCode)
   {
      this.code = newCode;
   }



}


(2)City.java

 
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/** 省
 * 
 * @pdOid 009f5c9b-914c-44ff-b82a-94c41b9d1abb */
@Entity(name="City")
@Table(name="city")
public class City implements java.io.Serializable {
   /** 主键
    * 
    * @pdOid 4cdf9132-ed58-4ddc-a02e-7bb33aceb88e */
   public java.lang.Integer id;
   /** 省名称
    * 
    * @pdOid 1d107605-655a-4eb6-b7d9-edd50a9e6908 */
   public java.lang.String name;
   /** 编号
    * 
    * @pdOid 5131c56e-fb66-452a-ab43-aa5321038f40 */
   public java.lang.Integer code;
   public Province fatherID;
   private  Set<Area> areas = new HashSet<Area>();
   @OneToMany(targetEntity =Area.class,mappedBy="fatherID", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   public Set<Area> getAreas() {
	return areas;
	}
	
	public void setAreas(Set<Area> areas) {
		this.areas = areas;
	}

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((code == null) ? 0 : code.hashCode());
	result = prime * result + ((fatherID == null) ? 0 : fatherID.hashCode());
	result = prime * result + ((id == null) ? 0 : id.hashCode());
	result = prime * result + ((name == null) ? 0 : name.hashCode());
	return result;
}

@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	City other = (City) obj;
	if (code == null) {
		if (other.code != null)
			return false;
	} else if (!code.equals(other.code))
		return false;
	if (fatherID == null) {
		if (other.fatherID != null)
			return false;
	} else if (!fatherID.equals(other.fatherID))
		return false;
	if (id == null) {
		if (other.id != null)
			return false;
	} else if (!id.equals(other.id))
		return false;
	if (name == null) {
		if (other.name != null)
			return false;
	} else if (!name.equals(other.name))
		return false;
	return true;
}

   @ManyToOne(cascade = CascadeType.ALL, optional = false)
   @JoinColumn(name="fatherID", referencedColumnName="provinceID")//外键为sut_id,与student中的id关联
   public Province getFatherID() {
	   return fatherID;
	}
	
	public void setFatherID(Province fatherID) {
		this.fatherID = fatherID;
	}

	public City() {
	      // TODO Add your own initialization code here.
    }
   
   /**
    * Get value of id
    *
    * @return id 
    */
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="id", nullable=false, insertable=true, updatable=true, length=11)
   public java.lang.Integer getId()
   {
      return id;
   }
   
   /**
    * Set value of id
    *
    * @param newId 
    */
   public void setId(java.lang.Integer newId)
   {
      this.id = newId;
   }
   
   /**
    * Get value of name
    *
    * @return name 
    */
   @Basic(optional=true)
   @Column(name="city", insertable=true, updatable=true, length=20)
   public java.lang.String getName()
   {
      return name;
   }
   
   /**
    * Set value of name
    *
    * @param newName 
    */
   public void setName(java.lang.String newName)
   {
      this.name = newName;
   }
   
   /**
    * Get value of code
    *
    * @return code 
    */
   @Basic(optional=true)
   @Column(name="cityID", insertable=true, updatable=true, length=11)
   public java.lang.Integer getCode()
   {
      return code;
   }
   
   /**
    * Set value of code
    *
    * @param newCode 
    */
   public void setCode(java.lang.Integer newCode)
   {
      this.code = newCode;
   }



}


注意:

(1).@OneToMany(targetEntity =City.class,mappedBy="fatherID", cascade = CascadeType.ALL, fetch = FetchType.LAZY) 这里mappedBy对应值填写City类中定义的Province属性fatherID( public Province fatherID;)

(2).  @JoinColumn(name="fatherID", referencedColumnName="provinceID")name填写city中的外键字段(实际表中可以没有外键约束),referencedColumnName填写,city表外键关联的province中具体字段,而不是Province类中的字段

下面是标的结构

(1)province表



(2)city表



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate是一个Java持久化框架,它能够将Java对象映射到数据库的表格,同时支持各种关系数据库,如MySQL、Oracle等。在Hibernate,对于一对一、一对多和多对多的关系,我们可以通过以下方式进行映射。 一对一关系:在Hibernate,可以通过主键关联和外键关联来实现一对一关系的映射。主键关联是指两个实体之间的关联通过主键来进行,可以使用@PrimaryKeyJoinColumn注解将两个实体关联起来。外键关联是指通过一个实体引用另一个实体的主键作为外键,使用@JoinColumn注解来指定外键属性。 一对多关系:在Hibernate一对多关系通常通过外键关联来实现。在一的一方,使用@OneToMany注解来定义一对多关系,同时使用@JoinColumn注解指定外键属性。在多的一方,使用@ManyToOne注解来定义多对一关系,并使用@JoinColumn注解指定外键属性。 多对多关系:在Hibernate,多对多关系通常通过间表来实现。在多对多的两个实体,使用@ManyToMany注解来定义多对多关系。同时,需要在间表创建两个外键,分别与两个实体的主键关联,并使用@JoinTable注解来指定间表的表名和两个外键的列名。 总结:通过Hibernate注解方式,可以方便地实现一对一、一对多和多对多关系的映射。通过合理地使用注解,可以减少编写映射文件的工作量,提高开发效率。同时,Hibernate还提供了在运行时自动生成表结构的功能,可以根据Java实体类来动态创建或更新对应的数据库表格,从而提高系统的可维护性和灵活性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值