比如department和employee是一对多关系的,我在department那边设置了mappedby,最后测试的结果只是不会生成第三张多余的表,其他没感觉什么,网上都说让多的这一段作为关系维护端,可是设置了mappedby后我还是可以在一的一端维护关系啊,那hibernate具体是怎么处理的呢,能不能从代码的角度再具体一点呢,谢谢
4条回答 默认 最新
- iteye_13460 2010-04-11 10:33关注
实体配置如下:
[code="java"]package com;
import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;/**
- Department entity.
-
@author MyEclipse Persistence Tools
*/
@Entity
@Table
public class Department implements java.io.Serializable {// Fields
@Id
private Long depid;
private String depname;
@OneToMany(mappedBy="department")
private Set employees = new HashSet();
// Constructors/** default constructor */
public Department() {
}/** minimal constructor */
public Department(Long depid) {
this.depid = depid;
}/** full constructor */
public Department(Long depid, String depname) {
this.depid = depid;
this.depname = depname;
}// Property accessors
public Long getDepid() {
return this.depid;
}public void setDepid(Long depid) {
this.depid = depid;
}public String getDepname() {
return this.depname;
}public void setDepname(String depname) {
this.depname = depname;
}public Set getEmployees() {
return employees;
}public void setEmployees(Set employees) {
this.employees = employees;
}
}[/code]
[code="java"]package com;import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;/**
- Employee entity.
- @author MyEclipse Persistence Tools */ @Entity @Table
public class Employee implements java.io.Serializable {
// Fields @Id private Long eeid; private String eename; @Column(name="dep_id",insertable=false,updatable=false) private Long depId; @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="dep_id") private Department department; // Constructors /** default constructor */ public Employee() { } /** minimal constructor */ public Employee(Long eeid) { this.eeid = eeid; } /** full constructor */ public Employee(Long eeid, String eename, Long depId) { this.eeid = eeid; this.eename = eename; this.depId = depId; } // Property accessors public Long getEeid() { return this.eeid; } public void setEeid(Long eeid) { this.eeid = eeid; } public String getEename() { return this.eename; } public void setEename(String eename) { this.eename = eename; } public Long getDepId() { return this.depId; } public void setDepId(Long depId) { this.depId = depId; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; }
}[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
- ¥15 C#调用python代码(python带有库)
- ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
- ¥15 活动选择题。最多可以参加几个项目?
- ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
- ¥15 vs2019中数据导出问题
- ¥20 云服务Linux系统TCP-MSS值修改?
- ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
- ¥20 怎么在stm32门禁成品上增加查询记录功能
- ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面