JAVA : HOW TO Join same table in hibernate and select column's value

标签 java mysql spring hibernate

我有一个类别表。

创建表.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 

产品类别.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;
}

使用类似的内容添加对父类别的引用。

您可以以相同的方式为子类别列表添加@OneToMany

关于JAVA : HOW TO Join same table in hibernate and select column's value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32652180/

相关文章:

java - 从Json数组中获取指定值

python - 关于 Django 项目中的数据库设置

spring - 如何使用 Spring Batch 读取多个表

spring - 用于创建 Kafka Producer 的演示应用程序为 java.lang.InstantiationException : null 引发 "Failed to construct kafka producer"错误

java - 在Java中将xml转换为json时如何在标签中保留空格?

java - 简单的入队 Action ,没那么简单

java - 接收比较器类类型

java - 为什么日期 1986/05/04 从 1 :00 but not 0:00 in java ( strange behaviours of calendar with the time zone - Shanghai )? 开始

java - 在mysql中将mysql日期格式转换为dd-mmm-yyyy

java - 在 NamedParameterJdbcTemplate 类型中不适用于参数 (String, new RowMapper<User>(){})