Hibernate加入两张表并在spring restful webservice中生成json响应? [英] Hibernate joining two table and producing json response in spring restful webservice?

查看:83
本文介绍了Hibernate加入两张表并在spring restful webservice中生成json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两张表格


$ b


活动



lockquote

在表类别前五个 category_name 是主
category.according这五个我必须获取result.for
例如,如果用户给 1 作为输入首先我需要排序 category_id
parent_category_id 1 位于类别表 。在这个例子中,我将
获取 category_id 6,7,8,9,10 ,因为它们有 parent_category_id 1

接下来,请参阅 Events table 我也有名称为
category_id 的字段。我得到了 Category category_id 的结果,即
,category_id 6,7 ,8,9,10



我必须从 Events table 其中 category_id
与我们从类别表

获得的结果相匹配。

Confusion ?????????


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


因为在我的事件表中只有
category_id 6和7 的记录。



我的查询正在做他的工作,



  SELECT *从e类别c内部连接`事件`e上e.category_id = c.category_id其中c.parent_category_id = 1; 




问题是我必须将查询与 hibernate ,并且
应该用 json spring restful web服务 >格式遵循
向客户端发送。



如果用户输入参数为 1



输出应该是json,或者是像这样的任何标准格式



  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转换,与
春天宁静的web服务。



现在,只要举行JSON转换,请帮助我fet ch
记录就像我提到的那样


目前为止我的密码
$ b


用于hibernate映射的事件实体类




  import java.io.Serializable; 
import java.util.Date;
import javax.persistence。*;

/ **
*用户数据库表的持久类。
*
* /
@Entity
@Table(name =events)
public class Events实现Serializable {

private static最终的长串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;

私有整数状态;

@Column(name =created_date)
@Temporal(javax.persistence.TemporalType.DATE)
private日期createdDate;

@Column(name =last_updated_date)
@Temporal(javax.persistence.TemporalType.DATE)
private 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;
}

}




类别实体




  import java.io.Serializable; 
import java.util.Date;
import javax.persistence。*;

/ **
*用户数据库表的持久类。
*
* /
@Entity
@Table(name =category)
public class Category implements Serializable {

private static最终的长串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日期createdDate;

@Column(name =last_updated_date)
@Temporal(javax.persistence.TemporalType.DATE)
private 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;
}

}




Fetch category method




  public List< Object []> getCategoryList(int id)抛出SQLException,ClassNotFoundException,IOException {
List< Object []> groupList = null;
尝试{
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;
}




我应该如何编写HQL查询?并使用spring restful web服务以标准json格式将结果发回客户端
?帮助我的朋友。


解决方案


  1. 直接处理HQL并不好。你可能想看看 jOOQ Querydsl 帮助您创建类型安全的查询。

  2. 为了您的JSON转换,您可以查看


I have two table

Category

Events

In table Category first five category_name is the main category.according to those five i have to fetch the result.for example if user giving 1 as the input first i need to sort category_id who have parent_category_id 1 in Category table.in this example i will get the category_id 6,7,8,9,10 because they have parent_category_id 1.

Next , see in Events table also i have a field with name category_id. i have the result from Category with category_id ie , category_id 6,7,8,9,10.

I have to fetch all records from Events table whose category_id matching from the result that we got from Category table.

Confusion ?????????

See below my snap shot with query and result

This result i got because in my Event table only have records with category_id 6 and 7.

My query is doing his job,

SELECT * FROM category c  inner join `events` e on e.category_id=c.category_id where c.parent_category_id=1;

The problem is i have to integrate the query with hibernate and should respond my spring restful web service with the json format follows towards client.

If the user giving input parameter as 1

Output should be json follows or in any standard format like that

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},...
            ],......
    } 

I done simple json conversions in spring by adding json library with spring restful web service.

Now , just hold the json conversion and please help me to fetch records like i mentioned ?

And so far my codes

Events entity class for hibernate mapping

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;
    }

}

Category entity

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;
    }

}

Fetch category method

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;
    }

How i should write the HQL query ? and Send back the result to client in standard json format using spring restful web service ? Help me friends.

解决方案

  1. Dealing with HQL directly is not that nice. You might wanna look into jOOQ or Querydsl to help you with creating type-safe queries.
  2. For your JSON conversion, you could for example look into this answer here or create a wrapping class, that maps your Category entities with the associated Events and use one of the many tutorials out there, like this.

这篇关于Hibernate加入两张表并在spring restful webservice中生成json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