我有两张表
分类
事件
在表
Category
中,前五个category_name
是主要的 category.according 5,我必须获取结果.for 例如,如果用户先输入1
作为输入,我需要对category_id
进行排序 在parent_category_id 1
Category table
中有category_id 6,7,8,9,10
的人。在这个例子中,我会 得到parent_category_id 1
,因为他们有Events table
。接下来,请参阅
category_id
我也有一个名字字段Category
。我的结果来自category_id
和6,7,8,9,10
ie ,category_idEvents table
。我必须从
category_id
Category table
获取所有记录 我们从Event table
获得的结果匹配。
混淆??????????
请参阅下面的带有查询和结果的快照
我得到的结果是因为我的
category_id 6 and 7
只有记录SELECT * FROM category c inner join `events` e on e.category_id=c.category_id where c.parent_category_id=1;
。我的查询正在完成他的工作,
hibernate
问题是我必须将查询与
spring restful web service
集成并且应该 以下列json
格式回复我的1
对客户。如果用户将输入参数设为
Events { Infotech[ {event_id:1,event_name:java_workshop},{event_id:2,event_name:java_workshop},...... ], Socia[ {event_id:1,event_name:java_workshop},{event_id:2,event_name:java_workshop},... ],...... }
输出应该是json跟随或以任何标准格式
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
/**
* The persistent class for the user database table.
*
*/
@Entity
@Table(name = "events")
public class Events implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "event_id")
private int eventId;
@Column(name = "event_name")
private String eventName;
@Column(name = "event_description")
private String eventDescription;
@Column(name = "category_id")
private Integer categoryId;
@Column(name = "is_trending_event")
private Integer isTrendingEvent;
@Column(name = "image_url")
private String imageUrl;
private Integer status;
@Column(name = "created_date")
@Temporal(javax.persistence.TemporalType.DATE)
private Date createdDate;
@Column(name = "last_updated_date")
@Temporal(javax.persistence.TemporalType.DATE)
private Date lastUpdatedDate;
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Date getLastUpdatedDate() {
return lastUpdatedDate;
}
public void setLastUpdatedDate(Date lastUpdatedDate) {
this.lastUpdatedDate = lastUpdatedDate;
}
public int getEventId() {
return eventId;
}
public void setEventId(int eventId) {
this.eventId = eventId;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public String getEventDescription() {
return eventDescription;
}
public void setEventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public Integer getIsTrendingEvent() {
return isTrendingEvent;
}
public void setIsTrendingEvent(Integer isTrendingEvent) {
this.isTrendingEvent = isTrendingEvent;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
我在春天通过添加json库完成了简单的json转换 春天宁静的网络服务。
现在,只需按住json转换,请帮我取 像我提到的记录?
到目前为止我的代码
用于hibernate映射的事件实体类
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
/**
* The persistent class for the user database table.
*
*/
@Entity
@Table(name = "category")
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
private int categoryId;
@Column(name = "category_name")
private String categoryName;
@Column(name = "parent_category_id")
private Integer parentCategoryId;
@Column(name = "created_date")
@Temporal(javax.persistence.TemporalType.DATE)
private Date createdDate;
@Column(name = "last_updated_date")
@Temporal(javax.persistence.TemporalType.DATE)
private Date lastUpdatedDate;
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public Integer getParentCategoryId() {
return parentCategoryId;
}
public void setParentCategoryId(Integer parentCategoryId) {
this.parentCategoryId = parentCategoryId;
}
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public Date getLastUpdatedDate() {
return lastUpdatedDate;
}
public void setLastUpdatedDate(Date lastUpdatedDate) {
this.lastUpdatedDate = lastUpdatedDate;
}
}
类别实体
public List<Object[]> getCategoryList(int id) throws SQLException, ClassNotFoundException, IOException {
List<Object[]> groupList = null;
try {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM Category c inner join Events e on e.categoryId=c.categoryId where c.parentCategoryId= :id");
query.setParameter("id", id);
groupList = query.list();
} catch (Exception e) {
}
return groupList;
}
获取类别方法
isset()
我应该如何编写HQL查询?并将结果发送回客户端 使用spring restful web服务的标准json格式?帮帮我的朋友。
答案 0 :(得分:0)