2016-03-29 347 views
0

我有三個表:Hibernate如何一次查詢三個表?

1. org, 
2. product_info 
3. service_info. 

而且,表product_info是映射表service_infoManyToMany

手段,許多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類

@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

謝謝你原諒我的英語不好,這是我第一次的問題在堆棧溢出。

+1

請添加你的持久化類。 –

+0

Hibernate不查詢表,它查詢實體。 – Tobb

+0

如果可能的話發佈你的實體類... –

回答

0

好的,我解決它。 我發現它的樣品,哈哈。 因爲大部分標準都被打包了。第一次,我想用HQL來解決它,但徒勞無功。我只是添加了這些句子,並且得到了它。 enter image description here