Olá amígos…
Estou tendo problemas com um mapeamento 1:N no Hibernate 3.
Andei procurando no forum mas não encontrei nada a respeito.
Será que alguém poderia me ajudar?
O problema:
Tenho uma classe Cliente que possui uma coleção de Nota (java.util.Set).
Cliente.java:
public class Cliente {
private Integer id;
private String nome;
private String endereco;
private String numero;
private String bairro;
private String cidade;
private String estado;
private String cep;
private String telefone;
private String celular;
private String obs;
private Set<Nota> notas;
//gets e sets...
Cliente.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD/EN" "http://hibernate.sourceforge.net/hibernate-mapping.dtd">
<hibernate-mapping>
<class name="notas.Cliente" table="clientes">
<id name="id" column="id" type="java.lang.Integer" unsaved-value="-1">
<generator class="increment"/>
</id>
<property name="nome" type="java.lang.String" column="nome" length="50"/>
<property name="endereco" type="java.lang.String" column="endereco" length="50"/>
<property name="numero" type="java.lang.String" column="numero" length="10"/>
<property name="bairro" type="java.lang.String" column="bairro" length="50"/>
<property name="cidade" type="java.lang.String" column="cidade" length="50"/>
<property name="estado" type="java.lang.String" column="estado" length="2" />
<property name="cep" type="java.lang.String" column="cep" length="10"/>
<property name="telefone" type="java.lang.String" column="telefone" length="20"/>
<property name="celular" type="java.lang.String" column="celular" length="20"/>
<property name="obs" type="java.lang.String" column="obs" length="200"/>
<set name="notas" inverse="true" cascade="save-update">
<key column="idCliente"/>
<one-to-many class="notas.Nota"/>
</set>
</class>
</hibernate-mapping>
Nota.java:
public class Nota {
private Integer id;
private Cliente cliente;
private String nome;
private String dataEmissao;
private String dataVencimento;
private double valor;
private boolean situacao; // true = pago; false = não pago
Nota.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD/EN" "http://hibernate.sourceforge.net/hibernate-mapping.dtd">
<hibernate-mapping>
<class name="notas.Nota" table="notas">
<property name="nome" type="java.lang.String" column="nome" />
<property name="emissao" type="java.lang.String" column="data" />
<property name="vencimento" type="java.lang.String" column="vencimento" />
<property name="valor" type="java.lang.Double" column="valor" />
<property name="situacao" type="java.lang.Boolean" column="situacao" />
<many-to-one name="cliente" class="Cliente" column="idCliente" />
<id name="id" column="id" type="java.lang.Integer" unsaved-value="-1">
<generator class="increment" />
</id>
</class>
</hibernate-mapping>
Porém, esse mapeamento retorna a seguinte esseção:
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Cliente.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at notas.HibernateConnection.<clinit>(HibernateConnection.java:11)
at notas.Main.main(Main.java:30)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:502)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 7 more
Caused by: org.xml.sax.SAXParseException: Attribute "role" is required and must be specified for element type "set".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
... 8 more
Exception in thread "main" java.lang.NullPointerException
at notas.HibernateConnection.getSession(HibernateConnection.java:23)
at notas.Main.main(Main.java:30)
Acho que o problema é referente a tag set. Repare na mensagem:
Attribute "role" is required and must be specified for element type "set".
Alguém poderia me dar um help?