DataNucleus是否具有M-1和1-1关联的批量(批量)提取功能?

时间:2016-10-11 05:33:44

标签: java jpa datanucleus

DataNucleus中是否有任何模拟功能,如Hibernate @BatchSize或EclipseLink @BatchFetch(BatchFetchType.IN)? 在JPQL中获取组和连接提取是IMO泄漏的抽象 - 调用者现在应该进行进一步处理所需的数据。在复杂的域代码中,几乎不可能知道具体业务调用需要什么。盲目地获取所有可能需要的数据远非有效的方式并且可以触发OOM。 使用FK上的JOIN获取单个ID的大量查询实际上会破坏我的应用程序性能。不在视图层而是业务逻辑层。 欢迎任何建议!

主要实体:

@Entity
@SequenceGenerator(name = "create_seq_question", sequenceName = "seq_question", allocationSize = 1)
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "create_seq_question")
@Column(name = "id")
private Long id;

@Temporal(TemporalType.TIMESTAMP)
@NotNull
private Date dateOfPost;

@Basic(optional = false)
@Enumerated(EnumType.ORDINAL)
private Status status = Status.NEW;

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private ChatMessage chatMessage;

//other fields and methods
}

相关实体:

@Entity
@SequenceGenerator(name = "create_seq_chatmessage", sequenceName = "seq_chatmessage", allocationSize = 1)
public class ChatMessage {
private static final Logger log = LoggerFactory.getLogger(ChatMessage.class);

public enum Type {
    QUESTION /*0*/, ANSWER /*1*/, TECHNICAL /*2*/, SUPERVISOR /*3*/, CLARIFYING_QUESTION /*4*/, TASK /*5*/, COMMUNICATION /*6*/, MARKETING /*7*/;
}

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "create_seq_chatmessage")
@Column(name = "id")
private Long id;

@NotNull
private String message;

@Temporal(TemporalType.TIMESTAMP)
private Date deleteDate;

@Temporal(TemporalType.TIMESTAMP)
private Date inputDate;

@Basic(optional = false)
@Enumerated(EnumType.ORDINAL)
private Type type;

@OneToOne(mappedBy = "chatMessage", fetch = FetchType.LAZY)
private Question question;

//other fields and methods

}

JPQL查询:

    List<Question> questions = entityManager.createQuery("SELECT q FROM Question q", Question.class).getResultList();
    for (Question question : questions) {
        ChatMessage cm = question.getChatMessage();
        //some logic
    }

生成的SQL:

SELECT 'core.domain.Question' AS DN_TYPE,Q.QUID,Q.ASTROLOGERSELECTIONTYPE,Q.CANCELLINGREASON,Q.CANCELLINGREASONTEXT,Q.CREATED,Q.DATEOFPOST,Q.ID,Q.PRIORITY,Q.STATUS,Q."TYPE",Q.UPDATED,Q.VERSION FROM QUESTION Q
SELECT B0.DATEOFSHARE,B0.DELETEDATE,B0.HIDEINWEBCHATHISTORY,B0.ID,B0.INPUTDATE,B0.ISRATETYPEBUTTON,B0.ISSHAREREQUIRED,B0.ISSHARED,B0.RATEDATE,B0.ISMARKNEEDED,B0.MARKVALUE,B0.MESSAGE,B0.MINIMUMMARKSCOREFORRATE,B0.RATEBUTTONTEXT,B0.ISRATED,B0.READDATE,B0.SHARINGTYPE,B0.STORETYPE,B0."TYPE",B0.UPDATED,B0.VERSION FROM QUESTION A0 LEFT OUTER JOIN CHATMESSAGE B0 ON A0.CHATMESSAGE_ID = B0.ID WHERE A0.ID = <101>
SELECT B0.DATEOFSHARE,B0.DELETEDATE,B0.HIDEINWEBCHATHISTORY,B0.ID,B0.INPUTDATE,B0.ISRATETYPEBUTTON,B0.ISSHAREREQUIRED,B0.ISSHARED,B0.RATEDATE,B0.ISMARKNEEDED,B0.MARKVALUE,B0.MESSAGE,B0.MINIMUMMARKSCOREFORRATE,B0.RATEBUTTONTEXT,B0.ISRATED,B0.READDATE,B0.SHARINGTYPE,B0.STORETYPE,B0."TYPE",B0.UPDATED,B0.VERSION FROM QUESTION A0 LEFT OUTER JOIN CHATMESSAGE B0 ON A0.CHATMESSAGE_ID = B0.ID WHERE A0.ID = <102>
SELECT B0.DATEOFSHARE,B0.DELETEDATE,B0.HIDEINWEBCHATHISTORY,B0.ID,B0.INPUTDATE,B0.ISRATETYPEBUTTON,B0.ISSHAREREQUIRED,B0.ISSHARED,B0.RATEDATE,B0.ISMARKNEEDED,B0.MARKVALUE,B0.MESSAGE,B0.MINIMUMMARKSCOREFORRATE,B0.RATEBUTTONTEXT,B0.ISRATED,B0.READDATE,B0.SHARINGTYPE,B0.STORETYPE,B0."TYPE",B0.UPDATED,B0.VERSION FROM QUESTION A0 LEFT OUTER JOIN CHATMESSAGE B0 ON A0.CHATMESSAGE_ID = B0.ID WHERE A0.ID = <103>
SELECT B0.DATEOFSHARE,B0.DELETEDATE,B0.HIDEINWEBCHATHISTORY,B0.ID,B0.INPUTDATE,B0.ISRATETYPEBUTTON,B0.ISSHAREREQUIRED,B0.ISSHARED,B0.RATEDATE,B0.ISMARKNEEDED,B0.MARKVALUE,B0.MESSAGE,B0.MINIMUMMARKSCOREFORRATE,B0.RATEBUTTONTEXT,B0.ISRATED,B0.READDATE,B0.SHARINGTYPE,B0.STORETYPE,B0."TYPE",B0.UPDATED,B0.VERSION FROM QUESTION A0 LEFT OUTER JOIN CHATMESSAGE B0 ON A0.CHATMESSAGE_ID = B0.ID WHERE A0.ID = <104>
SELECT B0.DATEOFSHARE,B0.DELETEDATE,B0.HIDEINWEBCHATHISTORY,B0.ID,B0.INPUTDATE,B0.ISRATETYPEBUTTON,B0.ISSHAREREQUIRED,B0.ISSHARED,B0.RATEDATE,B0.ISMARKNEEDED,B0.MARKVALUE,B0.MESSAGE,B0.MINIMUMMARKSCOREFORRATE,B0.RATEBUTTONTEXT,B0.ISRATED,B0.READDATE,B0.SHARINGTYPE,B0.STORETYPE,B0."TYPE",B0.UPDATED,B0.VERSION FROM QUESTION A0 LEFT OUTER JOIN CHATMESSAGE B0 ON A0.CHATMESSAGE_ID = B0.ID WHERE A0.ID = <105>
SELECT B0.DATEOFSHARE,B0.DELETEDATE,B0.HIDEINWEBCHATHISTORY,B0.ID,B0.INPUTDATE,B0.ISRATETYPEBUTTON,B0.ISSHAREREQUIRED,B0.ISSHARED,B0.RATEDATE,B0.ISMARKNEEDED,B0.MARKVALUE,B0.MESSAGE,B0.MINIMUMMARKSCOREFORRATE,B0.RATEBUTTONTEXT,B0.ISRATED,B0.READDATE,B0.SHARINGTYPE,B0.STORETYPE,B0."TYPE",B0.UPDATED,B0.VERSION FROM QUESTION A0 LEFT OUTER JOIN CHATMESSAGE B0 ON A0.CHATMESSAGE_ID = B0.ID WHERE A0.ID = <106>

