gpt4 book ai didi

java - 一对一 hibernate 映射

转载 作者:太空宇宙 更新时间:2023-11-04 07:28:03 27 4
gpt4 key购买 nike

我已经在 hibernate 3 中使用注释完成了一对一映射,我有两张表“组”和“类别”。类别是预定义的。当用户选择类别和组时,CategoryId和goupid应该只插入组表中。

那么应该如何映射。

我的 bean 类如下:

类别:

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

最佳答案

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

首先,数据库中必须满足关系。一个foreign key应该添加。我假设关系的方向是从组到类别。向 Group 表添加外键。外键和 CategoryId 之间必须满足关系。

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

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

关于java - 一对一 hibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18224176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com