根目录下的hibernate.properties
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
current_session_context_class=thread
hibernate.connection.url=jdbc:oracle:thin:localhost:1521:orcl
hibernate.connection.username=sj
hibernate.connection.password=123456
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
show_sql=true
hbm2ddl.auto=update
hibernate.connection.pool_size=1
cache.provider_class=org.hibernate.cache.NoCacheProvider
测试的Java类
package configuration;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import hibernate.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ConfigurationTest {
public static void main(String[] args) {
try {
Properties properties = new Properties();
File file = new File("E:/Workspace/hhh/src/hibernate.properties");
FileInputStream stream = new FileInputStream(file);
properties.load(stream);
Configuration cfg = new Configuration()
.addClass(hibernate.DocumentType.class)
.addClass(hibernate.Person.class)
.setProperties(properties);
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
session.beginTransaction();
Person person = (Person)session.load(Person.class, new Long(1));
person.getEmails().add("wow@sina.com");
session.getTransaction().commit();
session.close();
System.out.println("结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}