经典的1 + N延迟加载问题+获取查询不仅仅是通过PK获取,而是使用主实体表进行LEFT OUTER JOIN。其他提供者在第一次查询中获取FK并仅通过相关实体的PK执行延迟加载。 我知道&#34; fetch-fk-only&#34;扩展,但结果更糟:

 SELECT 'core.domain.Question' AS DN_TYPE,Q.QUID,Q.ASTROLOGERSELECTIONTYPE,Q.CANCELLINGREASON,Q.CANCELLINGREASONTEXT,Q.CREATED,Q.DATEOFPOST,Q.ID,Q.PRIORITY,Q.STATUS,Q."TYPE",Q.UPDATED,Q.VERSION FROM QUESTION Q
 SELECT A0.CHATMESSAGE_ID FROM QUESTION A0 WHERE A0.ID = <101>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <101>
 SELECT A0.CHATMESSAGE_ID FROM QUESTION A0 WHERE A0.ID = <102>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <102>
 SELECT A0.CHATMESSAGE_ID FROM QUESTION A0 WHERE A0.ID = <103>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <103>
 SELECT A0.CHATMESSAGE_ID FROM QUESTION A0 WHERE A0.ID = <104>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <104>

N * 2 + 1(!)查询。 如果我把关系变得懒惰,我就会得到这个:

 SELECT 'core.domain.Question' AS DN_TYPE,Q.QUID,Q.ASTROLOGERSELECTIONTYPE,Q.CANCELLINGREASON,Q.CANCELLINGREASONTEXT,Q.CHATMESSAGE_ID,Q.CREATED,Q.DATEOFPOST,Q.ID,Q.PRIORITY,Q.STATUS,Q."TYPE",Q.UPDATED,Q.VERSION FROM QUESTION Q
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <101>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <102>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <103>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <104>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <105>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <106>
 SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID = <107>

在任何关系触摸之前,就在getResultList()之后。这是最糟糕的变体,因为N + 1查询实际上没有触及懒惰关系。

我需要的是:

SELECT 'core.domain.Question' AS DN_TYPE,Q.QUID,Q.ASTROLOGERSELECTIONTYPE,Q.CANCELLINGREASON,Q.CANCELLINGREASONTEXT,Q.CHATMESSAGE_ID,Q.CREATED,Q.DATEOFPOST,Q.ID,Q.PRIORITY,Q.STATUS,Q."TYPE",Q.UPDATED,Q.VERSION FROM QUESTION Q

getResultList()之后。然后当我触摸关系时:

SELECT A0.DATEOFSHARE,A0.DELETEDATE,A0.HIDEINWEBCHATHISTORY,A0.INPUTDATE,A0.ISRATETYPEBUTTON,A0.ISSHAREREQUIRED,A0.ISSHARED,A0.RATEDATE,A0.ISMARKNEEDED,A0.MARKVALUE,A0.MESSAGE,A0.MINIMUMMARKSCOREFORRATE,A0.RATEBUTTONTEXT,A0.ISRATED,A0.READDATE,A0.SHARINGTYPE,A0.STORETYPE,A0."TYPE",A0.UPDATED,A0.VERSION FROM CHATMESSAGE A0 WHERE A0.ID IN (<101>, <102>, <103>, <104>, <105>)

0 个答案:

没有答案