facebook

problems with composite-id

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #227130 Reply

    ivansaraiva
    Member

    Hi All,

    My application has a table and the PK has two attributes. The first attribute is a foreign key from another table with many to one relationships. Follow the excert from the hbm map file:

    <composite-id name=”id” class=”PerfilKey” >
    <key-property name=”idEmpresa” column=”id_Empresa” type=”java.lang.Integer” />
    <key-property name=”idPerfil” column=”id_perfil” type=”java.lang.Integer” />
    <composite-id>

    When the Hibernate process this file it throws:
    net.sf.hibernate.MappingException: Repeated column in mapping for collection: com.dedalus.bean.Usuario.perfis column: id_empresa
    at net.sf.hibernate.collection.AbstractCollectionPersister.checkColumnDuplication(AbstractCollectionPersister.java:654)
    at net.sf.hibernate.collection.AbstractCollectionPersister.<init>(AbstractCollectionPersister.java:167)

    How can I get away from this exception?

    Thanks in advance,
    Ivan

    #227140 Reply

    Riyad Kalla
    Member

    Ivan,
    Please paste your hbm.xml files as well as the table schemas for both tables.

    #227183 Reply

    ivansaraiva
    Member

    Hi Riyad,

    Now I’m running into another problem.
    I’m thinking I need a custom CompositeUserType implementation
    to load Usuario.

    The code which I’m trying to load Usuario from database is:
    List users = null;
    Object[] fields = {user.getCpf(),
    user.getSenha()};

    NullableType[] types = {Hibernate.STRING,
    Hibernate.STRING};

    users = session.find(“from Usuario where ” +
    “cpf = ? and senha = ?”,
    fields,
    types);
    I’m thinking that the Hibernate loads the rest of objects for me.
    Am I wrong?

    Domain Model is:
    Usuario ————-*Perfil ————* Funcao

    follow my tables scripts:
    ==================================
    Usuario Table
    PK = id_empresa, id_usuario
    ==================================

    create table Usuario(
    id_usuario int identity(10),
    id_empresa int(10),
    cod_Status int(10) default ‘(1)’,
    id_cliente int(10) default ‘(0)’,
    nome varchar(50),
    icq varchar(50) default ‘(‘ ‘)’,
    email varchar(50) default ‘(‘ ‘)’,
    login varchar(50),
    cod_StatusAtividade int(10) default ‘(1)’,
    cpf varchar(11),
    senha varchar(20),
    Modified_Date datetime default ‘(getdate())’,
    Action char(1) default ‘(‘C’)’,
    User_id int(10),
    id_superior int(10) default ‘(0)’)

    ==================================
    Perfil Table
    Link table for many-to-many relationship between
    Usuario and Perfil
    PK = FK id_empresa, — from Usuario
    FK cod_Usuario, — from Usuario
    FK cod_Perfil — from Perfil
    ==================================
    create table Usuario_Perfil(
    id_empresa int(10) not null,
    cod_Usuario int(10) not null,
    cod_Perfil int(10) not null)

    ==================================
    Perfil Table
    PK = id_empresa, id_perfil
    ==================================
    create table Perfil(
    id_perfil int identity(10),
    User_Id int(10),
    id_empresa int(10),
    nome varchar(50),
    descricao varchar(100),
    Action char(1) default ‘(‘C’)’,
    Modified_Date datetime default ‘(getdate())’)

    ==================================
    Usuario.hbm.xml
    ==================================
    <?xml version=”1.0″?>
    <!DOCTYPE hibernate-mapping PUBLIC
    “-//Hibernate/Hibernate Mapping DTD 2.0//EN”
    http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd&#8221; >

    <!– DO NOT EDIT: This is a generated file that is synchronized –>
    <!– by MyEclipse Hibernate tool integration. –>
    <!– Created Thu Mar 17 15:13:41 BRT 2005 –>
    <hibernate-mapping package=”com.dedalus.bean”>

    <class name=”Usuario” table=”Usuario”>
    <composite-id name=”id” class=”UsuarioKey”>
    <key-property name=”idEmpresa” column=”id_empresa” type=”java.lang.Integer”/>
    <key-property name=”idUsuario” column=”id_usuario” type=”java.lang.Integer”/>
    </composite-id>

    <property name=”codStatus” column=”cod_Status” type=”java.lang.Integer” not-null=”true” ></property>
    <property name=”idCliente” column=”id_cliente” type=”java.lang.Integer” />
    <property name=”nome” column=”nome” type=”java.lang.String” not-null=”true” />
    <property name=”icq” column=”icq” type=”java.lang.String” />
    <property name=”email” column=”email” type=”java.lang.String” />
    <property name=”login” column=”login” type=”java.lang.String” not-null=”true” />
    <property name=”codStatusAtividade” column=”cod_StatusAtividade” type=”java.lang.Integer” />
    <property name=”cpf” column=”cpf” type=”java.lang.String” not-null=”true” />
    <property name=”senha” column=”senha” type=”java.lang.String” not-null=”true” />
    <property name=”modifiedDate” column=”Modified_Date” type=”java.util.Date” />
    <property name=”action” column=”Action” type=”java.lang.String” />
    <property name=”userId” column=”User_id” type=”java.lang.Integer” />
    <property name=”idSuperior” column=”id_superior” type=”java.lang.Integer” />

    <set
    name=”perfis”
    table=”Usuario_Perfil”>

    <key>
    <column name=”id_empresa” />
    <column name=”cod_Perfil” />
    </key>

    <composite-element class=”Perfil” />

    <!–

    <composite-element class=”Perfil” >
    <nested-composite-element name=”id” class=”PerfilKey”></nested-composite-element>
    </composite-element>

    <many-to-many class=”Perfil” />
    <column name=”id_empresa” />
    <column name=”id_perfil” />
    </many-to-many>
    –>
    </set>
    </class>

    </hibernate-mapping>

    ==================================
    Perfil.hbm.xml
    ==================================
    <?xml version=”1.0″?>
    <!DOCTYPE hibernate-mapping PUBLIC
    “-//Hibernate/Hibernate Mapping DTD 2.0//EN”
    http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd&#8221; >

    <!– DO NOT EDIT: This is a generated file that is synchronized –>
    <!– by MyEclipse Hibernate tool integration. –>
    <!– Created Tue Mar 22 14:55:05 BRT 2005 –>
    <hibernate-mapping package=”com.dedalus.bean”>

    <class name=”Perfil” table=”Perfil”>
    <composite-id name=”id” class=”PerfilKey” >
    <!–
    <key-many-to-one name=”idEmpresa” column=”id_Empresa”
    foreign-key=”true” class=”Empresa” />
    –>
    <key-property name=”idEmpresa” column=”id_Empresa”
    type=”java.lang.Integer” />
    <key-property name=”idPerfil” column=”id_perfil”
    type=”java.lang.Integer” />
    </composite-id>

    <property name=”userId” column=”User_Id” type=”java.lang.Integer” not-null=”true” />
    <property name=”nome” column=”nome” type=”java.lang.String” not-null=”true” ></property>
    <property name=”descricao” column=”descricao” type=”java.lang.String” not-null=”true” />
    <property name=”action” column=”Action” type=”java.lang.String” not-null=”true” />
    <property name=”modifiedDate” column=”Modified_Date” type=”java.util.Date” not-null=”true” />

    <!–
    <set
    name=”funcoes”
    table=”Perfil_Funcao”
    cascade=”save-update”>
    <key foreign-key=”id” >
    <column name=”id_Empresa” />
    <column name=”id_Perfil” />
    </key>
    <many-to-many class=”Funcao” column=”codigo” />
    </set>
    –>
    </class>

    </hibernate-mapping>

    Thanks,
    Ivan.

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: problems with composite-id

You must be logged in to post in the forum log in