Grails: how to store jScience Amounts

275 views Asked by At

I am trying to store jscience physics amounts in my grails project. I am using hibernate and defaults, but I would prefer something with general applicability. I am specifically concerned with Mass and if I could have my way I would just make a line in the domain object like so:

MyDomainClass {
  Amount<Mass> weight
}

But that gives this error:

Caused by: org.hibernate.exception.DataException: could not insert: [project.MyDomainClass] at $Proxy10.saveOrUpdate(Unknown Source) ... 27 more Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'weight' at row 1 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3601) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3535) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1989) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2150) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2333) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2318) at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105) ... 28 more

I can fix that error by changing it to

def weight

Is there anything wrong with that, or a better way? Is there any database solution with advantages for dealing with mixed units? Googling this is so frustrating because everything is about unit testing.

1

There are 1 answers

5
Burt Beckwith On BEST ANSWER

You only hid the error messages with def weight - that made the field non-persistent. Since it's not typed, Grails and Hibernate don't know how to persist it and ignore it.

My guess is that it was storing the object as a blob and it was too big for the default size, but that's probably not what you want. Look at http://grails.org/doc/latest/guide/GORM.html#customHibernateTypes for how to map them in your domain class.

The link in the Grails doc to the Hibernate docs is broken - the correct url is http://docs.jboss.org/hibernate/stable/core/manual/en-US/html/mapping.html#mapping-types-custom

jScience may already have custom Hibernate types (either in the distro or from a 3rd-party) - you should check their docs.