我有2个型号:
合约标题
public class ContractHeader {
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "contractHeader")
private List<ContractEntitlement> contractEntitlements;
ContractEntitlement
public class ContractEntitlement {
@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id", nullable = false, insertable = false, updatable = false)
private ContractHeader contractHeader;
@Column(name = "ch_id")
private int chId;
1个合约(id)可以拥有多个权利(ch_id),因此当我创建表单时,我的控制器是:
ContractHeader ch = new ContractHeader();
ch.setContractEntitlements(new AutoPopulatingList<ContractEntitlement>(ContractEntitlement.class));
model.addAttribute("ch", ch);
这完美地工作,并且能够保存两个表的数据,但现在我的问题是检索它们:
查看控制器:
model.addAttribute("contractHeader", new ContractHeader());
model.addAttribute("ch", this.contractHeaderService.listContractHeaders());
model.addAttribute("contractEntitlement", new ContractEntitlement());
model.addAttribute("ce", this.contractEntitlementService.listContractEntitlements());
查看jsp
<c:forEach items="${ch}" var="contractHeader">
... Elements to show each data
<c:forEach items="${ce}" var="contractEntitlement">
<c:if test="${contractHeader.id == contractEntitlement.chId}" >
... Elements to show each data
我生成了2个对象并比较了jsp视图中的id。
DAO ContractHeader
Session session = this.sessionFactory.getCurrentSession();
List<ContractHeader> contractHeaderList = session.createQuery("from ContractHeader").list();
DAO ContractEntitlement
Session session = this.sessionFactory.getCurrentSession();
List<ContractEntitlement> contractEntitlementList = session.createQuery("from ContractEntitlement").list();
使用一个模型(ContractHeader)创建它们时,是否有更好或者我说的正确方法来获取数据?
答案 0 :(得分:1)
在你的情况下,因为看起来你正试图在循环中分层次地呈现数据,我会去查询哪些实体获取.../app/build.gradle
minSdkVersion 18 << less than 21 so req support libraries
targetSdkVersion 25
个ContractHeader
个实体:
ContractEntitlements
然后将页面部分更改为:
Session session = this.sessionFactory.getCurrentSession();
List<ContractHeader> contractHeaderList = session.createQuery(
"select ch from ContractHeader ch inner join fetch ch.contractEntitlements")
.list();
现在,我认为你不需要在嵌套循环中使用那个额外的测试。