0

I have a problem that I've been struggling to solve but I'm not getting anywhere.

I'm using JPA Hibernate to create tables, but instead of creating the table, he shows me the error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘escola.tab_alunos’ doesn’t exist

Here's my persistence.xml:

<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="banco" transaction-type="RESOURCE_LOCAL">
    <description>
    Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
    </description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>org.halyph.sessiondemo.Event</class>

    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/escola" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="0000" />

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

And here's the class Aluno:

@Entity
@Table(name=“TAB_ALUNOS”)
@SequenceGenerator(name=“TAB_ALUNOS_PK”, sequenceName=“SEQ_ALUNOS_PK”, allocationSize=1)
public class Aluno {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TAB_ALUNOS_PK")
    private Long id;

    @Column(length=10, nullable=false)
    private String matricula;

    @Column(length=100, nullable=false)
    private String nome;

    @Column(length=9 ,nullable=false)
    private String sexo;

    @Column(name="DATA_NASCIMENTO", length=10, nullable=false)
    private String dataNascimento;

    @Column(length=30, columnDefinition="DEFAULT 'Ativo'")
    private String situacao;

    // ....
 }

Any idea on how can I solve this problem?

Just for the record, the first time I started the project, it worked just fine, but due to a mistake I've made, I had to drop the table, after that hibernate stopped creating the table and now I can't do anything

5 Answers5

0

Modify

<property name="hibernate.hbm2ddl.auto" value="update" />

to

<property name="hibernate.hbm2ddl.auto" value="create" />

The value="update" modifies the existing table while value="create" creates a table, but if the table already exists, it drops the table with the data and creates a table again.

Shubham Chopra
  • 1,678
  • 17
  • 30
0

Change <property name="hibernate.hbm2ddl.auto" value="create" />

Refer here for all possible values of hbm2ddl.auto

Hibernate hbm2ddl.auto possible values and what they do?

Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32
0

The error shows up because your persistance.xml has been configured to update tables. During the first run, there are no tables that have been created, and thus nothing to update.

On the first run, set <property name="hibernate.hbm2ddl.auto" value="create" /> so that a table can be created from your pojo. Then, set it back to <property name="hibernate.hbm2ddl.auto" value="update" /> so that your tables can be updated instead of being created at each run

Fabulous
  • 735
  • 1
  • 10
  • 28
0

Provide schema as well along with table name e.g.

@Table(name=“TAB_ALUNOS”, schema = "dbo")
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-1

I tried changing it to:

<property name="hibernate.hbm2ddl.auto" value="create" />

and i have also tried changing to:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

Neither one of those two worked.

  • The schema 'escola' is created. And the seq_alunos_pk is also created. The only thing that's not being created is the table tab_alunos – Matheus Genteluci May 25 '18 at 12:33
  • Try cleaning and restarting both your DBMS client and your web server, and dropping your schema. Everything looks fine. – Fabulous May 25 '18 at 12:46
  • I dropped the schema, restarted eclipse, restarted mysql service, even deleted the schema directly in the paste "mysql/data", the error remains. – Matheus Genteluci May 25 '18 at 13:27
  • Try changing xsi:schemalocation to "http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> and closing the tag at the end of the document – Fabulous May 25 '18 at 13:37
  • EDIT your QUESTION with this info. This is not an ANSWER to the QUESTION! –  May 25 '18 at 17:03