简体   繁体   English

一对一休眠映射

[英]one to one hibernate mapping

i have done one to one mapping in hibernate 3 with annotation, i have tow tables 'group' and 'category' . 我在冬眠3中使用注释完成了一对一的映射,我拖了两个表'group'和'category'。 categories are predefined. 类别是预定义的。 when user choose category and group ,CategoryId and goupid should insert in group table only. 当用户选择类别和组时,CategoryId和goupid仅应插入组表中。

So how should to mapping. 那么应该如何映射。

my bean classes are following: 我的bean类如下:

Categories: 分类:

@Entity
@Table(name = "biz_categories")
public class Categories implements Serializable {

private static final long serialVersionUID = -8422954389102945506L;

@Id
@Column(name = "CategoryId")
private Integer categoryId;

@Column(name = "CategoryName")
private String categoryName;

@Column(name = "CategoryDescription")
private String categoryDescription;

@Column(name = "CreatedBy")
private String createdBy;

@Column(name = "UpdatedBy")
private String updatedBy;

@Column(name = "CreatedDate")
private Date createdDate;

@Column(name = "UpadtedDate")
private Date upadatedDate;

@Column(name = "ActiveFlag")
private int activeFlag;


public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

public String getCategoryName() {
    return categoryName;
}

public void setCategoryDescription(String categoryDescription) {
    this.categoryDescription = categoryDescription;
}

public String getCategoryDescription() {
    return categoryDescription;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}

public String getCreatedBy() {
    return createdBy;
}

public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

public String getUpdatedBy() {
    return updatedBy;
}

public void setUpadatedDate(Date upadatedDate) {
    this.upadatedDate = upadatedDate;
}

public Date getUpadatedDate() {
    return upadatedDate;
}

public void setActiveFlag(int activeFlag) {
    this.activeFlag = activeFlag;
}

public int getActiveFlag() {
    return activeFlag;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

public Date getCreatedDate() {
    return createdDate;
}

public void setCategoryId(Integer categoryId) {
    this.categoryId = categoryId;
}

public Integer getCategoryId() {
    return categoryId;
}
}

Group: 组:

@Entity
@Table(name = "biz_facilitygroups")
public class Groups implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@Column(name = "GroupId")
@GeneratedValue
private Integer groupId;

@Column(name = "CategoryId")
private int categoryId;

@Column(name = "GroupName")
private String groupName;

@Column(name = "Description")
private String description;

@Column(name = "CreatedBy")
private String createdBy;

@Column(name = "UpdatedBy")
private String updatedBy;

@Column(name = "CreatedDate")
private Date createdDate;

@Column(name = "UpadtedDate")
private Date upadatedDate;

@Column(name = "ActiveFlag")
private int activeFlag;

@OneToOne(targetEntity=Categories.class,cascade=CascadeType.ALL)
@JoinColumn(name="CategoryId")
private Categories categories;

public Integer getGroupId() {
    return groupId;
}

public void setGroupId(Integer groupId) {
    this.groupId = groupId;
}

public String getGroupName() {
    return groupName;
}

public void setGroupName(String groupName) {
    this.groupName = groupName;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getCreatedBy() {
    return createdBy;
}

public void setCreatedBy(String createdBy) {
    this.createdBy = createdBy;
}

public String getUpdatedBy() {
    return updatedBy;
}

public void setUpdatedBy(String updatedBy) {
    this.updatedBy = updatedBy;
}

public Date getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

public Date getUpadatedDate() {
    return upadatedDate;
}

public void setUpadatedDate(Date upadatedDate) {
    this.upadatedDate = upadatedDate;
}

public int getActiveFlag() {
    return activeFlag;
}

public void setActiveFlag(int activeFlag) {
    this.activeFlag = activeFlag;
}

public static long getSerialversionuid() {
    return serialVersionUID;
}

public void setCategoryId(int categoryId) {
    this.categoryId = categoryId;
}

public int getCategoryId() {
    return categoryId;
}

public void setCategories(Categories categories) {
    this.categories = categories;
}

public Categories getCategories() {
    return categories;
}
}

Just another property to class relation is needed. 仅需要另一个属性与类的关系。

First of all, relation must be satisfied in the database. 首先,必须在数据库中满足关系。 A foreign key should be added. 应该添加一个外键 I assume that direction of relation is from Group to Category . 我认为关系的方向是从Group到Category Add a foreign key to Group table. 将外键添加到Group表。 Relation must be satisfied between foreign key and CategoryId. 外键和CategoryId之间必须满足关系。

Add a property for the relation to Group entity. 为与Group实体的关系添加属性。

@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "foreing_key_of_category")
private Category category;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM