J'ai deux classes d'entités Catégorie et Evénements.J'ai besoin de joindre les deux tables et chercher tous les enregistrements qui correspondent à la condition donnée
Ma requête SQL pour cette
SELECT * FROM category c inner join `events` e on e.category_i=c.category_id where c.parent_category_id=1;
Comment puis-je convertir cette requête SQL en HTML et récupérer les données? J'ai essayé ci-dessous mais je n'ai pas obtenu le résultat? Suis très nouveau pour hiberner
Classe d'entités événements pour le mappage hibernate
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_i")
private Integer categoryI;
public Integer getCategoryI() {
return categoryI;
}
public void setCategoryI(Integer categoryI) {
this.categoryI = categoryI;
}
@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 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;
}
}
Entité de catégorie
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;
@ManyToOne
@JoinTable(name="events", joinColumns = @JoinColumn(name="category_i"))
private Events events;
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;
}
}
Méthode de recherche par catégorie
public List<Object[]> getCategoryList(int id) throws SQLException, ClassNotFoundException, IOException {
List<Object[]> groupList = null;
try {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("select e from Category e inner join e.events where e.parentCategoryId=1");
//query.setParameter("id", id);
groupList = query.list();
} catch (Exception e) {
}
return groupList;
}
Vous devez penser en termes d’objets Java lorsque vous utilisez ORM tools.
D'après votre question, je pense que la requête que vous essayez d'écrire ressemblera à quelque chose comme:
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;
}
L'un des avantages de l'utilisation d'un ORM est qu'il résout la requête de jointure complète pour vous.
Pour que cela fonctionne, vous devez mettre à jour votre modèle de classe comme suit:
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;
...
}
et
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;
...
}
Hibernate concerne le mappage des objets et des relations, mais vous mappez des champs id simples.
Dans votre classe Events
, vous avez ce qui suit L
@Entity
@Table(name = "events")
public class Events implements Serializable {
@Column(name = "category_i")
private Integer categoryI;
}
Cependant, il devrait s'agir d'une Category
au lieu d'une Integer
.
@Entity
@Table(name = "events")
public class Events implements Serializable {
@ManyToOne
@Column(name = "category_i")
private Category category;
}
Ensuite, dans votre Category
, vous devez ajouter le champ mappedBy
au @ManyToOne
du champ events
et supprimer le @JoinColumn
.
@Entity
@Table(name = "category")
public class Category implements Serializable {
@OneToMany(mappedBy="category")
private Events events;
}
La même chose s'applique à la parentCategoryId
de la classe Category
.
Maintenant que votre cartographie est corrigée, vous devriez pouvoir écrire la requête comme vous le souhaitez.
Ty ce code ci-dessous
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();
}
J'espère que ça marche.