【发布时间】:2015-08-21 18:27:49
【问题描述】:
我正在关注一个休眠教程。我已经创建了 hibernate.cfg.xml。
本教程的第一个示例不起作用。当调用 configure 方法时,它会抛出 HibernateException 报告配置无效。我在这里留下了代码:
public static void main(String[] args) {
Session session = null;
try {
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()
).buildServiceRegistry();
SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
session.beginTransaction();
System.out.println("Adding a customer record !");
Customer customer = new Customer();
customer.setCustomerName("Customer-a");
customer.setCustomerAddress("Address1");
session.save(customer);
session.getTransaction().commit();
System.out.println("Done!");
} catch (HibernateException e) {
System.out.println("=========>" + e.getMessage());
} finally {
if (session != null) {
session.flush();
session.close();
}
}
}
我的 hibernate.cfg.xml 是这样的:
<hibernate-configuration
xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">pepe</property>
<property name="hibernate.connection.password">pepe</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernate</property>
<property name="connection_pool_size">1</property>
<property name="hbm2ddl.auto">updated</property>
<property name="show_sql">true</property>
<mapping resource="hibernateexample1/domain/costumer.hbm.xml"/>
</session-factory>
【问题讨论】:
-
请输入您引用此示例的教程链接!!!!