How does the <class> tag work in the persistence.xml file in jpa?

Asked

Viewed 22 times

0

Good night. I’m starting in jpa studies, and in a moment I came across the following situation, I filled in the persistence.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.1"
        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_1.xsd">
        <persistence-unit name="exercicios-jpa">
                <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
                <class>modelo.basico.Usuario</class>
                <class>modelo.basico.Estado</class>
                <class>modelo.basico.Cidade</class>
                <properties>
                        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
                        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/curso_java"/>
                        <property name="javax.persistence.jdbc.user" value="root"/>
                        <property name="javax.persistence.jdbc.password" value="12345678"/>

                        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57Dialect"/>
                        <property name="hibernate.show_sql" value="true"/>
                        <property name="hibernate.format_sql" value="true"/>
                        <property name="hibernate.hbm2ddl.auto" value="update"/>
                </properties>
        </persistence-unit>
    </persistence>

It worked perfectly, but on another occasion I filled in the same file only I forgot to add the tag class , and even then it worked normally, I believe this occurs because only the Annotation @Entity is enough to inform the class, I was left with doubt as to the actual use of the tag, since apparently it is not mandatory. I did research, but I only found subjective answers, and I think it serves cases where there is more than one persistence unit in the file, to identify which unit each class belongs to, but I would like the right answer so that I do not create a wrong concept. Thanks in advance.

  • The answer in English: https://stackoverflow.com/q/1780341/2241463

  • Thank you for your reply. I’ve seen this publication, but it didn’t completely clear up my doubts, for example, not informing the tag and the classes can generate me some problem?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.