我在优化Hibernate查询以免执行联接或辅助选择时遇到了麻烦。
执行Hibernate查询(条件或hql)时,例如:
return getSession().createQuery(("from GiftCard as card where card.recipientNotificationRequested=1").list();
…,而where子句检查不需要与其他表进行任何联接的属性…,但是Hibernate仍与其他表进行完全联接(或辅助选择,具体取决于我如何设置fetchMode)。
有问题的对象(GiftCard)有几个ManyToOne关联,在这种情况下(但不一定是所有情况),我希望它们被延迟加载。我想要一个解决方案,可以控制执行查询时延迟加载的内容。
以下是GiftCard实体的外观:
@Entity
@Table(name = "giftCards")
public class GiftCard implements Serializable
{
private static final long serialVersionUID = 1L;
private String id_;
private User buyer_;
private boolean isRecipientNotificationRequested_;
@Id
public String getId()
{
return this.id_;
}
public void setId(String id)
{
this.id_ = id;
}
@ManyToOne
@JoinColumn(name = "buyerUserId")
@NotFound(action = NotFoundAction.IGNORE)
public User getBuyer()
{
return this.buyer_;
}
public void setBuyer(User buyer)
{
this.buyer_ = buyer;
}
@Column(name="isRecipientNotificationRequested", nullable=false, columnDefinition="tinyint")
public boolean isRecipientNotificationRequested()
{
return this.isRecipientNotificationRequested_;
}
public void setRecipientNotificationRequested(boolean isRecipientNotificationRequested)
{
this.isRecipientNotificationRequested_ = isRecipientNotificationRequested;
}
}
如前所述
我想要一个 可以控制 执行查询时 延迟加载的内容 的解决方案
如果您有这样的映射
@Entity
public class GiftCard implements Serializable {
private User buyer;
@ManyToOne
@JoinColumn(name="buyerUserId")
public User getBuyer() {
return this.buyer;
}
}
默认情况下,任何
ToOne关系(例如@OneToOne和@ManyToOne)都是FetchType.EAGER,这意味着将始终对其进行提取。但是,这可能不是您想要的。当
我可以控制延迟加载的内容时, 您所说的 内容 可以翻译为“
获取策略”*
。Action
Book中的POJO支持这种模式(Notice方法签名)
public class GiftCardRepositoryImpl implements GiftCardRepository {
public List<GiftCard> findGiftCardWithBuyer() {
return sessionFactory.getCurrentSession().createQuery("from GiftCard c inner join fetch c.buyer where c.recipientNotificationRequested = 1").list();
}
}
因此,根据您的用例,您可以创建自己的 find … With … And … 方法。它会照顾 您想要的东西
但这有一个问题:它不支持通用方法签名。对于每个@Entity存储库,您必须定义自定义 find … With … And
方法。因此,我向您展示了如何定义通用存储库
public interface Repository<INSTANCE_CLASS, UPDATABLE_INSTANCE_CLASS, PRIMARY_KEY_CLASS> {
void add(INSTANCE_CLASS instance);
void remove(PRIMARY_KEY_CLASS id);
void update(PRIMARY_KEY_CLASS id, UPDATABLE_INSTANCE_CLASS updatableInstance);
INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id);
INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id, FetchingStrategy fetchingStrategy);
List<INSTANCE_CLASS> findAll();
List<INSTANCE_CLASS> findAll(FetchingStrategy fetchingStrategy);
List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize);
List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize, FetchingStrategy fetchingStrategy);
List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria);
List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria, FetchingStrategy fetchingStrategy);
List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria);
List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, FetchingStrategy fetchingStrategy);
}
但是, 有时
,您不希望通用存储库接口定义所有方法。解决方案:创建一个AbstractRepository类,它将实现一个虚拟存储库。例如,Spring
Framework 大量使用这种模式 接口>> AbstractInterface
public abstract class AbstractRepository<INSTANCE_CLASS, UPDATABLE_INSTANCE_CLASS, PRIMARY_KEY_CLASS> implements Repository<INSTANCE_CLASS, UPDATABLE_INSTANCE_CLASS, PRIMARY_KEY_CLASS> {
public void add(INSTANCE_CLASS instance) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void remove(PRIMARY_KEY_CLASS id) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void update(PRIMARY_KEY_CLASS id, UPDATABLE_INSTANCE_CLASS updatableInstance) {
throw new UnsupportedOperationException("Not supported yet.");
}
public INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id) {
throw new UnsupportedOperationException("Not supported yet.");
}
public INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id, FetchingStrategy fetchingStrategy) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAll() {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAll(FetchingStrategy fetchingStrategy) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize, FetchingStrategy fetchingStrategy) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria, FetchingStrategy fetchingStrategy) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria) {
throw new UnsupportedOperationException("Not supported yet.");
}
public List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, FetchingStrategy fetchingStrategy) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
因此,您的GiftCardRepository可以重写为(请参见扩展而不是工具),并且 可以覆盖您真正想要的内容
public class GiftCardRepository extends AbstractRepository<GiftCard, GiftCard, String> {
public static final GIFT_CARDS_WITH_BUYER GIFT_CARDS_WITH_BUYER = new GIFT_CARDS_WITH_WITH_BUYER();
public static final GIFT_CARDS_WITHOUT_NO_RELATIONSHIP GIFT_CARDS_WITHOUT_NO_RELATIONSHIP = new GIFT_CARDS_WITHOUT_NO_RELATIONSHIP();
public List<GiftCard> findAll(FetchingStrategy fetchingStrategy) {
sessionFactory.getCurrentSession().getNamedQuery(fetchingStrategy.toString()).list();
}
/**
* FetchingStrategy is just a marker interface
* public interface FetchingStrategy {}
*
* And AbstractFetchingStrategy allows you to retrieve the name of the Fetching Strategy you want, by overriding toString method
* public class AbstractFetchingStrategy implements FetchingStrategy {
*
* @Override
* public String toString() {
* return getClass().getSimpleName();
* }
*
* }
*
* Because there is no need to create an instance outside our repository, we mark it as private
* Notive each FetchingStrategy must match a named query
*/
private static class GIFT_CARDS_WITH_BUYER extends AbstractFetchingStrategy {}
private static class GIFT_CARDS_WITHOUT_NO_RELATIONSHIP extends AbstractFetchingStrategy {}
}
现在,我们将命名查询外部化为多行且可读且可维护的xml文件
// app.hbl.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<query name="GIFT_CARDS_WITH_BUYER">
<![CDATA[
from
GiftCard c
left join fetch
c.buyer
where
c.recipientNotificationRequested = 1
]]>
</query>
<query name="GIFT_CARDS_WITHOUT_NO_RELATIONSHIP">
<![CDATA[
from
GiftCard
]]>
</query>
</hibernate-mapping>
因此,如果您想与买家一起找回GiftCard,只需致电
Repository<GiftCard, GiftCard, String> giftCardRepository;
List<GiftCard> giftCardList = giftCardRepository.findAll(GiftCardRepository.GIFT_CARDS_WITH_WITH_BUYER);
并要获取没有任何关系的GiftCard,只需致电
List<GiftCard> giftCardList = giftCardRepository.findAll(GiftCardRepository.GIFT_CARDS_WITHOUT_NO_RELATIONSHIP);
或使用import static
import static packageTo.GiftCardRepository.*;
和
List<GiftCard> giftCardList = giftCardRepository.findAll(GIFT_CARDS_WITHOUT_NO_RELATIONSHIP);
希望对您有用!
问题内容: 在Hibernate 3中,有没有办法等效于以下HQL中的MySQL限制? 如果可能,我不想使用setMaxResults。这肯定在较旧的Hibernate / HQL版本中是可行的,但似乎已经消失了。 问题答案: 几年前,当有人问到为什么它在Hibernate 2中有效但在Hibernate 3中无效时,此消息发布在Hibernate论坛上: 在HQL中, 从不 支持Limit 子句
问题内容: 对于开发人员何时使用联接而不是子查询是否有经验法则还是相同的? 问题答案: 取决于RDBMS。您应该比较两个查询的执行计划。 根据我对Oracle 10和11的经验,执行计划始终是相同的。
我在创建使用嵌套查询或使用联接更新实体CommitteeMembership的HQL时遇到问题,我首先尝试了以下查询: 但生成的SQL错误如下: 在“交叉连接”之后没有任何this使Hibernate抛出SQLGrammarException 之后,我将查询更改为使用子查询: 现在Hibernate投球 任何人都知道我如何在HQL中编写此更新查询??
我为Hibernate创建了一个自定义类型来存储OffsetDateTime的时间戳和偏移量(因为默认的JPA 2.2/Hibernate 5.2 with java 8支持实现会丢失偏移量信息): 现在,我想比较一下,所以这个JPA-QL查询可以工作: 在这个模型上: } 但这在以下方面失败了: 如何使CustomUserType具有可比性?
问题内容: 我在MySQL中有以下语言表,用于选择其他语言的文本。 该表包含以下数据 组字段告诉我每种翻译属于哪些文本。在上面的示例中,组“ 3”具有默认文本(英语)和德语翻译。 现在,我想选择德语的所有文本,如果不存在,我将要使用后备英语文本。 有人知道如何将它们整合到SQL语句中吗? 问题答案: SQLFiddle演示
关于执行查询的信息 $this->db->insert_id() 当执行 INSERT 语句时,这个方法返回新插入行的ID。 注解 If using the PDO driver with PostgreSQL, or using the Interbase driver, this function requires a $name parameter, which specifies the