照hibernate官网的例子写了一下,
其中setProperty时是按xml时写的那种缩写比如hibernate.dialect 写成dialect
hibernate.connection.username写成connection.username
这样会提示
(0 ms) [main] WARN : org.hibernate.connection.UserSuppliedConnectionProvider
#configure : No connection properties specified - the user must supply JDBC connections
正确的是按hibernate手册里把属性名写全了就没事了
package configuration;
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) {
Configuration cfg = new Configuration()
.addClass(hibernate.DocumentType.class)
.addClass(hibernate.Person.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect")
.setProperty("current_session_context_class", "thread")
.setProperty("hibernate.connection.url", "jdbc:oracle:thin:localhost:1521:orcl")
.setProperty("hibernate.connection.username", "sj")
.setProperty("hibernate.connection.password", "123456")
.setProperty("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver")
.setProperty("show_sql", "true")
.setProperty("hbm2ddl.auto", "update")
.setProperty("hibernate.connection.pool_size", "1")
.setProperty("hbm2ddl.auto", "update")
.setProperty("cache.provider_class", "org.hibernate.cache.NoCacheProvider");
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
session.beginTransaction();
Person person = (Person)session.load(Person.class, new Long(1));
person.getEmails().add("csdn@sina.com");
session.getTransaction().commit();
session.close();
System.out.println("结束");
}
}