Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>static embedded</code> is ok, but in my situation it was better to define my own <code>"user-type"</code>and adding it to GORM mapping.</p> <p>I have only one <code>String</code> field with random generated business key in my class, so I wanted to store this value as varchar in db. The solutions is:</p> <p>In <code>Config.groovy</code> define:</p> <pre><code>grails.gorm.default.mapping={ "user-type" (type: my.package.persistence.PersistentBookKey, class: BookKey) } </code></pre> <p>Create class <code>PersistentBookKey</code> which implements <code>UserType</code> interface and ovveride some methods:</p> <pre><code>final class PersistentBookKey implements UserType { @Override protected BookKey createKeyFromString(String key) { BookKey.from(key) } @Override Class returnedClass() { BookKey } private static int [] SQL_TYPES=[Types.VARCHAR] as int[] @Override int[] sqlTypes() { return SQL_TYPES } @Override boolean equals(Object x, Object y) throws HibernateException { return x.equals(y); } @Override int hashCode(Object x) throws HibernateException { return x.hashCode() } @Override Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException { def key=rs.getString(names[0]) return this.createKeyFromString(key); } @Override void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException { BookKey persistent=value as BookKey st.setString(index,persistent?.getKey()) } @Override Object deepCopy(Object value) throws HibernateException { return value } @Override boolean isMutable() { return false //To change body of implemented methods use File | Settings | File Templates. } @Override Serializable disassemble(Object value) throws HibernateException { return value as Serializable } @Override Object assemble(Serializable cached, Object owner) throws HibernateException { return cached } @Override Object replace(Object original, Object target, Object owner) throws HibernateException { return original } } </code></pre> <p>Now BookKey objects are stored in database as Varchar, but when I get them, they are converted back to BookKey object.</p> <p>For more informations you can look here:</p> <p><a href="http://grails.org/doc/2.0.x/ref/Database%20Mapping/Usage.html" rel="nofollow">http://grails.org/doc/2.0.x/ref/Database%20Mapping/Usage.html</a></p> <p><a href="http://grails.1312388.n4.nabble.com/Working-example-of-user-type-td1377468.html" rel="nofollow">http://grails.1312388.n4.nabble.com/Working-example-of-user-type-td1377468.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload