Do I need to include classes mapped in persistence.xml? Java-Eclipse-Maven-Hibernate-H2database

Asked

Viewed 533 times

0

I’m trying to set up a Maven project in Eclipse with Hibernate and H2 Database. So I’m trying to understand the persistence.xml settings, which I had to create.

One question is: do I need to include the classes I’m going to map in this persistence.xml? I took an example from the internet that has:

<persistence 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_2_0.xsd"
    version="2.0">

 <persistence-unit name="tarefas">

   <!-- provedor/implementacao do JPA -->
   <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

   <!-- entidade mapeada -->
   <class>br.com.caelum.tarefas.modelo.Tarefa</class>

   <properties>
    <!-- dados da conexao -->
     <property name="javax.persistence.jdbc.driver" 
             value="com.mysql.jdbc.Driver" />
    <property name="javax.persistence.jdbc.url" 
            value="jdbc:mysql://localhost/fj21" />
    <property name="javax.persistence.jdbc.user" 
            value="root" />
    <property name="javax.persistence.jdbc.password" 
            value="<SENHA DO BANCO AQUI>" />

     <!--  propriedades do hibernate -->
     <property name="hibernate.dialect" 
            value="org.hibernate.dialect.MySQL5InnoDBDialect" />
    <property name="hibernate.show_sql" value="true" />
    <property name="hibernate.format_sql" value="true" />

    <!--  atualiza o banco, gera as tabelas se for preciso -->
    <property name="hibernate.hbm2ddl.auto" value="update" />

   </properties>
 </persistence-unit>
</persistence>

It has an attribute/tag "class" that indicates the Java class to be mapped. I have to do this with all classes in my project?:

2- Another question is: if the above answer is yes, how does it work to indicate the relative path of the class relative to the location of my persistence.xml?

For example: see where . class and persistence.xmlareare in my project. How I point out the location of . class in persistence attributes?

inserir a descrição da imagem aqui

1 answer

1


You can map entities in two ways:

1 with the tag class, as you mentioned:

<class>caminho.inteiro.do.pacote.NomeDaEntidade</class>

2 with the annotation @Entity:

@Entity public class NomeDaEntidade { ... }

Answering the second question, how to inform the FQN - Full Qualified Name for the tag class, Hibernate knows exactly where the class is.

Browser other questions tagged

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