DEV Community

Bruno Barros
Bruno Barros

Posted on

2

Diferença entre os GenerationTypes do Hibernate

Questão de entrevista de emprego!

Image description

IDENTITY: Talvez o mais usado, quando utilizamos IDENTITY o Hibernate utilizará a geração AUTO_INCREMENT para SQL/MySQL, já para o Postgres faz uso do tipo SERIAL.

SEQUENCE: Se permitido pelo banco, utiliza sequencias (sequences) para gerar a chave primária, do contrário, automaticamente será utilizado o padrão TABLE. (https://lnkd.in/d8x5_3bx) - podemos customizar esse generation com nome e valores da sequencia. Atualmente o generation type recomendado pelo Hibernate.

Podemos definir o gerador das sequências através da anotação @SequenceGenerator

AUTO: Default. Se nenhum valor for especificado para o GenerationType, este será utilizado. A implementação definirá que estratégia utilizar. No caso do Hibernate com JPA, seria a SEQUENCE para Postgres e oracle e TABLE para MySQL.

TABLE: Funciona em todos os bancos relacionais. Porém, quando fazemos uso dela o Hibernate precisa necessariamente uma outra tabela para gerar e guardar as chaves primárias. Por motivos de locks e lentidão ao escalar a aplicação, é bom evitar o uso de TABLE.

Mais detalhes e fontes:

https://lnkd.in/dXm6RV3i

https://lnkd.in/dAnHUnMW

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️