-->
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: CompositeUserType, Foreign Key, and SchemaExport
PostPosted: Fri Sep 16, 2005 11:18 am 
Newbie

Joined: Tue May 11, 2004 10:47 am
Posts: 16
Hibernate version: 3

Mapping documents:
<property name="valueAndEntity" type="ValueAndEntityUserType">
<column name="value"/>
<column name="entity" />
</property>


Name and version of the database you are using:
MySQL 5 for Win NT

In the API for CompositeUserType:

Quote:
A CompositeUserType may be used in almost every way that a component may be used. It may even contain many-to-one associations.


For my ValueAndEntityUserType, I want the second column to be a many-to-one association - so a foreign key to the "Entity" table. However, when I use SchemaExport, these foreign keys are not created.

Here's the user type:

Code:
public class ValueAndEntityUserType implements CompositeUserType {

    private static final int VALUE = 0;

    private static final int ENTITY = 1;

    private static final String[] NAMES = { "value", "entity" };

    private static final Type[] TYPES = { Hibernate.INTEGER,
            Hibernate.entity(Entity.class) };

    public String[] getPropertyNames() {
        return NAMES;
    }

    public Type[] getPropertyTypes() {
        return TYPES;
    }

    public Object getPropertyValue(Object component, int property)
            throws HibernateException {
        ValueAndEntity cd = (ValueAndEntity) component;
        switch (property) {
        case VALUE:
            return new Integer(cd.getValue());
        case ENTITY:
            return cd.getEntity();
        }
        throw new IllegalArgumentException("Unknown property index: "
                + property);
    }

    public Object nullSafeGet(ResultSet rs, String[] names,
            SessionImplementor session, Object owner)
            throws HibernateException, SQLException {
        ValueAndEntity ret = new ValueAndEntity();

        ret.setValue(rs.getInt(names[0]));
        ret.setEntity((Entity) session.get(
                Entity.class, new Long(rs.getLong(names[1]))));

        return ret;
    }

    public void nullSafeSet(PreparedStatement st, Object value, int index,
            SessionImplementor session) throws HibernateException, SQLException {
        if (value == null) {
            st.setNull(index, Types.INTEGER);
            st.setNull(index + 1, Types.BIGINT);
        } else {
            ValueandEntity cd = (ValueandEntity) value;
            st.setInt(index, cd.getValue());
            st.setLong(index + 1, cd.getEntity().getId());
        }
    }

    public Class returnedClass() {
        return ValueAndEntity.class;
    }

    public void setPropertyValue(Object component, int property, Object value)
            throws HibernateException {
        ValueAndEntity cd = (ValueAndEntity) component;
        switch (property) {
        case VALUE:
            cd.setValue(((Integer) value).intValue());
            break;
        case ENTITY:
            cd.setEntity((Entity) value);
            break;
        default:
            throw new IllegalArgumentException("Unknown property index: "
                    + property);
        }
    }


I thought that since
Code:
getPropertyTypes()

returns
Code:
{ Hibernate.INTEGER, Hibernate.entity(Entity.class) }

that the second column would be a foreign key to the Entity table.

So my questions are: is it possible to have SchemaExport create a foreign key for a column in a CompositeUserType and, if so, what am I doing wrong?? Also, is it appropriate that in
Code:
nullSafeSet
I use
Code:
ret.setEntity((Entity) session.get(Entity.class, new Long(rs.getLong(names[1]))));

?

Thanks in advance,

Luke


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 16, 2005 2:55 pm 
Newbie

Joined: Tue May 11, 2004 10:47 am
Posts: 16
Hey Hibernate Team, look what a good boy I am: I helped (or tried to) out two people today:

http://forum.hibernate.org/viewtopic.php?t=947730
http://forum.hibernate.org/viewtopic.php?t=947728

So... can one of you take a quick look at my problem, please?

Thanks, Luke.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 9:52 am 
Newbie

Joined: Tue May 11, 2004 10:47 am
Posts: 16
This isn't too complicated a question, I don't think - could somebody from the Hibernate Team please comment on my initial post? It would really help me out...

Luke


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 21, 2005 9:45 am 
Newbie

Joined: Tue May 11, 2004 10:47 am
Posts: 16
I would really appreciate some help here...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 4:28 pm 
Newbie

Joined: Tue May 11, 2004 10:47 am
Posts: 16
Testing: one... two... three... anybody?? Where are all you Hibernate geniuses??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 4:51 pm 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
luke wrote:
Testing: one... two... three... anybody?? Where are all you Hibernate geniuses??

Hi,

Sorry I can't help you (I'm definitely no a Hibernate genius...) - but I wanted to tell you that someone is there...

I'll try to have a look at CompositeUserType tomorrow - but I fear, it would take more time to be able to help you.

Hopefully in the meantime a real specialist will rescue both of us :-).

So


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 9:08 am 
Newbie

Joined: Fri Feb 18, 2005 8:06 am
Posts: 4
I use a "component".

For example:

public class Almacen {

private Long id;
private int version;
private short codigo;
private Direccion direccion;

}

public class Direccion {

private String domicilio;
private String telefono;
private TipoDomicilio tipoDomicilio;
private Distrito distrito;
private Pais pais;

}

<hibernate-mapping>
<class name="Almacen" table="almacen" dynamic-update="false" dynamic-insert="false">
<id name="id" type="long">
<column name="id"/>
<generator class="native"></generator>
</id>
<version name="version" type="int" column="version" access="property" unsaved-value="undefined"/>
<property name="codigo" type="short" update="true" insert="true" access="property">
<column name="codigo" not-null="true" unique="true"/>
</property>

<component name="direccion" class="Direccion">
<property name="domicilio" column="domicilio" type="string" length="255" not-null="false" />
<property name="telefono" column="telefono" type="string" length="60" not-null="false" />
<many-to-one name="tipoDomicilio" class="TipoDomicilio" column="id_tipo_domicilio" foreign-key="fk_almacen_tipodomicilio" not-null="false" outer-join="true" />
<many-to-one name="distrito" class="Distrito" column="id_distrito" foreign-key="fk_almacen_distrito" not-null="false" outer-join="true" />
<many-to-one name="pais" class="Pais" column="id_pais" foreign-key="fk_almacen_pais" not-null="false" outer-join="true" />
</component>


</class>
</hibernate-mapping>


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.