教程中的 Hibernate 示例不起作用
Hibernate example from a tutorial don't work
我正在关注休眠教程。我创建了 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>
您可能需要在休眠配置之上添加休眠 DOCTYPE,即
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
还要检查您的 xml 格式是否正确,寻找任何未关闭的标签等。因为在配置文件中看不到 'hibernate-configuration' 标签的关闭。
我正在关注休眠教程。我创建了 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>
您可能需要在休眠配置之上添加休眠 DOCTYPE,即
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
还要检查您的 xml 格式是否正确,寻找任何未关闭的标签等。因为在配置文件中看不到 'hibernate-configuration' 标签的关闭。