Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Minha tabela criada ficou com alguns atributos diferentes (type e null)

-> Type

  • No resultado apresentado em aula, o id, agencia e conta ficaram como "bigint(20)".
  • Para mim, apenas o id ficou como bigint(20) (na classe está como Long), os outros dois atributos (Integer na classe), ficaram como int(11).

-> Null

  • No resultado apresentado na aula, apenas o titular ficou com "null" igual a YES.
  • Pra mim, agencia, conta e titular ficaram como "YES".

Por que essa diferença? Faltou alguma configuração? Segue como está minha classe Conta:

@Entity
public class Conta {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Long id;
    private Integer agencia;
    private Integer conta;
    private String titular;

E o 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="contas">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>br.com.alura.jpa.modelo.Conta</class>

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

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            <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>
1 resposta
solução!

Olá, Andressa (Não sou do suporte da Alura)

Então sobre o mapeamento da sua tabela, está correto ao meu ver, porque o Long no Hibernate "converte" para "bigInt(20)" por padrão, enquanto o Int ele "converte" para "int(11)".

Referente a coluna como nulla, não sei o que pode ter ocorrido para ocorrer isso, recomendo tentar criar novamente está classe, mas caso não funcione, existe dois tipos de anotações que podem ajudar a resolver esse problema que são a @Column(nullable = false) e a anotação @NotNull. Você poderá mapear sua classe da seguinte maneira:

package br.com.alura.jpa.modelo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import com.sun.istack.NotNull;

@Entity
public class Conta {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String titular;
    @NotNull
    private Integer agencia;
    @NotNull
    private Integer numero;
    @NotNull
    private Double saldo;

    //Getter e Setters ocultados

}

Ou deixar usando o @Column(nullable=false):

package br.com.alura.jpa.modelo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Conta {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable=false)
    private Long id;
    private String titular;
    @Column(nullable=false)
    private Integer agencia;
    @Column(nullable=false)
    private Integer numero;
    @Column(nullable=false)
    private Double saldo;

    //Getter e Setters ocultados

}

Se consegui te ajudar, por favor, marque essa pergunta como resolvida ^^ !

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software