Hibernate! ESTOU DESESPERADO AUDEM-ME!

5 respostas
R

Ola a todos vou começar aqui meu dilema.

tenho um mapeamento:

<?xml version="1.0"?>
&lt;!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" &gt;

&lt;hibernate-mapping package="basecard.adm"&gt;
	&lt;class name="Veiculo" table="VEIC"&gt;
		&lt;id
			column="RENAVAM"
			name="renavam"
			type="java.lang.Integer" 
		&gt;
			&lt;generator class="increment"  /&gt;
		&lt;/id&gt;
		&lt;property
			column="ANO_FABRIC"
			length="4"
			name="anoFabricacao"
			not-null="false"
			type="java.lang.Integer" 
		 /&gt;

		&lt;property
			column="PLACA"
			length="8"
			name="placa"
			not-null="true"
			type="java.lang.String"
		 /&gt;
		&lt;property
			column="ANO_MOD"
			length="4"
			name="anoModelo"
			not-null="false"
			type="java.lang.Integer" 
		 /&gt;
		&lt;property
			column="KM_INI"
			length="6"
			name="quilometragemInicial"
			not-null="true"
			type="java.lang.Integer" 
		 /&gt;

		&lt;property
			column="CHASSI"
			length="20"
			name="chassi"
			not-null="false"
			type="java.lang.String"
		 /&gt;

		&lt;property
			column="NUM_CART"
			length="16"
			name="numeroCartao"
			not-null="true"
			type="java.lang.String"
		 /&gt;

		
		&lt;many-to-one class="Cor" name="cor"	not-null="false" insert="false" update="false"&gt;
			&lt;column name="COR_ID" /&gt;
            &lt;column name="EMPRESA_ID" /&gt;
		&lt;/many-to-one&gt;
        &lt;many-to-one class="ModeloVeiculo" name="modeloVeiculo"	not-null="false"	&gt;
			&lt;column name="MOD_VEI_ID" /&gt;
			&lt;column name="EMPRESA_ID" /&gt;
		&lt;/many-to-one&gt;
		&lt;many-to-one class="Empresa" name="empresa"	not-null="false" insert="false" update="false" &gt;
			&lt;column name="EMPRESA_ID" /&gt;
		&lt;/many-to-one&gt;
		&lt;set inverse="true" name="ManutencaoVeiculoSet"&gt;
			&lt;key column="RENAVAM" /&gt;
			&lt;one-to-many class="ManutencaoVeiculo" /&gt;
		&lt;/set&gt;
	&lt;/class&gt;
&lt;/hibernate-mapping&gt;

O problema eh, a coluna empresa_id se repete nos relacionamentos e no começo dava erro ate eu ter que colocar os valores insert="false" update="false" ate ai tudo bem. o problema eh que ja tem mais de 3 dias que qundo eu faço VeiculoDAO.find("from Veiculo v where v.empresa = 1234") ele da erro ao retornar a empresa relacionada ao veiculo, o erro eh:

Hibernate: select veiculo0_.RENAVAM as RENAVAM, veiculo0_.ANO_FABRIC as ANO_FABRIC, veiculo0_.PLACA as PLACA, veiculo0_.ANO_MOD as ANO_MOD, veiculo0_.KM_INI as KM_INI, veiculo0_.CHASSI as CHASSI, veiculo0_.NUM_CART as NUM_CART, veiculo0_.COR_ID as COR_ID, veiculo0_.EMPRESA_ID as EMPRESA_ID, veiculo0_.MOD_VEI_ID as MOD_VEI_ID from VEIC veiculo0_ where (veiculo0_.EMPRESA_ID=1234 )
Hibernate: select empresa0_.EMPRESA_ID as EMPRESA_ID0_, empresa0_.RAZ_SOC as RAZ_SOC0_, empresa0_.NM_FANT as NM_FANT0_ from EMPRESA empresa0_ where empresa0_.EMPRESA_ID=?
11:06:06,041  WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: null
11:06:06,051 ERROR JDBCExceptionReporter:46 - No data found
11:06:06,051  WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: null
11:06:06,071 ERROR JDBCExceptionReporter:46 - No data found
11:06:06,111 ERROR JDBCExceptionReporter:38 - Could not execute query
java.sql.SQLException: No data found

na hora que ele tenta pegar a empresa relacionada ao veioculo ele da pau, so que existe dado no banco! nao estou entendendo esse erro!

se eu coloco na tabela veiculo o campo empresa_id como nulo, ele passa blz, so nao retorna resultado nenhum!

