Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 是否只保留实体id而不保留集合<;实体>;用于少连接操作_Mysql_Database_Hibernate_Entity Relationship_Hibernate Onetomany - Fatal编程技术网

Mysql 是否只保留实体id而不保留集合<;实体>;用于少连接操作

Mysql 是否只保留实体id而不保留集合<;实体>;用于少连接操作,mysql,database,hibernate,entity-relationship,hibernate-onetomany,Mysql,Database,Hibernate,Entity Relationship,Hibernate Onetomany,我正在尝试实现一个应用程序。在这个应用程序中,我有一个实体名注释、答案和问题。提问、回答和评论都有评论,所以我计划创建一个可评论类,所有这三个类都可以扩展可评论类。在这个可注释的类中,它保持不变,但我想知道这个结构的性能。因为它总是创建一个连接查询来获取注释。如果我创建一个具有commentedObjectId的单独注释实体,并且在需要注释时对单独的表进行查询,比如where commentedObjectId=id,该怎么办 class Answer extends Commentab

我正在尝试实现一个应用程序。在这个应用程序中,我有一个实体名注释、答案和问题。提问、回答和评论都有评论,所以我计划创建一个可评论类,所有这三个类都可以扩展可评论类。在这个可注释的类中,它保持不变,但我想知道这个结构的性能。因为它总是创建一个连接查询来获取注释。如果我创建一个具有commentedObjectId的单独注释实体,并且在需要注释时对单独的表进行查询,比如where commentedObjectId=id,该怎么办

    class Answer extends Commentable{}
    class Comment extends Commentable{}
    class Question extends Commentable{}

    class Commentable extends BaseModel{

    @OneToMany
    Set<Comment> comments;

    public Set<Comments> getComments() {
        return comments;
    }

    public void setComments(Set<Comment> comments) {
        this.comments = comments;
    }`

您肯定应该使用域对象。Hibernate允许指定要加入或延迟加载的内容

要仅加载注释而不加入注释,可以使用以下查询:

Criteria cirt = session.createCriteria(Commentable.class);
crit.setFetchMode("comments", FetchMode.LAZY);

@SuppressWarnings("unchecked")
List<Commentable> result = crit.list();
Criteria cirt=session.createCriteria(Commentable.class);
crit.setFetchMode(“comments”,FetchMode.LAZY);
@抑制警告(“未选中”)
列表结果=crit.List();

感谢您的快速回复,但在获取评论时,它将再次进行加入查询。如果有一天我有数百万的问题和数百万的评论。这个连接查询是如何执行的?它仍然会生成连接sql,重点是;我最好对性能进行非规范化?
Criteria cirt = session.createCriteria(Commentable.class);
crit.setFetchMode("comments", FetchMode.LAZY);

@SuppressWarnings("unchecked")
List<Commentable> result = crit.list();