1

我已经在 hibernate 3 中使用注释进行了一对一的映射,我有两个表 'group' 和 'category' 。类别是预定义的。当用户选择类别和组时,CategoryId 和 goupid 应该只插入组表中。

那么应该如何映射。

我的豆类如下:

分类:

@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;
}
}

团体:

@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;
}
}
4

1 回答 1

1

只需要另一个属性与类关系。

首先,必须在数据库中满足关系。应添加外键。我假设关系的方向是从 Group 到CategoryGroup向表中添加外键。外键和 CategoryId 之间必须满足关系。

Group为与实体的关系添加属性。

@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "foreing_key_of_category")
private Category category;
于 2013-08-14T06:06:39.470 回答