Por favor ajudem um desesperado :shock: :( :cry:

[]´s

Reinaldo Nunes Junior

5 Respostas

Umlauf

A princípio, só vejo algo estranho na cláusula from:

VeiculoDAO.find(“from Veiculo v where v.empresa.id = 1234”)

Embora o Hibernate esteja montando a query certinho.

E o mapeamento de EMPRESA, como tá? Posta pra gente, por favor.

R

Ola Amigo!

Obrigado por me ajudar!

o mapeamento da Empresa ta assim:
<?xml version="1.0"?>
&lt;!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" &gt;

&lt;hibernate-mapping package="basecard.adm"&gt;
	&lt;class name="Empresa" table="EMPRESA"&gt;
		&lt;id
			column="EMPRESA_ID"
			name="identificador"
			type="java.lang.Integer"
		&gt;
			&lt;generator class="increment"  /&gt;
		&lt;/id&gt;
		
		&lt;property
			column="RAZ_SOC"
			length="50"
			name="razaoSocial"
			not-null="false"
			type="java.lang.String"
		 /&gt;

		&lt;property
			column="NM_FANT"
			length="20"
			name="nomeFantasia"
			not-null="true"
			type="java.lang.String"
		 /&gt;
		 
	&lt;/class&gt;
&lt;/hibernate-mapping&gt;

eu não entendo esse erro, to nisso tem um tempao ja!!
não entendo o porque desse NO DATA FOUND.

[]´s

Reinaldo Nunes Junior

Umlauf

Hummm era o que eu suspeitava. Seu mapeamento de EMPRESA não faz nenhuma referência a VEICULO. Só não sei se minha suspeita é válida. :P

De qqer forma, tente mudar o mapeamento de empresa:

<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping package="basecard.adm">
	<class name="Empresa" table="EMPRESA">
		<id
			column="EMPRESA_ID"
			name="identificador"
			type="java.lang.Integer"
		>
			<generator class="increment"  />
		</id>
		
		<property
			column="RAZ_SOC"
			length="50"
			name="razaoSocial"
			not-null="false"
			type="java.lang.String"
		 />

		<property
			column="NM_FANT"
			length="20"
			name="nomeFantasia"
			not-null="true"
			type="java.lang.String"
		 />
		 
<!-- Adicione isso -->
        <set
            name="veiculos"
            lazy="false"
            inverse="true"
        >

              <key
                  column="EMPRESA_ID"
              >
              </key>

              <one-to-many
                  class="Veiculo"
              />

        </set>
<!-- Até aqui -->
	</class>
</hibernate-mapping>

Não esqueça de adicionar na classe Empresa uma propriedade private Set veiculos e seus respectivos getter e setter.

R

Ola amigo, Bom Dia.

fiz isso e nada.
mas andei fazendo uns testes e concluir o seguinte:
o modelo eh esse:

no meu HBM Veiculo quando eu tiro a empresa, isto eh comento a linha

&lt;many-to-one class="Empresa" name="empresa" not-null="false" &gt; &lt;column name="EMPRESA_ID" /&gt; &lt;/many-to-one&gt;

ele funciona blz,
mas funciona blz tb se eu tirar comentar a linha referente COR e MODELOVEICULO deixado so empresa!

a conclusao que eu tirei eh, eu acho que o problema esta nessas PK´s Compostas, pq se eu colocar empresa junto com qualquer um dos dois que tenha PK composta da esse erro:

10:27:37,690 WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: null 10:27:37,700 ERROR JDBCExceptionReporter:46 - No data found 10:27:37,700 WARN JDBCExceptionReporter:38 - SQL Error: 0, SQLState: null 10:27:37,710 ERROR JDBCExceptionReporter:46 - No data found 10:27:37,730 ERROR JDBCExceptionReporter:38 - Could not execute query java.sql.SQLException: No data found at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7139) at sun.jdbc.odbc.JdbcOdbc.SQLGetDataInteger(JdbcOdbc.java:3812) at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(JdbcOdbcResultSet.java:5642) at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:585) at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:603) ...

não sei o que deve ser.

POrFavor alguem me ajude, podem falar qualquer coisa.
Garanto a vcs que vao ser bem vinda :stuck_out_tongue: :slight_smile:

[]´s

Reinaldo Nune Junior

_fs

Você tentou a sugestão do Umlauf de colocar no find “v.empresa.id”? Muito estranho ele montar a query de forma correta, mas é melhor arrumar o seu código :smiley:

Criado 1 de dezembro de 2004
Ultima resposta 3 de dez. de 2004
Respostas 5
Participantes 3