Hibernate a UserType

Roman Hrivik roman na hrivik.com
Středa Říjen 6 13:53:40 CEST 2004


s hibernate som robil len kratko a usertype mam len 2x
ale vsade mam ako usertype samostatny class implementujuci
net.sf.hibernate.UserType

ako to mas namapovane v tvojom hbm.xml ?
co je v tvojom pripade EntityBeanParent ?
ma v sebe implements net.sf.hibernate.UserType ?

sprav si nato extra class
RoleUserType implements UserType 
a v mapping file to namapuj ako

<property name="role" column="role_id"
type="tvoj.package.RoleUserType"/>

Roman





-----Original Message-----
From: Jaroslav Hurdes [mailto:java na ataco.cz] 
Sent: Wednesday, October 06, 2004 1:29 PM
To: Konference Java
Subject: Hibernate a UserType


Zdravim vsechny, mam tridu RolesGroups a v ni instancni promennou role 
typu Role. Chci aby se mi pri ulozeni RolesGroups  do db se zapsalo z 
promenne typu Role pouze id. Trida role implementuje UserType. Problem 
je, ze se mi do tabulky, kde se ma ulozit id role vygeneruje sloupec 
scrg_sco_id RAW (255) a ne jak bych predpokladal NUMERIC. Predpokladal 
jsem ze se typ sloupce u uzivatelskych tyu ridi podle navratove hodnoty 
metody 

int[] sqlTypes() { return new int[] {Types.INTEGER }; }. Nenapada vas 
nekoho jak z toho ven. Pro generovani db pouzivam ant a tag vyuziva 
net.sf.hibernate.tool.hbm2ddl.SchemaExportTask nebo 
net.sf.hibernate.tool.hbm2ddl.SchemaUpdateTask.

/**
 * @hibernate.class
 *      table = "SC_ROLES_GROUPS"
 */
class RolesGroups extends EntityBeanParent {
    private static final long serialVersionUID = 1L;
   
    protected Role role;
    protected Group group;
   
    /**
     * Implicitni konstruktor
     */
    public RolesGroups() {
        this.role = null;
        this.group = null;
    }
~
~
~

    /**
     * @hibernate.property
     *      column = "scrg_sco_id"
     *      type = "com.xxx.security.business.Role"
     *      not-null = "true"
     *      insert = "true"
     *      update = "true"
     */
    public Role getRole() {
        return role;
    }   
    public void setRole(Role role) {
        this.role = role;
    }
   
~
~
~
    }


/**
 * @hibernate.class
 *      table = "SC_ROLES"
 */
class Role extends EntityBeanParent {
~
~
~
 
    //Implementace hibernate UserType
    public int[] sqlTypes() {
        return new int[] { Types.INTEGER };
    }

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

    public boolean equals(Object x, Object y) {
        return (x == y) || (x != null && y != null && x.equals(y));
    }

    public Object nullSafeGet(ResultSet rs, String[] names, Object 
owner) throws HibernateException, SQLException {
        long id =  rs.getLong(names[0]);
        Role role = new Role();
        role.setId(new Long(id));
        return role;
    }
   
    public void nullSafeSet(PreparedStatement st, Object value, int
index)
            throws HibernateException, SQLException  {
        if(value == null) {
            return;
        }
        st.setLong(index, ((Role)value).getId().longValue());
    }

    public Object deepCopy(Object value) {
        if (value == null) {
            return null;
        }

        return new Role((Role)value);
    }

    public boolean isMutable() {
        return true;
    }
}

Diky Jaroslav Hurdes.



Další informace o konferenci Konference