我有两个实体类Category和Events。我需要连接两个表并获取与给定条件匹配的所有记录
我的SQL查询
SELECT * FROM category c inner join `events` e on e.category_i=c.category_id where c.parent_category_id=1;
我怎样才能将此SQL查询转换为hql并获取数据? 我在下面尝试了但没有得到结果?hibernate很新
用于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;
}
}
类别实体
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;
}
}
提取类别方法
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;
}
使用ORM工具时,您需要考虑Java对象。
根据您的问题,我认为您要编写的查询看起来像:
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;
}
使用ORM的好处之一是它为您解决了完全联接查询。
为此,您需要按如下所示更新类模型:
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;
...
}
和
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;
...
}
问题内容: 假设有两个表: 表A 表B 表之间的联系是 messageID 字段。 我想要一个查询来生成这样的结果,在该查询中,我将所有字段从表A中拉出,并对表B中每条消息的注释数进行计数,如下所示: 我已经尝试过这样的事情: 但这是行不通的。有任何想法吗?似乎应该可以在一个查询中执行此操作。我正在使用MSSQL。谢谢你的帮助。 问题答案: 标量子查询将起作用: 与往常一样,有很多方法可以改变这只
我是SQL新手,目前不知道要搜索的正确单词。 我有第一张这样的桌子——它有5行。 这是我的第二张桌子: 第二个表有6行。 因此,如果我简单地使用内部联接、外部联接等方式联接表#1中的5行和表#2中的6行,它将只返回5行。 我如何加入,以便它将返回所有行,并为缺少的值设置默认值为零?(我也不知道哪个表的行数更多。它可能会时不时地改变,我无法进行左、右连接)。
我想加入2个表 表1: 表2: 我正在做的是 我期望得到的是
我正在寻找一种方法,以获得所有的DNS记录的网址<有没有一种方法可以在没有任何库的情况下使用C#实现这一点?
我有一个表,它在Oracle中有一个复合主键(多列的pk)。我正在运行一个示例查询以获取随机主键。 从表样(0.1)中选择col1、col2、col3、col4,其中col1='XXXXX'顺序为col2、col3、col4; 示例输出如下所示
我正在尝试获取数据库(Oracle 11g)中的所有表名,以便为UI中的每个表动态生成复选框。我没有映射.cfg.xml文件中的任何这些表。 我使用了下面的代码: 但它的错误如下:组织。冬眠hql。内部的ast。QuerySyntaxException:未映射user\u表[从user\u表中选择table\u名称] 请告诉我是否有任何方法可以在Hibernate 4中获取所有表名