【发布时间】:2016-06-08 12:50:15
【问题描述】:
如果我们正在为网站创建后端。我们显然会创建类别和帖子/产品。要添加/编辑/删除类别,我们显然会创建一个类似表的结构。例如:This is category listing as table
Category 在同一个表中会有一个子类别,子类别的数量可以是 1 - many。我想显示上图中提到的子类别的数量。
作为一名 CI 开发人员,我过去常常通过在视图中使用 Query 来做到这一点,
这是一个例子
<?php
if(sizeof($results)>0)
{
$i=1;
foreach($results as $key => $list)
{
if($i%2==0)$cls="row1"; else $cls="row2";
?>
<tr id="<?php echo $list->id;?>" class="<?php echo $cls; ?>">
<td> <?php echo $list->title, anchor("admin/categories/update/".$list->id."/", '<i class="fa fa-pencil rtrt"></i>' ); ?></td>
<td>
<?php
// it will return number of childs
echo anchor ('admin/categories/category/'.$list->id, $this->Common->select_child ('tbl_category', $list->id) );
?>
</td>
现在我正在使用 Spring-mvc、hibernate、Jpa 和 Mysql DB。我知道从 jsp 进行 sql 查询是一种不好的做法。
这是我的表格结构的 Jsp 代码,
<table class="table table-striped">
<thead>
<tr>
<th><i class="fa fa-pencil"></i></th>
<th>Category Name</th>
<th>Child</th>
<th>Created</th>
<th>Updated</th>
<th><input type="checkbox" /></th>
</tr>
</thead>
<tbody>
<c:if test="${not empty category}">
<c:forEach var="cat" items="${category}">
<tr>
<td><a href="${pageContext.request.contextPath}/category/${cat.id}/edit"><i class="fa fa-pencil"></i></a></td>
<td>${cat.title}</td>
<td><a href="${pageContext.request.contextPath}/category/show/${cat.id}"><i class="fa fa-folder-open"></i></a></td>
<td>${cat.created}</td>
<td>${cat.updated}</td>
<td><input type="checkbox" /></td>
</tr>
</c:forEach>
</c:if>
这里是类别 POjo 类
@Entity
@Table(name = "categories")
public class Categories {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
private Long parent_id;
private String title;
private String url_title;
private String description;
private String created;
private String updated;
/*
* @ At the time of Creating new user
* Auto persist created date
* Auto persist updated for first time
*/
@PrePersist
protected void onCreate (){
// Date conversion to string
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd h:mm:s");
sdf.setLenient(false);
String now = sdf.format(date);
created = now;
updated = now;
}
/*
* Respective Getter and Setter Methods
*
*/
public Long getId() {
return id;
}public void setId(Long id) {
this.id = id;
}
public Long getParent_id() {
return parent_id;
}public void setParent_id(Long parent_id) {
this.parent_id = parent_id;
}
public String getTitle() {
return title;
}public void setTitle(String title) {
this.title = title;
}
public String getUrl_title() {
return url_title;
}public void setUrl_title(String url_title) {
this.url_title = url_title;
}
public String getCreated() {
return created;
}public void setCreated(String created) {
this.created = created;
}
public String getUpdated() {
return updated;
}public void setUpdated(String updated) {
this.updated = updated;
}
public String getDescription() {
return description;
}public void setDescription(String description) {
this.description = description;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Categories other = (Categories) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
问题是
如何在不从 Jsp 执行 sql 查询的情况下获取孩子数? 请帮助我,我需要一个建议。
【问题讨论】:
-
可以添加分类实体plez吗?
-
我刚刚添加了分类类?你能建议我,如何实现我所需要的。
-
我已经发布了一个答案,但我不知道你的真正意思是:children count
-
考虑一个服装网站类别的示例,该类别将具有价值,例如(男装、童装、女装),其中儿童将是 T 恤、牛仔裤、衬衫。
-
像这样:
@OneToMany private List<Categories> subCategories;
标签: java spring hibernate spring-mvc jpa