Andrei Ermicioi's Blog

Just another WordPress.com weblog

Hibernate UserType field

leave a comment »

Nu voi incepe sa vorbesc ce este Hibernate, doar voi spune ca e ORM Framework pentru Java.

Problema pe care o voi incerca s-o tratez este, cum facem atunci cind vrem sa mapam un tip de date defenit de noi cu relatia bazei de date? Incerc sa explic. Presupunem avem un bean care are un parametru ce arata starea obiectului, sa zicem SUCCESS/ERROR, acest parametru este un ENUM. Apare intrebarea sub ce tip de date pastram acest parametru in DB, si cum facem asa ca atunci cind bean-ul se pastreaza in DB sa lucreze conform tipului de date ales in DB, iar cind lucram cu el in applicatie el sa fie un ENUM? Aici in ajutor ne vine interfata UserType, astfel vom avea ca field-ul in DB va fi de tip varchar, iar in bean el va fi ENUM.

Defenim tipul nostru

public class BeanStatus implements UserType {
}

classa BeanStatus va fi nevoita sa implementeze citeva metode defenite in UserType.

public boolean isMutable() {
return false;
}

public Object deepCopy(Object arg0) throws HibernateException {
return arg0;
}

public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
throw new UnsupportedOperationException();
}

public Serializable disassemble(Object arg0) throws HibernateException {
throw new UnsupportedOperationException();
}

public int hashCode(Object arg0) throws HibernateException {
return arg0.hashCode();
}

public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
throw new UnsupportedOperationException();
}

Cam astea ar fi metodele care ar avea o mai putina importanta in implimentare, dar daca e nevoie trebuie implimentate.
Metodele ce urmeaza am decis ca au o importanta majora pentru tipul BeanStatus. Mai intii defenim SQL tipul si o facem asta prin un array

private static final int[] SQL_TYPES = {Types.VARCHAR};

Urmeaza metodele

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

public int[] sqlTypes() {
return SQL_TYPES;
}

public boolean equals(Object o1, Object o2) throws HibernateException {
if (o1 == o2) {
return true;
}

if (o1 == null || o2 == null) {
return false;
}

return o1.equals(o2);
}

public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {
String value = resultSet.getString(names[0]);
return (resultSet.wasNull()) ? null : BeanStatus.valueOf(value);
}

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {
if (value != null) {
statement.setString(index, ((BeanStatus)value).toString());
} else {
statement.setNull(index, Types.VARCHAR);
}
}

Dupa implementarea classei putem s-o utilizam in hbm file-uri, spre ex:

<property name="status" column="status" type="com.wordpress.eraniblog.BeanStatus" />

Advertisements

Written by eraniblog

May 4, 2010 at 10:46

Posted in Coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: