我是hibernate的新手,我写了一个简单的bean:User
。虽然我可以在我的java代码中使用configuration.addResource("User.hbm.xml")
直接配置它,但是当我通过hibernate.cfg.xml
配置它时:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<!-- JDBC connection pool(use the built-in) -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">none</property>
<!-- List of XML mappings -->
<mapping resource="User.hbm.xml"/>
</session-factory>
应用程序未配置User
实体,它运行时出现错误:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.philip.fin.test.User
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at com.philip.fin.test.TestHibernate.main(TestHibernate.java:37)
下面粘贴的User.hbm.xml文件:
<hibernate-mapping>
<class name="com.philip.fin.test.User" table="User_table" catalog="test">
<id name="userId" column="User_Id">
<generator class="increment"/>
</id>
<property name="userName" column="User_Name" type="string"/>
<property name="userMessage" column="User_Message type="string"/>
</class>
</hibernate-mapping>