Os与OsFeature为一对多关系(OneToMany)
Os:
privateLongosId;
.
.
.
privateSet<OsFeature>osFeatures=newHashSet<OsFeature>();
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="OsId",unique=true,nullable=false,insertable=true,updatable=true)
publicLonggetOsId()
{
returnosId;
}
...
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="os")
publicSet<OsFeature>getOsFeatures()
{
returnosFeatures;
}
...
OsFeature:
privateOsos;
.
.
.
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="OsId",referencedColumnname="OsId",unique=false,nullable=false,insertable=true,updatable=true)
publicOsgetOs()
{
returnos;
}
...
注意:OsFeature里面的referencedColumnName,它表示多对一中一方的主键
如果不加该属性,在程序中,如果是保存一方Os的话,保存成功,但是在Os对应的表中会保存多次,即:os中关联多少个osFeature,os就会保存多少次,加上referencedColumnName属性后,一切正常