Reputation: 199
i have done one to one mapping in hibernate 3 with annotation, i have tow tables 'group' and 'category' . categories are predefined. when user choose category and group ,CategoryId and goupid should insert in group table only.
So how should to mapping.
my bean classes are following:
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;
}
}
Upvotes: 1
Views: 125
Reputation: 3763
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
. Add a foreign key to Group
table. Relation must be satisfied between foreign key and CategoryId.
Add a property for the relation to Group
entity.
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinColumn(name = "foreing_key_of_category")
private Category category;
Upvotes: 1