Hibernate的FetchMode SELECT与JOIN [英] Hibernate FetchMode SELECT vs JOIN

查看:122
本文介绍了Hibernate的FetchMode SELECT与JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  @Entity 
public class TestContentElementResponse
{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
保护长ID;



@OneToMany(mappedBy =testContentElementResponse,cascade = CascadeType.ALL,orphanRemoval = false)
private Set< ResponseAttribute> associatedResponseAttributes = new HashSet< ResponseAttribute>();


@ManyToOne
@JoinColumn(name =userfulltestId,referencedColumnName =id,nullable = false)
private UserFullTest userFullTest;

@ManyToOne
@JoinColumn(name =testContentElementId,referencedColumnName =id,nullable = false)
private TestContentElement testContentElement;



@OneToOne(cascade = CascadeType.ALL)
private TestContentElementScore testContentElementScore;

@ManyToOne
@JoinColumn(name =userId,referencedColumnName =id,nullable = false)
私人用户用户;

TestContentElementResponse.class表示测试中的一个用户响应。
一个测试可以有30个问题,所以每个用户有30个响应。

现在我想为一个用户调用ALL TestContentElementResponse(常用的ID是UserFullTestId)和ALL每个TestContentElementResponse的ResponseAttributes。


我可以用标准查询来做到这一点,但我不确定是使用SELECT还是JOIN FetchMode。我明白,JOIN将会对数据库进行一次大的调用,而SELECT将进行许多快速调用。然而,我不知道哪些因素可以帮助我确定哪种方法是最优的。



(当然,我会运行测试,但这只会回答哪种方法是最优的,它不会解释为什么)



帮助将不胜感激

解决方案

<这里有一个很好的讨论,有一些具体的例子:

http://www.mkyong.com/hibernate/hibernate-fetching-strategies-examples/



如果您使用原始SQL(SQL语句或存储过程)进行编码,答案通常毫不费力:一个复杂的select / join查询几乎总是被多个查询所偏好。



就Hibernate而言,答案是join通常比select更好。



建议:修改你的spring.xml以启用show_sql,并比较结果。



<其他两个不错的链接:



I have the following class:

   @Entity
public class TestContentElementResponse
{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;



    @OneToMany(mappedBy = "testContentElementResponse", cascade = CascadeType.ALL, orphanRemoval = false)
    private Set<ResponseAttribute> associatedResponseAttributes = new HashSet<ResponseAttribute>();


    @ManyToOne
    @JoinColumn(name = "userfulltestId", referencedColumnName = "id", nullable = false)
    private UserFullTest userFullTest;

    @ManyToOne
    @JoinColumn(name = "testContentElementId", referencedColumnName = "id", nullable = false)
    private TestContentElement testContentElement;



    @OneToOne(cascade = CascadeType.ALL)
    private TestContentElementScore testContentElementScore;

    @ManyToOne
    @JoinColumn(name = "userId", referencedColumnName = "id", nullable = false)
    private User user;

TestContentElementResponse.class represents one user response in a test. One test can be 30 questions, so 30 responses per user.

Now I want to call ALL TestContentElementResponse for ONE user (common id is UserFullTestId) and ALL ResponseAttributes for each TestContentElementResponse.

I can do this with a criteria query, but I am not sure whether to use a SELECT or JOIN FetchMode. I do understand that JOIN will make one big call to the database and SELECT will make many rapid calls. However, I do not know what factors help me decide which method is optimum.

(Of course, I will run tests, but that will only answer which method is optimum, it won't explain why)

Help would be greatly appreciated

解决方案

Here is an excellent discussion, with some concrete examples:

http://www.mkyong.com/hibernate/hibernate-fetching-strategies-examples/

If you were coding in "raw SQL" (either SQL statements or stored procedures), the answer is usually a no-brainer: a single complex "select/join" query is almost always preferred to multiple queries.

As far as Hibernate, the answer is "join" is usually better than select.

SUGGESTION: modify your spring.xml to enable "show_sql", and compare the results.

PS:

Two other good links:

这篇关于Hibernate的FetchMode SELECT与JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