Java Hibernate关系获取配置文件

Java Hibernate关系获取配置文件,java,hibernate,fetch,Java,Hibernate,Fetch,我正在用Struts2、Spring和Hibernate开发一个应用程序,我正在进行模型获取优化。对于这个例子,考虑“ExpRePayTABLE”可能有超过500 K的记录,并且他的所有 OneToMany < /代码>关系可能有更多的记录(例如,文档< /代码>表,与文档>行>代码> > 下面是示例代码: @Entity @Table(name = "example") public class Example extends BaseModel { // Base Model is map

我正在用Struts2、Spring和Hibernate开发一个应用程序,我正在进行模型获取优化。对于这个例子,考虑“ExpRePayTABLE”可能有超过500 K的记录,并且他的所有<代码> OneToMany < /代码>关系可能有更多的记录(例如,<代码>文档< /代码>表,与<代码>文档>行>代码> >

下面是示例代码:

@Entity
@Table(name = "example")
public class Example extends BaseModel { // Base Model is mapped as superclass and contains the Id column and create,update,delete timestamps
    private String exampleName;

    /*
     * ...
     */

    /* 
     * This relation contains another relation inside it  
     * (ex. Set<ExampleRelationRelation> exampleRelationRelations)
     */
    private ExampleRelation1 exampleRelation1;

    private Set<ExampleRelation2> exampleRelations2;

    // COSTRUCTORS --------------------------------------------------------
    /*
     * Entity constructors
     */

    // GETTER AND SETTER --------------------------------------------------
    @Column(name = "exampleName")
    public String getExampleName() {
        return exampleName;
    }

    public void setExampleName(String exampleName) {
        this.exampleName = exampleName;
    }

    /*
     * ...
     */

    @OneToOne(mappedBy = "example_relation_1", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = ExampleRelation1.class)
    @NotFound(action = NotFoundAction.IGNORE)
    public ExampleRelation1 getExampleRelation1() {
        return exampleRelation;
    }

    public void setExampleRelation1(ExampleRelation1 exampleRelation1) {
        this.exampleRelation1 = exampleRelation1;
    }

    @OneToMany(mappedBy = "example", targetEntity = ExampleRelation2.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @NotFound(action = NotFoundAction.IGNORE)
    public Set<ExampleRelation2> getExampleRelation2() {
        return exampleRelation2;
    }

    public void setExampleRelation2(Set<ExampleRelation2> exampleRelation2) {
        this.exampleRelation2 = exampleRelation2;
    }
}
@实体
@表(name=“示例”)
公共类示例扩展了BaseModel{//BaseModel映射为超类,并包含Id列和创建、更新、删除时间戳
私有字符串示例名;
/*
* ...
*/
/* 
*此关系中包含另一个关系
*(例如Set-exampleRelationRelations)
*/
私人示例关系1示例关系1;
私有集示例关系2;
//构造师--------------------------------------------------------
/*
*实体构造函数
*/
//接二连三--------------------------------------------------
@列(name=“exampleName”)
公共字符串getExampleName(){
返回exampleName;
}
public void setExampleName(字符串exampleName){
this.exampleName=exampleName;
}
/*
* ...
*/
@OneTONE(mappedBy=“example\u relation\u 1”,cascade=CascadeType.ALL,fetch=FetchType.LAZY,targetEntity=ExampleRelation1.class)
@NotFound(action=NotFoundAction.IGNORE)
public ExampleRelation1 getExampleRelation1(){
返回示例关系;
}
public void setExampleRelation1(ExampleRelation1 ExampleRelation1){
this.exampleRelation1=exampleRelation1;
}
@OneToMany(mappedBy=“example”,targetEntity=ExampleRelation2.class,fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@NotFound(action=NotFoundAction.IGNORE)
公共集getExampleRelation2(){
返回示例relation2;
}
public void setExampleRelation2(Set exampleRelation2){
this.exampleRelation2=exampleRelation2;
}
}
在我的应用程序中,我可能需要使用
examplelation1
和他的子关系
examplelationrelations
加载
examplelationrelations
,但并不总是这样。也许下次我必须加载没有关系的
示例
,或者只加载
示例关系1
示例关系2

我找到的最佳解决方案是实现,以便用我需要的关系动态地连接表。有了这个解决方案,我可以告诉Struts2控制器检索我需要的数据

这是一个好的解决方案吗?或者我应该使用其他策略

*编辑*


我还发现,这可能是一个解决方案?

获取配置文件可以解决您的问题

但是,您可以在代码上控制这种行为,只要在事务中延迟加载希望每次调用正确方法的实体

我可能需要在示例中加载ExampleRelation1和他的子关系exampleRelationRelations

所以,你可以用这样的方法:

@Transactional // from Spring
public Example getExampleWithExampleRelation1AndExampleRelationRelations(String exampleName) {

    Example example = em.find(Example.class, exampleName);
    ExampleRelation1 exampleRelation1 = example.getExampleRelation1(); //lazy load
    exampleRelation1.exampleRelationRelations().size(); //lazy load list
    return example;
}
也许下次我必须加载没有关系的示例,或者只加载exampleRelation1和exampleRelation2

只需创建另一个方法并调用它:

@Transactional // from Spring
public Example getExampleWithExampleRelation1AndExampleRelation2(String exampleName) {

    Example example = em.find(Example.class, exampleName);
    example.getExampleRelation1(); //lazy load
    example.getExampleRelation2(); //lazy load
    return example;
}
您还可以使用JPQL并与
FETCH
word连接,以仅使用一个查询来获取实体,如:

String jpql = "SELECT e FROM Example e " + 
    "JOIN FETCH e.ExampleRelation1" + 
    "JOIN FETCH e.ExampleRelation2 ";

获取配置文件可以解决您的问题

但是,您可以在代码上控制这种行为,只要在事务中延迟加载希望每次调用正确方法的实体

我可能需要在示例中加载ExampleRelation1和他的子关系exampleRelationRelations

所以,你可以用这样的方法:

@Transactional // from Spring
public Example getExampleWithExampleRelation1AndExampleRelationRelations(String exampleName) {

    Example example = em.find(Example.class, exampleName);
    ExampleRelation1 exampleRelation1 = example.getExampleRelation1(); //lazy load
    exampleRelation1.exampleRelationRelations().size(); //lazy load list
    return example;
}
也许下次我必须加载没有关系的示例,或者只加载exampleRelation1和exampleRelation2

只需创建另一个方法并调用它:

@Transactional // from Spring
public Example getExampleWithExampleRelation1AndExampleRelation2(String exampleName) {

    Example example = em.find(Example.class, exampleName);
    example.getExampleRelation1(); //lazy load
    example.getExampleRelation2(); //lazy load
    return example;
}
您还可以使用JPQL并与
FETCH
word连接,以仅使用一个查询来获取实体,如:

String jpql = "SELECT e FROM Example e " + 
    "JOIN FETCH e.ExampleRelation1" + 
    "JOIN FETCH e.ExampleRelation2 ";

这两个主题具有与延迟加载特性相反的行为。顺便说一句,为什么你要加载超过500k的记录,很难处理它们。我可能用一种不好的方式解释了我自己。。。500k是表的大小,当然对于一个web应用程序来说,在一个视图中显示500k条记录是疯狂的,没有对它们进行分页…如果相关对象具有1:n或n:n关系,也会如此?谢谢您的建议!这两个主题具有与延迟加载特性相反的行为。顺便说一句,为什么你要加载超过500k的记录,很难处理它们。我可能用一种不好的方式解释了我自己。。。500k是表的大小,当然对于一个web应用程序来说,在一个视图中显示500k条记录是疯狂的,没有对它们进行分页…如果相关对象具有1:n或n:n关系,也会如此?谢谢您的建议!