Hibernate加入两个表并在spring restful webservice中生成json响应?

时间:2015-07-17 12:58:13

标签: java mysql json spring hibernate

  

我有两张表

     

分类

enter image description here

  

事件

enter image description here

  

在表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_id6,7,8,9,10 ie   ,category_id Events table

     

我必须从category_id Category table获取所有记录   我们从Event table获得的结果匹配。

混淆??????????

  

请参阅下面的带有查询和结果的快照

enter image description here

  

我得到的结果是因为我的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格式?帮帮我的朋友。

1 个答案:

答案 0 :(得分:0)

  1. 直接处理HQL并不是那么好。您可能需要查看jOOQQuerydsl以帮助您创建类型安全的查询。
  2. 对于您的JSON转换,您可以查看this answer here或创建一个包装类,它将您的Category实体与相关事件进行映射,并使用其中一个教程,如this