且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在运行简单的hibernate应用程序时摆脱'java.lang.IllegalArgumentException:未知实体'?

更新时间:2023-01-24 11:23:03

取决于项目结构,但可能通过添加以下内容persistence.xml直接在persistence-unit元素下。

Depends bit about project structure, but likely by adding following to persistence.xml directly under persistence-unit element.

<class>model.Students</class>
<class>model.Address</class>

正好如下:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="OneToOnePU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>students</jta-data-source>
    <class>model.Students</class>
    <class>model.Address</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="create-tables"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
      <property name="hibernate.connection.username" value="app"/>
      <property name="hibernate.connection.password" value="app"/>
      <property name="hibernate.connection.url" value="jdbc:derby://localhost:1527/StudentsData"/>
    </properties>
  </persistence-unit>
</persistence>

顺便说一句,为什么要配置 hibernate.dialect 等属性persistence.xml和hibernate.cfg.xml?

By the way, why do you configure properties like hibernate.dialect in both persistence.xml and hibernate.cfg.xml?