-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Mapping Class.hbm.xml
PostPosted: Fri Aug 05, 2005 3:56 pm 
Newbie

Joined: Fri Aug 05, 2005 3:42 pm
Posts: 12
Hi!

I'm working with Hibernate and i dont know how to map a class. I'll post the class, the SQL and the Mapping.hbm.xml i've created. If there's somebody who could help me i'll thank a LOT!

Under here is the SQL

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

CREATE TABLE `projeto` (
`ID_PROJETO` int(11) NOT NULL auto_increment,
`ID_TIPO_SITUACAO_PROJETO` int(11) NOT NULL default '0',
`VERSAO` varchar(10) default NULL,
`SIGLA` varchar(15) default NULL,
`NOME_PROJETO` varchar(200) default NULL,
`DESCRICAO` text,
`DATA_INICIO` date default NULL,
`DATA_FIM_PREVISTO` date default NULL,
`DATA_FIM_REAL` date default NULL,
PRIMARY KEY (`ID_PROJETO`),
KEY `PROJETO_FKIndex1` (`ID_TIPO_SITUACAO_PROJETO`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now i'm posting the class code

-------------------------------------------------------------------------------------------
package com.transfer.object;

import java.util.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
* @version 1.0
* @created 02-mai-2005 18:12:55
* @Classe de um grupo;
*/
public class GrupoTO implements Serializable{

private long idGrupo; //Hash de um grupo
private long idGrupoFilho; //Hash do grupo filho
private String nomeDoGrupo; //String nome do grupo
private String nivel; //Nível de identação
private Set listaDeFilhos; //Lista de filhos do objeto

//Construtor da classe
public GrupoTO(){
this.listaDeFilhos = new HashSet();
}
public void setListaDeFilhos(Set lista){
this.listaDeFilhos = lista;
}
public Set getListaDeFilhos(){
return this.listaDeFilhos;
}
public void setNivel(String umNivel){
this.nivel = umNivel;
}
public String getNivel(){
return this.nivel;
}
//Seta o Id Grupo
public void setIdGrupo(long id){
this.idGrupo = id;
}
//Retorna o id do grupo
public long getIdGrupo(){
return this.idGrupo;
}
//Seta o id do grupo filho.
public void setIdGrupoPai(long id){
this.idGrupoFilho = id;
}
//Retorna o id do grupo filho
public long getIdGrupoPai(){
return this.idGrupoFilho;
}
//Seta o nome do grupo
public void setNomeDoGrupo(String umNomeGrupo){
this.nomeDoGrupo = umNomeGrupo;
}
//Retorna o nome do grupo
public String getNomeDoGrupo(){
return this.nomeDoGrupo;
}
//Zera o bichinho.
public void finalize() throws Throwable {
this.idGrupo = 0;
this.idGrupoFilho = 0;
this.nomeDoGrupo = null;
}
}

And now the GrupoTO.hbm.xml.

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

<?xml version="1.0" encoding="UTF-8"?>

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


<hibernate-mapping>
<class name="com.transfer.object.GrupoTO" table="grupo">
<id name="idGrupo" column="id_grupo" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="nomeDoGrupo" type="java.lang.String" column="descricao" length="255" not-null="true"/>
<property name="nivel" type="java.lang.String" column="nivel" length="2" not-null="true"/>
<set name="listaDeFilhos" lazy="true" inverse="true">
<key column="id_grupo_superior"/>
<many-to-one class="com.transfer.object.GrupoTO"/>
</set>
</class>
</hibernate-mapping>

Well the model and the business rule says that every 'grupo' line has a father that is represented by the column id_grupo_superior.
In the class model every 'grupo' object has a Set o sons.

Finally the mapping file is there and i really don't know how the make this, so every help will be welcome!

Thanks a lot!

Luís Augusto Machado Moretto


Top
 Profile  
 
 Post subject: Re: Mapping Class.hbm.xml
PostPosted: Fri Aug 05, 2005 4:17 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Have you tried the examples and read the reference doc ?

This is a really straightforward class to map so if you should be able to do it without trouble after you've walked through the first couple of chapters of the reference doc.

Also, I notice that your mapping files are using the version 2 dtd. Is this a new project you are working on or just doing this on your own ? If so, I'd recommend downloading 3.0.5 and using that.

Try and get an example running and if you have code that's throwing exceptions you don't understand, post them here and I'll be happy to help you.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject: Re: Mapping Class.hbm.xml
PostPosted: Fri Aug 05, 2005 9:20 pm 
Newbie

Joined: Fri Aug 05, 2005 3:42 pm
Posts: 12
Yes i did. But actually it seem's that the information is fragmented...
It's the seek for the santo grall!

[]'s

pksiv wrote:
Have you tried the examples and read the reference doc ?

This is a really straightforward class to map so if you should be able to do it without trouble after you've walked through the first couple of chapters of the reference doc.

Also, I notice that your mapping files are using the version 2 dtd. Is this a new project you are working on or just doing this on your own ? If so, I'd recommend downloading 3.0.5 and using that.

Try and get an example running and if you have code that's throwing exceptions you don't understand, post them here and I'll be happy to help you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 10:40 pm 
Beginner
Beginner

Joined: Thu Mar 31, 2005 9:58 pm
Posts: 25
Location: Valparaiso
Download the Caveat Emptor application, it's really usefull!

_________________
Daniel Casanueva R.

Jcode
Valparaiso, Chile


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 06, 2005 8:29 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 8:51 pm
Posts: 20
Location: Bergamo, Italy
To have an idea you can use MIDDLEGEN (search google) thant with eclipse plugin is very quick to use. It create automatically your mapping files so you have an idea..
..read the doc too!

bye


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 11:24 am 
Newbie

Joined: Fri Aug 05, 2005 3:42 pm
Posts: 12
vanholy wrote:
To have an idea you can use MIDDLEGEN (search google) thant with eclipse plugin is very quick to use. It create automatically your mapping files so you have an idea..
..read the doc too!

bye


[list=]Well,

Could you post the mapping file generated? Just to help me mapping the entire model....Thanks a lot ![/list][url][/url][url][/url]


Top
 Profile  
 
 Post subject: middlegen
PostPosted: Sun Aug 07, 2005 3:22 pm 
Beginner
Beginner

Joined: Tue Jun 28, 2005 8:51 pm
Posts: 20
Location: Bergamo, Italy
This is an autogenerate code with middlegen beginning from your table ( in the ide you can do some minor chancges too),

bye

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by the Middlegen Hibernate plugin 2.2

    http://boss.bekk.no/boss/middlegen/
    http://www.hibernate.org/
-->

<class
    name="org.ultimania.model.Projeto"
    table="projeto"
>

    <id
        name="idProjeto"
        type="java.lang.Integer"
        column="ID_PROJETO"
    >
   
        <generator class="assigned" />
    </id>

    <property
        name="idTipoSituacaoProjeto"
        type="int"
        column="ID_TIPO_SITUACAO_PROJETO"
        not-null="true"
        length="11"
    />
    <property
        name="versao"
        type="java.lang.String"
        column="VERSAO"
        length="10"
    />
    <property
        name="sigla"
        type="java.lang.String"
        column="SIGLA"
        length="15"
    />
    <property
        name="nomeProjeto"
        type="java.lang.String"
        column="NOME_PROJETO"
        length="200"
    />
    <property
        name="descricao"
        type="java.lang.String"
        column="DESCRICAO"
        length="65535"
    />
    <property
        name="dataInicio"
        type="java.sql.Date"
        column="DATA_INICIO"
        length="10"
    />
    <property
        name="dataFimPrevisto"
        type="java.sql.Date"
        column="DATA_FIM_PREVISTO"
        length="10"
    />
    <property
        name="dataFimReal"
        type="java.sql.Date"
        column="DATA_FIM_REAL"
        length="10"
    />

    <!-- Associations -->
 

</class>
</hibernate-mapping>


Code:
package org.ultimania.model;

import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class Projeto implements Serializable {

    /** identifier field */
    private Integer idProjeto;

    /** persistent field */
    private int idTipoSituacaoProjeto;

    /** nullable persistent field */
    private String versao;

    /** nullable persistent field */
    private String sigla;

    /** nullable persistent field */
    private String nomeProjeto;

    /** nullable persistent field */
    private String descricao;

    /** nullable persistent field */
    private Date dataInicio;

    /** nullable persistent field */
    private Date dataFimPrevisto;

    /** nullable persistent field */
    private Date dataFimReal;

    /** full constructor */
    public Projeto(Integer idProjeto, int idTipoSituacaoProjeto, String versao, String sigla, String nomeProjeto, String descricao, Date dataInicio, Date dataFimPrevisto, Date dataFimReal) {
        this.idProjeto = idProjeto;
        this.idTipoSituacaoProjeto = idTipoSituacaoProjeto;
        this.versao = versao;
        this.sigla = sigla;
        this.nomeProjeto = nomeProjeto;
        this.descricao = descricao;
        this.dataInicio = dataInicio;
        this.dataFimPrevisto = dataFimPrevisto;
        this.dataFimReal = dataFimReal;
    }

    /** default constructor */
    public Projeto() {
    }

    /** minimal constructor */
    public Projeto(Integer idProjeto, int idTipoSituacaoProjeto) {
        this.idProjeto = idProjeto;
        this.idTipoSituacaoProjeto = idTipoSituacaoProjeto;
    }

    public Integer getIdProjeto() {
        return this.idProjeto;
    }

    public void setIdProjeto(Integer idProjeto) {
        this.idProjeto = idProjeto;
    }

    public int getIdTipoSituacaoProjeto() {
        return this.idTipoSituacaoProjeto;
    }

    public void setIdTipoSituacaoProjeto(int idTipoSituacaoProjeto) {
        this.idTipoSituacaoProjeto = idTipoSituacaoProjeto;
    }

    public String getVersao() {
        return this.versao;
    }

    public void setVersao(String versao) {
        this.versao = versao;
    }

    public String getSigla() {
        return this.sigla;
    }

    public void setSigla(String sigla) {
        this.sigla = sigla;
    }

    public String getNomeProjeto() {
        return this.nomeProjeto;
    }

    public void setNomeProjeto(String nomeProjeto) {
        this.nomeProjeto = nomeProjeto;
    }

    public String getDescricao() {
        return this.descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public Date getDataInicio() {
        return this.dataInicio;
    }

    public void setDataInicio(Date dataInicio) {
        this.dataInicio = dataInicio;
    }

    public Date getDataFimPrevisto() {
        return this.dataFimPrevisto;
    }

    public void setDataFimPrevisto(Date dataFimPrevisto) {
        this.dataFimPrevisto = dataFimPrevisto;
    }

    public Date getDataFimReal() {
        return this.dataFimReal;
    }

    public void setDataFimReal(Date dataFimReal) {
        this.dataFimReal = dataFimReal;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("idProjeto", getIdProjeto())
            .toString();
    }

}

Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>

   <session-factory>

      <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.username">root</property>
      <property name="connection.password">root</property>
      <property name="connection.url">jdbc:mysql://localhost:3306/full_3</property>
        <property name="show_sql">true</property>
       
      <mapping resource="org/ultimania/model/Projeto.hbm.xml" />

   </session-factory>
</hibernate-configuration>

_________________
Marco

Don't forget to give credit if you get helpful information!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.