Ajuda ao gerar codigo .hbm - hibernate

2 respostas
D

Ola pessoal,

Estou com dificuldade em rodar uma pequena aplicação de duas tabelas.

1 Municipio para N Praia

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<!-- a SessionFactory instance listed as /jndi/name -->
	<session-factory name="org/hibernate/SessionFactory">
		<!-- properties -->
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/sin
		</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		<property name="show_sql">true</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123</property>
		<!-- mapping files -->
		<mapping resource="map/Municipio.hbm.xml" />
		<mapping resource="map/Praia.hbm.xml" />
		
	</session-factory>
</hibernate-configuration>
Municipio.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="bean.Municipio" table="municipio">
		<id name="codMunicipio" column="codMunicipio" type="int">
			<generator class="native">
			</generator>
		</id>
		<property name="descMunicipio" type="java.lang.String" column="descMunicipio"/>

		<one-to-many name="praia" class="Praia"column="codPraia" />
       
	</class>
</hibernate-mapping>
Praia.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="bean.Praia" table="praia">
		<id name="codPraia" column="codPraia" type="int">
			<generator class="native">
			</generator>
		</id>
		<property name="descPraia" type="java.lang.String" column="descPraia"/>

	  <many-to-one name="municipio"class="Municipio"column="codMunicipio" />

            </set>
	</class>
</hibernate-mapping>
Teste
package testes;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import bean.Municipio;

public class AddMunicipio {

	public static void main(String[] args) {

		SessionFactory sf = new Configuration().configure("/conf/hibernate.cfg.xml").buildSessionFactory();
		
		Session session = sf.openSession();
		
		Transaction tx = session.beginTransaction();
		
		Municipio municipio = new Municipio();
		municipio.setDescMunicipio("palhoça");
	
				
		session.save(municipio);
		tx.commit();
		session.close();
		
		
	}

}

Aparece o seguinte erro:
Error on line 12 of document : Element type "one-to-many" must be followed by either attribute specifications, ">" or "/>". Nested exception: Element type "one-to-many" must be followed by either attribute specifications, ">" or "/>"

Alguem poderia me dar um help?

Obrigado

2 Respostas

T
&lt;many-to-one name="municipio"class="Municipio"column="codMunicipio" /&gt;

Não chequei seu XML, mas não seria interessante pôr uns espacinhos a mais? A tecla de espaços é grande, principalmente para ser fácil de usar :stuck_out_tongue:

Copiei seu XML em um arquivo, tentei carregá-lo no Internet Explorer para checar se ele era um XML válido, e deu o seguinte:

The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 


--------------------------------------------------------------------------------

Required white space was missing. Error processing resource 'file:///C:/temp/teste.xml'. Line 12, Position 34 

    &lt;many-to-one name="municipio"class="Municipio"column="codMunicipio" /&gt;
---------------------------------^
D

Olá Pessoal,

Problema resolvido. Colo os dos codigos hbm.xml pra próximas consultas:
Obrigadao

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="bean.Municipio" table="municipio">
		<id name="codMunicipio" column="codMunicipio" type="int">
			<generator class="native">
			</generator>
		</id>
		<property name="descMunicipio" type="java.lang.String" column="descMunicipio"/>

		<bag
            name="praia"
            inverse="true"
             order-by="DATE_TIME"
            cascade="all">

            <key column="codPraia"/>
            <one-to-many class="bean.Praia"/>

        </bag>
				      
	</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="bean.Praia" table="praia">
		<id name="codPraia" column="codPraia" type="int">
			<generator class="native">
			</generator>
		</id>
		<property name="descPraia" type="java.lang.String" column="descPraia"/>

	  			
			<many-to-one
            name="municipio"
            column="codMunicipio"
            not-null="true"/>

          </class>
</hibernate-mapping>
Criado 21 de março de 2007
Ultima resposta 21 de mar. de 2007
Respostas 2
Participantes 2