Java/Hibernate初学者问题理解下面的代码

Java/Hibernate初学者问题理解下面的代码,java,hibernate,Java,Hibernate,我正在努力理解上面的代码。我是Java的初学者,很难理解下面这一行。这是否意味着Configuration对象有一个configure方法,Configuration方法有buildSessionFactory方法 package com.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private sta

我正在努力理解上面的代码。我是Java的初学者,很难理解下面这一行。这是否意味着
Configuration
对象有一个configure方法,Configuration方法有
buildSessionFactory
方法

package com.util;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception to track it
            System.err.println("SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Optional but can be used to Close caches and connection pools
        getSessionFactory().close();
    }

}

方法没有方法,方法返回对象,而那些对象有方法

这里,Configuration有一组返回confoguration的方法(因此该方法返回它所调用的对象)。这允许方法链接,以便在该对象上调用configure,然后在同一对象上调用buildSessionFactory

一个更常见的方法链接示例是java.lang.StringBuilder类。您可以通过对同一构建器对象的连续追加调用来构建字符串:

return new Configuration().configure().buildSessionFactory();
  • 配置对象有一个configure方法,该方法将hibernate.cfg.xml中指定的所有配置作为配置对象返回。此信息用于连接到数据库
  • 然后,我们从配置对象获取SessionFactory对象,该对象将用于创建连接到数据库的会话对象
  • 配置=空;SessionFactory=null

    配置=新 Configuration().configure(“com/app/cfgs/hibernate.cfg.xml”); factory=cfg.buildSessionFactory()

    String example = new StringBuilder(“hello”)
        .append(“ “)
        .append(“world”)
        .toString();