Java/Hibernate初学者问题理解下面的代码
我正在努力理解上面的代码。我是Java的初学者,很难理解下面这一行。这是否意味着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
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();
String example = new StringBuilder(“hello”)
.append(“ “)
.append(“world”)
.toString();