-->
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.  [ 4 posts ] 
Author Message
 Post subject: What is UserType.
PostPosted: Mon Sep 13, 2004 1:45 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Hi everyone:

I see the UserType in many document.It seems very important conception in Hibernate. The Hibernate document says it is better to use UserType than composite-id. But there is little introduce to the "UserType". Someone can tell me how to use the "UserType" or give me an example? An simple example of "UserType" . THks.

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 4:04 am 
Regular
Regular

Joined: Wed Aug 18, 2004 5:16 am
Posts: 69
Location: Modena, Italy
This is an example UserType to map a BigInteger to DB, it uses only one column but you can write UserType mapped on many columns:
Code:
import net.sf.hibernate.UserType;
import java.sql.*;
import java.math.*;

public class BigIntegerType implements UserType {
   
    private static final int[] SQL_TYPES = {Types.VARCHAR};
    private static final int RADIX = 2;
   
    public int[] sqlTypes() {
        return SQL_TYPES;
    }
   
    public BigIntegerType() {
    }
   
    public boolean equals(Object x, Object y) throws net.sf.hibernate.HibernateException {
        if (x == y) return true;
        if (x == null || y == null) return false;
        return x.equals(y);
    }
   
    public Object deepCopy(Object obj) throws net.sf.hibernate.HibernateException {
        return obj;
    }
   
    public boolean isMutable() {
        return false;
    }
   
    public Object nullSafeGet(java.sql.ResultSet resultSet, String[] names, Object owner) throws net.sf.hibernate.HibernateException, java.sql.SQLException {
        if (resultSet.wasNull()) return null;
        String res = resultSet.getString(names[0]);
        return decode(res);
    }
   
    public void nullSafeSet(java.sql.PreparedStatement statement, Object value, int index) throws net.sf.hibernate.HibernateException, java.sql.SQLException {
        if (value == null) {
            statement.setNull(index, sqlTypes()[0]);
        } else {
            BigInteger number = (BigInteger)value;
            statement.setString(index, encode(number));
        }
    }
   
    public Class returnedClass() {
        return BigInteger.class;
    }
   
    public static String encode(BigInteger value) {
        return value.toString(RADIX);
    }
   
    public static BigInteger decode(String value) {
        return new BigInteger(value, RADIX);
    }
   
}

Then you can use your class as type value for properties for example:
Code:
    <class name="Product" table="PRODUCTS">
        <id name="id" column="ID" type="string" length="32">
            <generator class="uuid.hex"/>
        </id>
       
        <property name="name" column="NAME" type="string" length="256"/>
        <property name="veryLongNumericCode" column="CODE" type="BigIntegerType"/>
       
    </class>


Top
 Profile  
 
 Post subject: Thks
PostPosted: Wed Sep 15, 2004 3:54 am 
Senior
Senior

Joined: Wed Dec 17, 2003 4:24 am
Posts: 188
Thank you for your reply.But there are some thing that I don't understand in your code , the following code:
Code:
public static String encode(BigInteger value) {
        return value.toString(RADIX);
    }
   
    public static BigInteger decode(String value) {
        return new BigInteger(value, RADIX);
    }
   


What does it mean? I don't know why need I have to implement these two methed. Could you give me an composite-id example ? I mean that how to write a composite-id (two primary key) if I implement it using UserType? Thks

_________________
You are not alone...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 15, 2004 8:25 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Type:
Code:
package ch.bedag.gba.cap.server.persistence.hibernate;

import java.io.Serializable;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;

public class CapiPK implements Serializable {

   private Long mySystemId = null;
   private Long myId = null;
   
   public CapiPK() {
       super();
   }
    public CapiPK(Long systemId, Long pid) {
        this();
        mySystemId = systemId;
        myId = pid;
    }
    public CapiPK(long systemId, long pid) {
        super();
        mySystemId = new Long(systemId);
        myId = new Long(pid);
    }
   public Long getId() {
        return myId;
    }
    public void setId(Long pid) {
        myId = pid;
    }
    public Long getSystemId() {
        return mySystemId;
    }
    public void setSystemId(Long systemId) {
        mySystemId = systemId;
    }
   
    public boolean equals(Object obj) {
        if (!(obj instanceof CapiPK)) return false;
        CapiPK pk = (CapiPK)obj;
        return new EqualsBuilder()
           .append(mySystemId, pk.mySystemId)
           .append(myId, pk.myId)
           .isEquals();
    }
   
    public int hashCode() {
        return new HashCodeBuilder()
           .append(mySystemId)
           .append(myId)
           .toHashCode();
    }
   
    public String toString() {
       return new ReflectionToStringBuilder(this).toString();
    }
}


CompositeUserType:
[code]package ch.bedag.gba.cap.server.persistence.hibernate;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import net.sf.hibernate.CompositeUserType;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.engine.SessionImplementor;
import net.sf.hibernate.type.Type;
import ch.bedag.gba.server.persistence.PersistenceRuntimeException;

public class CapiPkType implements CompositeUserType {

private static final ch.bedag.gba.common.logging.CapiLogger LOG = ch.bedag.gba.common.logging.CapiLoggerFactory
.getLog(CapiPkType.class);

private static final String[] PROPERTY_NAMES = {"systemId", "id"};
private static final Type[] PROPERTY_TYPES = {Hibernate.LONG, Hibernate.LONG};

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

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

public Object getPropertyValue(Object component, int property) throws HibernateException {
CapiPK capiPk = (CapiPK)component;
switch (property) {
case 0:
return capiPk.getSystemId();
case 1:
return capiPk.getId();
default:
throw new PersistenceRuntimeException(property+" ist kein g


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.