休眠:persistence.xml 中的错误 - 元素 'persistence-unit' 不能包含文本内容。内容类型定义为仅元素
Hibernate : Error in persistence.xml - Element 'persistence-unit' cannot contain text content. The content type is defined as element-only
我正在使用 Hibernate,下面的 persistence.xml 工作正常。但是在 Eclipse 中从 Table 创建新的 JPA 实体后,persistence.xml 给出错误 - 元素 'persistence-unit' 不能包含文本内容。内容类型定义为仅元素。我已经从 persistence.xml 中删除了新的 class,但错误仍然存在。
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="FirstJPAProject">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.boi.network.test.entity.Location</class>
<class>com.boi.network.test.entity.Supportvendor</class>
<class>com.boi.network.test.entity.Project</class>
<properties>
<!-- The JDBC URL to the database instance -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/boi?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property>
<property name="javax.persistence.jdbc.user" value="dba"></property>
<property name="javax.persistence.jdbc.password" value="12345"></property>
</properties>
</persistence-unit>
</persistence>```
Hibernate 支持自动检测,因此只需删除您的 class 元素并添加此 属性,同时使用 @Entity 注释对您的 classes 进行注释。
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="FirstJPAProject">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--<class>com.boi.network.test.entity.Location</class>
<class>com.boi.network.test.entity.Supportvendor</class>
<class>com.boi.network.test.entity.Project</class>-->
<properties>
<!-- The JDBC URL to the database instance -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/boi?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property>
<property name="javax.persistence.jdbc.user" value="dba"></property>
<property name="javax.persistence.jdbc.password" value="12345"></property>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
</properties>
</persistence-unit>
</persistence>```
看看这个answer
我正在使用 Hibernate,下面的 persistence.xml 工作正常。但是在 Eclipse 中从 Table 创建新的 JPA 实体后,persistence.xml 给出错误 - 元素 'persistence-unit' 不能包含文本内容。内容类型定义为仅元素。我已经从 persistence.xml 中删除了新的 class,但错误仍然存在。
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="FirstJPAProject">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.boi.network.test.entity.Location</class>
<class>com.boi.network.test.entity.Supportvendor</class>
<class>com.boi.network.test.entity.Project</class>
<properties>
<!-- The JDBC URL to the database instance -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/boi?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property>
<property name="javax.persistence.jdbc.user" value="dba"></property>
<property name="javax.persistence.jdbc.password" value="12345"></property>
</properties>
</persistence-unit>
</persistence>```
Hibernate 支持自动检测,因此只需删除您的 class 元素并添加此 属性,同时使用 @Entity 注释对您的 classes 进行注释。
<persistence version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="FirstJPAProject">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!--<class>com.boi.network.test.entity.Location</class>
<class>com.boi.network.test.entity.Supportvendor</class>
<class>com.boi.network.test.entity.Project</class>-->
<properties>
<!-- The JDBC URL to the database instance -->
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/boi?useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"></property>
<property name="javax.persistence.jdbc.user" value="dba"></property>
<property name="javax.persistence.jdbc.password" value="12345"></property>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
</properties>
</persistence-unit>
</persistence>```
看看这个answer