Hibernate tries to get version information from unversioned entity


#1

Hi, I have an entity versioned with @Version. This class has some @ManyToOne relations, but the related classes are not versioned.

Therefore, Hibernate tries to get version information from these entities, randomly. In one query tries to get version from entity A, in a subsequent query tries to get version of entity B, and so on.

I’m using Hibernate ORM 5.3.5.final. Any help will be appreciated. Thanks.

07:45:12.044 [http-nio-80-exec-10] TRACE org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl - Starting after statement execution processing [AFTER_TRANSACTION]
**_07:45:12.044 [http-nio-80-exec-10] TRACE org.hibernate.persister.entity.AbstractEntityPersister - Getting version: [br.com.gecom.model.Empresa#1]_**
**_07:45:12.044 [http-nio-80-exec-10] DEBUG org.hibernate.SQL - select id from empresa where id =?_**
**_Hibernate: select id from empresa where id =?_**
07:45:12.045 [http-nio-80-exec-10] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Registering statement [com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@5682f4fe [wrapping: com.mysql.cj.jdbc.ClientPreparedStatement: select id from empresa where id =** NOT SPECIFIED **]]
07:45:12.045 [http-nio-80-exec-10] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [1]
07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Registering result set [com.mchange.v2.c3p0.impl.NewProxyResultSet@4eb6e6f6 [wrapping: com.mysql.cj.jdbc.result.ResultSetImpl@68893072]]
07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Releasing result set [com.mchange.v2.c3p0.impl.NewProxyResultSet@4eb6e6f6 [wrapping: com.mysql.cj.jdbc.result.ResultSetImpl@68893072]]
07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Closing result set [com.mchange.v2.c3p0.impl.NewProxyResultSet@4eb6e6f6 [wrapping: com.mysql.cj.jdbc.result.ResultSetImpl@68893072]]
07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Releasing statement [com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@5682f4fe [wrapping: com.mysql.cj.jdbc.ClientPreparedStatement: select id from empresa where id =1]]
07:45:12.047 [http-nio-80-exec-10] DEBUG org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - HHH000387: ResultSet's statement was not registered
07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Closing prepared statement [com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@5682f4fe [wrapping: com.mysql.cj.jdbc.ClientPreparedStatement: select id from empresa where id =1]]
07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl - Starting after statement execution processing [AFTER_TRANSACTION]
07:45:12.059 [http-nio-80-exec-10] ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException
07:45:12.059 [http-nio-80-exec-10] DEBUG org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl - JDBC transaction marked for rollback-only (exception provided for stack trace)

#2

No, it does not.

I suppose you are talking about these queries:

07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl - Closing prepared statement [com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@5682f4fe [wrapping: com.mysql.cj.jdbc.ClientPreparedStatement: select id from empresa where id =1]]
07:45:12.047 [http-nio-80-exec-10] TRACE org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl - Starting after statement execution processing [AFTER_TRANSACTION]
07:45:12.059 [http-nio-80-exec-10] ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException
07:45:12.059 [http-nio-80-exec-10] DEBUG org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl - JDBC transaction marked for rollback-only (exception provided for stack trace)

Which do not select any version.

You have a NullPointerException that you need to investigate.

07:45:12.059 [http-nio-80-exec-10] ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException


#3

Vlad, Empresa is a @ManyToOne relation on the main enitity. Also, as i said, not always hibernate tries to get version field of the same related entity. See the fragment of log below, where versao is the verision field in the OrdemDeServico and Pessoa entities:

Hibernate: select ordemdeser0_.id as id1_11_0_, pessoa1_.id as id1_18_1_, pessoa2_.id as id1_18_2_, pessoa3_.id as id1_18_3_, prateleira4_.id as id1_20_4_, fabricante5_.id as id1_6_5_, modelospro6_.id as id1_10_6_, modeloprod7_.id as id1_10_7_, tipodeequi8_.id as id1_25_8_, ostipodeat9_.id as id1_15_9_, servicos10_.id as id1_14_10_, unidadedem11_.id as id1_26_11_, servico12_.id as id1_22_12_, produtos13_.id as id1_13_13_, unidadedem14_.id as id1_26_14_, produto15_.id as id1_21_15_, historicos16_.id as id1_9_16_, pessoa18_.id as id1_18_17_, anexos17_.id as id1_12_18_, empresa19_.id as id1_5_19_, config20_.id as id1_1_20_, servico21_.id as id1_22_21_, unidadedem22_.id as id1_26_22_, ordemdeser0_.acessorios_equipamento as acessori2_11_0_, ordemdeser0_.cliente_id as cliente18_11_0_, ordemdeser0_.data_cadastro as data_cad3_11_0_, ordemdeser0_.data_entrada as data_ent4_11_0_, ordemdeser0_.data_entrega as data_ent5_11_0_, ordemdeser0_.data_para_emissao_do_orcamento as data_par6_11_0_, ordemdeser0_.defeito_encontrado as defeito_7_11_0_, ordemdeser0_.descricao_do_servico_executado as descrica8_11_0_, ordemdeser0_.empresa_id as empresa19_11_0_, ordemdeser0_.equipamento as equipame9_11_0_, ordemdeser0_.fabricante_produto_id as fabrica20_11_0_, ordemdeser0_.funcionario_id as funcion21_11_0_, ordemdeser0_.funcionario_que_abriu_os_id as funcion22_11_0_, ordemdeser0_.laudo_tecnico as laudo_t10_11_0_, ordemdeser0_.modelo_produto_id as modelo_23_11_0_, ordemdeser0_.numero_de_serie as numero_11_11_0_, ordemdeser0_.numero_os as numero_12_11_0_, ordemdeser0_.observacoes_do_cliente as observa13_11_0_, ordemdeser0_.prateleira_id as pratele24_11_0_, ordemdeser0_.problema_relatado as problem14_11_0_, ordemdeser0_.status as status15_11_0_, ordemdeser0_.os_tipo_de_atendimento_id as os_tipo25_11_0_, ordemdeser0_.tipo_de_equipamento_id as tipo_de26_11_0_, ordemdeser0_.urgencia as urgenci16_11_0_, ordemdeser0_.versao as versao17_11_0_, pessoa1_.categoria_cliente as categori2_18_1_, pessoa1_.cliente as cliente3_18_1_, pessoa1_.cnpj as cnpj4_18_1_, pessoa1_.codigo_cliente as codigo_c5_18_1_, pessoa1_.contato as contato6_18_1_, pessoa1_.cpf as cpf7_18_1_, pessoa1_.data_cadastro as data_cad8_18_1_, pessoa1_.data_nascimento as data_nas9_18_1_, pessoa1_.email1 as email10_18_1_, pessoa1_.email2 as email11_18_1_, pessoa1_.email3 as email12_18_1_, pessoa1_.bairro as bairro13_18_1_, pessoa1_.cep as cep14_18_1_, pessoa1_.cidade as cidade15_18_1_, pessoa1_.complemento as complem16_18_1_, pessoa1_.gia as gia17_18_1_, pessoa1_.ibge as ibge18_18_1_, pessoa1_.logradouro as logrado19_18_1_, pessoa1_.numero as numero20_18_1_, pessoa1_.uf as uf21_18_1_, pessoa1_.fornecedor as fornece22_18_1_, pessoa1_.funcionario as funcion23_18_1_, pessoa1_.ie as ie24_18_1_, pessoa1_.motivo_bloqueio as motivo_25_18_1_, pessoa1_.natureza as naturez26_18_1_, pessoa1_.nome_fantasia as nome_fa27_18_1_, pessoa1_.nome_razaosocial as nome_ra28_18_1_, pessoa1_.observacoes as observa29_18_1_, pessoa1_.rg as rg30_18_1_, pessoa1_.sexo as sexo31_18_1_, pessoa1_.status as status32_18_1_, pessoa1_.telefone1 as telefon33_18_1_, pessoa1_.telefone2 as telefon34_18_1_, pessoa1_.telefone3 as telefon35_18_1_, pessoa1_.versao as versao36_18_1_, pessoa2_.categoria_cliente as categori2_18_2_, pessoa2_.cliente as cliente3_18_2_, pessoa2_.cnpj as cnpj4_18_2_, pessoa2_.codigo_cliente as codigo_c5_18_2_, pessoa2_.contato as contato6_18_2_, pessoa2_.cpf as cpf7_18_2_, pessoa2_.data_cadastro as data_cad8_18_2_, pessoa2_.data_nascimento as data_nas9_18_2_, pessoa2_.email1 as email10_18_2_, pessoa2_.email2 as email11_18_2_, pessoa2_.email3 as email12_18_2_, pessoa2_.bairro as bairro13_18_2_, pessoa2_.cep as cep14_18_2_, pessoa2_.cidade as cidade15_18_2_, pessoa2_.complemento as complem16_18_2_, pessoa2_.gia as gia17_18_2_, pessoa2_.ibge as ibge18_18_2_, pessoa2_.logradouro as logrado19_18_2_, pessoa2_.numero as numero20_18_2_, pessoa2_.uf as uf21_18_2_, pessoa2_.fornecedor as fornece22_18_2_, pessoa2_.funcionario as funcion23_18_2_, pessoa2_.ie as ie24_18_2_, pessoa2_.motivo_bloqueio as motivo_25_18_2_, pessoa2_.natureza as naturez26_18_2_, pessoa2_.nome_fantasia as nome_fa27_18_2_, pessoa2_.nome_razaosocial as nome_ra28_18_2_, pessoa2_.observacoes as observa29_18_2_, pessoa2_.rg as rg30_18_2_, pessoa2_.sexo as sexo31_18_2_, pessoa2_.status as status32_18_2_, pessoa2_.telefone1 as telefon33_18_2_, pessoa2_.telefone2 as telefon34_18_2_, pessoa2_.telefone3 as telefon35_18_2_, pessoa2_.versao as versao36_18_2_, pessoa3_.categoria_cliente as categori2_18_3_, pessoa3_.cliente as cliente3_18_3_, pessoa3_.cnpj as cnpj4_18_3_, pessoa3_.codigo_cliente as codigo_c5_18_3_, pessoa3_.contato as contato6_18_3_, pessoa3_.cpf as cpf7_18_3_, pessoa3_.data_cadastro as data_cad8_18_3_, pessoa3_.data_nascimento as data_nas9_18_3_, pessoa3_.email1 as email10_18_3_, pessoa3_.email2 as email11_18_3_, pessoa3_.email3 as email12_18_3_, pessoa3_.bairro as bairro13_18_3_, pessoa3_.cep as cep14_18_3_, pessoa3_.cidade as cidade15_18_3_, pessoa3_.complemento as complem16_18_3_, pessoa3_.gia as gia17_18_3_, pessoa3_.ibge as ibge18_18_3_, pessoa3_.logradouro as logrado19_18_3_, pessoa3_.numero as numero20_18_3_, pessoa3_.uf as uf21_18_3_, pessoa3_.fornecedor as fornece22_18_3_, pessoa3_.funcionario as funcion23_18_3_, pessoa3_.ie as ie24_18_3_, pessoa3_.motivo_bloqueio as motivo_25_18_3_, pessoa3_.natureza as naturez26_18_3_, pessoa3_.nome_fantasia as nome_fa27_18_3_, pessoa3_.nome_razaosocial as nome_ra28_18_3_, pessoa3_.observacoes as observa29_18_3_, pessoa3_.rg as rg30_18_3_, pessoa3_.sexo as sexo31_18_3_, pessoa3_.status as status32_18_3_, pessoa3_.telefone1 as telefon33_18_3_, pessoa3_.telefone2 as telefon34_18_3_, pessoa3_.telefone3 as telefon35_18_3_, pessoa3_.versao as versao36_18_3_, prateleira4_.descricao as descrica2_20_4_, fabricante5_.descricao as descrica2_6_5_, modelospro6_.descricao as descrica2_10_6_, modelospro6_.fabricante_produto_id as fabrican3_10_6_, modelospro6_.fabricante_produto_id as fabrican3_10_0__, modelospro6_.id as id1_10_0__, modeloprod7_.descricao as descrica2_10_7_, modeloprod7_.fabricante_produto_id as fabrican3_10_7_, tipodeequi8_.descricao as descrica2_25_8_, ostipodeat9_.cor_do_texto as cor_do_t2_15_9_, ostipodeat9_.descricao as descrica3_15_9_, servicos10_.desconto as desconto2_14_10_, servicos10_.descricao as descrica3_14_10_, servicos10_.ordem_de_servico_id as ordem_de8_14_10_, servicos10_.quantidade as quantida4_14_10_, servicos10_.servico_id as servico_9_14_10_, servicos10_.taxa_de_urgencia as taxa_de_5_14_10_, servicos10_.tipo_desconto as tipo_des6_14_10_, servicos10_.unidade_de_medida_id as unidade10_14_10_, servicos10_.valor as valor7_14_10_, servicos10_.ordem_de_servico_id as ordem_de8_14_1__, servicos10_.id as id1_14_1__, unidadedem11_.descricao as descrica2_26_11_, unidadedem11_.sigla as sigla3_26_11_, servico12_.ativo as ativo2_22_12_, servico12_.descricao as descrica3_22_12_, servico12_.unidade_de_medida_id as unidade_5_22_12_, servico12_.valor as valor4_22_12_, produtos13_.desconto as desconto2_13_13_, produtos13_.descricao as descrica3_13_13_, produtos13_.ordem_de_servico_id as ordem_de7_13_13_, produtos13_.produto_id as produto_8_13_13_, produtos13_.quantidade as quantida4_13_13_, produtos13_.tipo_desconto as tipo_des5_13_13_, produtos13_.unidade_de_medida_id as unidade_9_13_13_, produtos13_.valor as valor6_13_13_, produtos13_.ordem_de_servico_id as ordem_de7_13_2__, produtos13_.id as id1_13_2__, unidadedem14_.descricao as descrica2_26_14_, unidadedem14_.sigla as sigla3_26_14_, produto15_.controla_estoque as controla2_21_15_, produto15_.descricao as descrica3_21_15_, produto15_.estoque_minimo as estoque_4_21_15_, produto15_.grupo_de_produto_id as grupo_de9_21_15_, produto15_.margem_de_lucro as margem_d5_21_15_, produto15_.preco_de_compra as preco_de6_21_15_, produto15_.preco_de_venda as preco_de7_21_15_, produto15_.status_produto as status_p8_21_15_, produto15_.unidade_de_medida_id as unidade10_21_15_, historicos16_.data as data2_9_16_, historicos16_.descricao as descrica3_9_16_, historicos16_.ordem_de_servico_id as ordem_de4_9_16_, historicos16_.pessoa_id as pessoa_i5_9_16_, historicos16_.ordem_de_servico_id as ordem_de4_9_3__, historicos16_.id as id1_9_3__, pessoa18_.categoria_cliente as categori2_18_17_, pessoa18_.cliente as cliente3_18_17_, pessoa18_.cnpj as cnpj4_18_17_, pessoa18_.codigo_cliente as codigo_c5_18_17_, pessoa18_.contato as contato6_18_17_, pessoa18_.cpf as cpf7_18_17_, pessoa18_.data_cadastro as data_cad8_18_17_, pessoa18_.data_nascimento as data_nas9_18_17_, pessoa18_.email1 as email10_18_17_, pessoa18_.email2 as email11_18_17_, pessoa18_.email3 as email12_18_17_, pessoa18_.bairro as bairro13_18_17_, pessoa18_.cep as cep14_18_17_, pessoa18_.cidade as cidade15_18_17_, pessoa18_.complemento as complem16_18_17_, pessoa18_.gia as gia17_18_17_, pessoa18_.ibge as ibge18_18_17_, pessoa18_.logradouro as logrado19_18_17_, pessoa18_.numero as numero20_18_17_, pessoa18_.uf as uf21_18_17_, pessoa18_.fornecedor as fornece22_18_17_, pessoa18_.funcionario as funcion23_18_17_, pessoa18_.ie as ie24_18_17_, pessoa18_.motivo_bloqueio as motivo_25_18_17_, pessoa18_.natureza as naturez26_18_17_, pessoa18_.nome_fantasia as nome_fa27_18_17_, pessoa18_.nome_razaosocial as nome_ra28_18_17_, pessoa18_.observacoes as observa29_18_17_, pessoa18_.rg as rg30_18_17_, pessoa18_.sexo as sexo31_18_17_, pessoa18_.status as status32_18_17_, pessoa18_.telefone1 as telefon33_18_17_, pessoa18_.telefone2 as telefon34_18_17_, pessoa18_.telefone3 as telefon35_18_17_, pessoa18_.versao as versao36_18_17_, anexos17_.arquivo as arquivo2_12_18_, anexos17_.mime_type as mime_typ3_12_18_, anexos17_.nome as nome4_12_18_, anexos17_.ordem_de_servico_id as ordem_de5_12_18_, anexos17_.ordem_de_servico_id as ordem_de5_12_4__, anexos17_.id as id1_12_4__, empresa19_.cnae as cnae2_5_19_, empresa19_.cnpj as cnpj3_5_19_, empresa19_.email as email4_5_19_, empresa19_.bairro as bairro5_5_19_, empresa19_.cep as cep6_5_19_, empresa19_.cidade as cidade7_5_19_, empresa19_.complemento as compleme8_5_19_, empresa19_.gia as gia9_5_19_, empresa19_.ibge as ibge10_5_19_, empresa19_.logradouro as logrado11_5_19_, empresa19_.numero as numero12_5_19_, empresa19_.uf as uf13_5_19_, empresa19_.ie as ie14_5_19_, empresa19_.nome_fantasia as nome_fa15_5_19_, empresa19_.razao_social as razao_s16_5_19_, empresa19_.telefone_comercial as telefon17_5_19_, config20_.dias_orcamento_atencao as dias_orc2_1_20_, config20_.dias_orcamento_atrasado as dias_orc3_1_20_, config20_.dias_orcamento_muito_atrasado as dias_orc4_1_20_, config20_.email_endereco as email_en5_1_20_, config20_.email_smtp as email_sm6_1_20_, config20_.email_senha as email_se7_1_20_, config20_.email_tipo_transporte as email_ti8_1_20_, config20_.empresa_id as empresa10_1_20_, config20_.email_porta as email_po9_1_20_, config20_.servico_associado_a_ordem_servico_de_urgencia_id as servico11_1_20_, servico21_.ativo as ativo2_22_21_, servico21_.descricao as descrica3_22_21_, servico21_.unidade_de_medida_id as unidade_5_22_21_, servico21_.valor as valor4_22_21_, unidadedem22_.descricao as descrica2_26_22_, unidadedem22_.sigla as sigla3_26_22_ from ordem_de_servico ordemdeser0_ inner join pessoas pessoa1_ on ordemdeser0_.cliente_id=pessoa1_.id left outer join pessoas pessoa2_ on ordemdeser0_.funcionario_id=pessoa2_.id inner join pessoas pessoa3_ on ordemdeser0_.funcionario_que_abriu_os_id=pessoa3_.id left outer join prateleiras prateleira4_ on ordemdeser0_.prateleira_id=prateleira4_.id left outer join fabricante_produto fabricante5_ on ordemdeser0_.fabricante_produto_id=fabricante5_.id left outer join modelo_produto modelospro6_ on fabricante5_.id=modelospro6_.fabricante_produto_id left outer join modelo_produto modeloprod7_ on ordemdeser0_.modelo_produto_id=modeloprod7_.id left outer join tipo_de_equipamento tipodeequi8_ on ordemdeser0_.tipo_de_equipamento_id=tipodeequi8_.id left outer join os_tipos_de_atendimentos ostipodeat9_ on ordemdeser0_.os_tipo_de_atendimento_id=ostipodeat9_.id left outer join os_servicos servicos10_ on ordemdeser0_.id=servicos10_.ordem_de_servico_id left outer join unidades_de_medidas unidadedem11_ on servicos10_.unidade_de_medida_id=unidadedem11_.id left outer join servicos servico12_ on servicos10_.servico_id=servico12_.id left outer join os_produtos produtos13_ on ordemdeser0_.id=produtos13_.ordem_de_servico_id left outer join unidades_de_medidas unidadedem14_ on produtos13_.unidade_de_medida_id=unidadedem14_.id left outer join produtos produto15_ on produtos13_.produto_id=produto15_.id left outer join historico historicos16_ on ordemdeser0_.id=historicos16_.ordem_de_servico_id left outer join pessoas pessoa18_ on historicos16_.pessoa_id=pessoa18_.id left outer join os_anexos anexos17_ on ordemdeser0_.id=anexos17_.ordem_de_servico_id left outer join empresa empresa19_ on ordemdeser0_.empresa_id=empresa19_.id left outer join config config20_ on empresa19_.id=config20_.empresa_id left outer join servicos servico21_ on config20_.servico_associado_a_ordem_servico_de_urgencia_id=servico21_.id left outer join unidades_de_medidas unidadedem22_ on servico21_.unidade_de_medida_id=unidadedem22_.id where ordemdeser0_.id=?
_**Hibernate: select versao from ordem_de_servico where id =?**_
_**Hibernate: select versao from pessoas where id =?**_
_**Hibernate: select versao from pessoas where id =?**_
_**Hibernate: select id from os_tipos_de_atendimentos where id =? --> There is not a field called id annotated with @Version **_
09:37:39.084 [http-nio-80-exec-7] ERROR org.hibernate.AssertionFailure - HHH000099: an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException

#4

The default optimistic locking mechanism does not select the version like this:

select versao from pessoas where id =?**_

The only way Hibernate will do that is if you are using LockModeType.OPTIMISTIC.

Just send us a replicating test case to prove that Hibernate issues those select queries unless you are issueing an explicit optimistic locking request.


#5

Ok Vlad, thanks for reply.