gpt4 book ai didi

java - Hibernate如何同时查询三个表?

转载 作者:行者123 更新时间:2023-11-29 21:12:04 24 4
gpt4 key购买 nike

我有三张 table :

 1. org,
2. product_info
3. service_info.

并且,表product_info是映射表service_info ManyToMany

意味着许多产品映射许多服务

而表org是映射表product_info OneToMany

意味着一个组织拥有许多产品

当我初始化我的网络时

enter image description here

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

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

感谢您原谅我糟糕的英语,这是我第一次在 Stack Overflow 上提问。

最佳答案

好的,我解决了。我发现它是如此样本,哈哈。因为,大部分的条件都已经封装好了,第一次想用HQL来解析,但是没成功,直接加上这句话,就搞定了。 enter image description here

关于java - Hibernate如何同时查询三个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36278530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com