[英]JAVA : HOW TO Join same table in hibernate and select column's value
I have a category table. 我有一个类别表。
createTable.sql createTable.sql
CREATE TABLE IF NOT EXISTS `product_category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`category_name` varchar(10) NOT NULL,
`parent_category_id` int(11) DEFAULT NULL,
`created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created_by` int(11) NOT NULL,
PRIMARY KEY (`category_id`),
KEY `created_by` (`created_by`),
KEY `parent_category_id` (`parent_category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11
ProductCategory.java ProductCategory.java
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
@Entity
@Table(name="product_category")
public class ProductCategory {
private int categoryId;
private String categoryName;
private Integer parentCategoryId;
private Timestamp createdOn;
private int createdBy;
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="category_id", unique=true, nullable=false)
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
@Column(name="category_name", nullable=false, length=100)
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
@Column(name="parent_category_id")
public Integer getParentCategoryId() {
return parentCategoryId;
}
public void setParentCategoryId(Integer parentCategoryId) {
this.parentCategoryId = parentCategoryId;
}
@Column(name="created_on")
public Timestamp getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Timestamp createdOn) {
this.createdOn = createdOn;
}
@Column(name="created_by")
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public ProductCategory(String categoryName) {
this.categoryName = categoryName;
}
public ProductCategory() {
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="parent_category_id")
private ProductCategory subcategory;
@OneToMany(mappedBy="subcategory")
private Set<ProductCategory> subordinates=new HashSet<ProductCategory>();
}
@ManyToOne
@JoinColumn(name = "parent_category_id")
public ProductCategory getParentCategory() {
return parentCategory;
}
public void setParentCategoryId(ProductCategory parentCategory) {
this.parentCategory = parentCategory;
}
Use something like this to add a reference to parent category. 使用类似这样的方法来添加对父类别的引用。
You can add @OneToMany for list of subcategories the same way 您可以以相同的方式为子类别列表添加@OneToMany
You have mentioned " parent_category_id " twice in your code. 您在代码中两次提到了“ parent_category_id ”。 It is only required at "@JoinColumn(name="parent_category_id")".
仅在“ @JoinColumn(name =“ parent_category_id”)”处需要。 Below is the final mapping for your requirement:
以下是您要求的最终映射:
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="category_id", unique=true, nullable=false)
private int categoryId;
@Column(name="category_name", nullable=false, length=100)
private String categoryName;
@Column(name="created_on")
private Timestamp createdOn;
@Column(name="created_by")
private int createdBy;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="parent_category_id")
private ProductCategory subcategory;
@OneToMany(mappedBy="subcategory")
private Set<ProductCategory> subordinates=new HashSet<ProductCategory>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.