-->
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: UserType, <id> and HQL
PostPosted: Fri Aug 20, 2004 10:49 am 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
Hibernate version: 2.1.6

Mapping documents:
<class name="Cat">
<id name="name" type="UStringType">
<generator class="assigned"/>
</id>
<version name="version"/>
<property name="age"/>
</class>

----
public class UString implements Serializable {

private final String value;

public UString (String value) {
this.value = (value!=null ? value.toUpperCase() : null);
}

public String toString() {
return value;
}

public boolean equals(Object o) {
return ((UString)o).toString().equals(value);
}

public int hashCode() {
return value.hashCode();
}

}

------
public class UStringType implements UserType {

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

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

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

public boolean equals(Object x, Object y) {
if (x==y) return true;
if ((x==null) || (y==null)) return false;
return x.equals(y);
}

public Object deepCopy(Object value) {
return value;
}

public boolean isMutable() {
return false;
}

public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
throws HibernateException, SQLException {

if (resultSet.wasNull()) return null;
String value = resultSet.getString(names[0]);
return new UString(value);
}

public void nullSafeSet(PreparedStatement statement, Object value, int index)
throws HibernateException, SQLException {

if (value==null) {
statement.setNull(index, Types.VARCHAR);
} else {
UString ustring = (UString)value;
statement.setString(index, ustring.toString());
}

}

}




Code between sessionFactory.openSession() and session.close():
List cats = session.find("from Cat as cat where cat.name like ?", new UString("FU%"), Hibernate.custom(UStringType.class));
Iterator iter = cats.iterator();


Name and version of the database you are using: MySQL 4


I perfectly know that there's always a great debate on custom types (UerType and CompositeUserType).
I've read all the documentation (quite carefully, I suppose...) and a large number of posts in the forum, but...

1) I still don't understand if it's legal to have a mapping similar to that I've
posted: it seems to work perfectly but, who knows, ...

and

2) trying to query with a partial match, similar to the example:
SELECT * FROM cat WHERE name LIKE 'FU%'
It works!!

Now: could someone answer me to 1 and 2 ?

Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 21, 2004 4:25 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
1) This works if you are sure, that there's no application writing lower case ids into this column. Else you can get unwanted conversions from lower case to upercase.

2) Anybody else knows the answer?

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 21, 2004 4:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sure its legal.

You can even auto-generate custom type ids by writing your own IdentifierGenerator.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 21, 2004 2:22 pm 
Beginner
Beginner

Joined: Thu Aug 19, 2004 5:36 am
Posts: 30
Location: Italy
Thank you Ernst, thank you Gavin; and congratulations to Gavin for Hibernate: really a good product!
And a good community...


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.