I have two entity class Category and Events.I need to join both the tables and fetch all records which matching the given condition
Hibernate is about mapping objects and the relations, however you are mapping simple id fields.
In your Events
class you have the followingL
@Entity
@Table(name = "events")
public class Events implements Serializable {
@Column(name = "category_i")
private Integer categoryI;
}
However it should be a Category
instead of an Integer
.
@Entity
@Table(name = "events")
public class Events implements Serializable {
@ManyToOne
@Column(name = "category_i")
private Category category;
}
Then in your Category
you should add the mappedBy
field to the @ManyToOne
on the events
field and remove the @JoinColumn
.
@Entity
@Table(name = "category")
public class Category implements Serializable {
@OneToMany(mappedBy="category")
private Events events;
}
The same applies to the parentCategoryId
of the Category
class.
Now that you have your mapping corrected you should be able to write the query as you wanted to.
This query should do the job:
from Category as c
inner join c.events as e
where c.parentCategoryId = 1
Plus you seem to have a typo (missing "d" at the end) here:
@JoinColumn(name="category_i"))
You need to think in terms of Java objects when using ORM tools.
From your question I think the query that you're trying to write will look something like:
public List<Category> getCategoryList(int id) {
List<Category> groupList;
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("select c from Category c join fetch c.events where c.parentCategory.categoryId = 1");
//query.setParameter("id", id);
groupList = query.list();
return groupList;
}
One of the benefits of using an ORM is that it works out the full join query for you.
For this to work you need to update your class model as follows:
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Entity
@Table(name = "events")
public class Event 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;
@ManyToOne
@JoinColumn(name = "category_i")
private Category category;
@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;
...
}
and
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@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;
@ManyToOne
@JoinColumn(name="parent_category_id")
private Category parentCategory;
@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;
@OneToMany(mappedBy="category")
private List<Event> events;
...
}
Ty this below code
public List<Object[]> getCategoryList(int id) throws SQLException, ClassNotFoundException, IOException {
Session session = sessionFactory.getCurrentSession();
return session.createCriteria(Catagory.class)
.setFetchMode("events", FetchMode.JOIN)
.add(Restrictions.eq("parentCatagoryId", 1))
.list();
}
Hope this stuff works.