Hibernate如何一次查询三个表?

时间:2016-03-29 07:55:26

标签: java mysql hibernate

我有三张桌子:

 1. org,
 2. product_info 
 3. service_info.

而且,表product_info是映射表service_info ManyToMany

表示很多products映射了许多services

虽然,表org是映射表product_info OneToMany

表示一个orgmany products

初始化网页时

enter image description here

我想查看org表的column。怎么做 ? 在类下面是三个表的持久类。

ProductService类: `

@Entity
@Table(name="product_service")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)

public class ProductService  implements java.io.Serializable {

private static final long serialVersionUID = 1L;

 private String id;
 private ServiceInfo serviceInfo;//this is the service table
 private String parammapping;
 private ProductInfo productInfo;//this is the product table


// Constructors

/** default constructor */
public ProductService() {
}

/** minimal constructor */
public ProductService(String id) {
    this.id = id;
}



// Property accessors
@Id   
@Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="SERVICEID")
public ServiceInfo getServiceInfo() {
    return this.serviceInfo;
}

public void setServiceInfo(ServiceInfo serviceInfo) {
    this.serviceInfo = serviceInfo;
}


@Column(name="PARAMMAPPING", length=1000)
public String getParammapping() {
    return parammapping;
}

public void setParammapping(String parammapping) {
    this.parammapping = parammapping;
}

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="PRODUCTID")
public ProductInfo getProductInfo() {
    return this.productInfo;
}

public void setProductInfo(ProductInfo productInfo) {
    this.productInfo = productInfo;
}

}`

baseOrg class

@Entity
@Table(name="base_org")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class BaseOrg  implements java.io.Serializable {
private static final long serialVersionUID = 1L;

 private String id;
 private String code;
 private String name;

private List<BaseRuleEngineLog> serviceUsedLogs = new ArrayList<BaseRuleEngineLog>(0);
 private List<ProductInfo> productInfos = new ArrayList<ProductInfo>(0);
 private List<BaseCreditQuery> baseCreditQueries = new ArrayList<BaseCreditQuery>(0);

// Constructors

/** default constructor */
public BaseOrg() {
}

/** minimal constructor */
public BaseOrg(String id) {
    this.id = id;
}

@Id 
@Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}

@Column(name="CODE", length=50)

public String getCode() {
    return this.code;
}

public void setCode(String code) {
    this.code = code;
}

@Column(name="NAME", length=200)

public String getName() {
    return this.name;
}
public void setName(String name) {
    this.name = name;
}


@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="baseOrg")
public List<BaseRuleEngineLog> getServiceUsedLogs() {
    return this.serviceUsedLogs;
}

public void setServiceUsedLogs(List<BaseRuleEngineLog> serviceUsedLogs) {
    this.serviceUsedLogs = serviceUsedLogs;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="baseOrg")
public List<ProductInfo> getProductInfos() {
    return this.productInfos;
}

public void setProductInfos(List<ProductInfo> productInfos) {
    this.productInfos = productInfos;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="baseOrg")
public List<BaseCreditQuery> getBaseCreditQueries() {
    return this.baseCreditQueries;
}

public void setBaseCreditQueries(List<BaseCreditQuery> baseCreditQueries) {
    this.baseCreditQueries = baseCreditQueries;
}

}

productInfo类

@Entity
@Table(name="product_info")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class ProductInfo  implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private String id;
 private BaseOrg baseOrg;//baseOrg table
 private String code;
 private String name;
 private String orgcode;

 private List<ProductService> productServices = new ArrayList<ProductService>(0);


// Constructors

/** default constructor */
public ProductInfo() {
}

/** minimal constructor */
public ProductInfo(String id) {
    this.id = id;
}



// Property accessors
@Id 
@Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ORGID")
public BaseOrg getBaseOrg() {
    return this.baseOrg;
}

public void setBaseOrg(BaseOrg baseOrg) {
    this.baseOrg = baseOrg;
}

@Column(name="CODE", length=100)
public String getCode() {
    return this.code;
}

public void setCode(String code) {
    this.code = code;
}

@Column(name="NAME", length=100)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}


@Column(name="ORGCODE", length=100)
public String getOrgcode() {
    return this.orgcode;
}

public void setOrgcode(String orgcode) {
    this.orgcode = orgcode;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="productInfo")
public List<ProductService> getProductServices() {
    return this.productServices;
}

public void setProductServices(List<ProductService> productServices) {
    this.productServices = productServices;
}

}

serviceInfo类

@Entity
@Table(name="service_info")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class ServiceInfo  implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String id;
 private String name;
 private String code;
 private List<ProductService> productServices = new ArrayList<ProductService>(0);

// Constructors

/** default constructor */
public ServiceInfo() {
}

/** minimal constructor */
public ServiceInfo(String id) {
    this.id = id;
}

// Property accessors
@Id 
@Column(name="ID", unique=true, nullable=false, length=50)
public String getId() {
    return this.id;
}

public void setId(String id) {
    this.id = id;
}


@Column(name="NAME", length=100)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name="CODE", length=100)
public String getCode() {
    return this.code;
}

public void setCode(String code) {
    this.code = code;
}


@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="serviceInfo")
public List<ProductService> getProductServices() {
    return this.productServices;
}

public void setProductServices(List<ProductService> productServices) {
    this.productServices = productServices;
}

} product_service table

感谢您原谅我的英语不好,这是我第一次质疑Stack Overflow。

1 个答案:

答案 0 :(得分:0)

好的,我解决了。 我发现它的样本,哈哈。 因为,大多数标准已被打包。第一次,我想使用HQL来解决它,但是徒劳无功。我只是添加这些句子,并得到它。 enter image description here