首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hibernate :持久性元素“persistence.xml -unit”不能包含文本内容时出错。内容类型被定义为仅限元素

问Hibernate :持久性元素“persistence.xml -unit”不能包含文本内容时出错。内容类型被定义为仅限元素
EN

Stack Overflow用户
提问于 2020-05-23 23:09:44
回答 1查看 266关注 0票数 1

我正在使用Hibernate,下面的persistence.xml运行得很好。但是在eclipse的表格中创建了一个新的JPA实体后,persistence.xml给出了错误--元素'persistence-unit‘不能包含文本内容。内容类型被定义为仅限元素。我已经从persistence.xml中删除了新类,但错误仍然存在。

代码语言:javascript
复制
<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&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;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>```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-23 23:18:39

Hibernate支持自动检测,所以只需删除类元素并添加此属性,同时使用@Entity注解注解您的类。

代码语言:javascript
复制
<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&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61974131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档