Reputation: 3423
I wrote a simple class:
@Entity
@Table(name = "battle_log")
@SQLInsert(sql = "INSERT INTO battle_log (id, version, date_created, some_date) VALUES (?,?,?,?)", check = ResultCheckStyle.NONE)
@NamedQueries(value = {
@NamedQuery(name = "BattleLog.findAll",
query = "SELECT a FROM BattleLog a"),
@NamedQuery(name = "BattleLog.findAllBySomeDate",
query = "SELECT a FROM BattleLog a WHERE a.someDate = :someDate")
})
public class BattleLog implements Serializable {
private Long id;
private Integer version;
private Date someDate;
private Date created;
public BattleLog() {
created = new Date();
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
@Column(name = "version")
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Column(name = "date_created", nullable = false, columnDefinition = "timestamp")
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
@Column(name = "some_date", columnDefinition = "timestamp")
public Date getSomeDate() {
return someDate;
}
public void setSomeDate(Date someDate) {
this.someDate = someDate;
}
}
And a simple Bootstrap.groovy
BattleLog blog = new BattleLog(someDate: new Date())
blog.save()
println("should be saved now")
I'm doing it as a test project to see how Grails and Postgres partitioning work together. Everything is fine, if I comment out @SQLInsert code. When I do have it, I'm getting this exception:
Caused by: java.sql.SQLException: Wrong data type: java.lang.NumberFormatException: For input string: "2012-02-29 13:48:17.67"
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.setParameter(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.setTimestamp(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setTimestamp(DelegatingPreparedStatement.java:147)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setTimestamp(DelegatingPreparedStatement.java:147)
... 29 more
I'm not sure what can I do to make it work :/
Hope someone could help me.
Thanks, Krystian
EDIT:
I've tried using JodaTime, and after a little fight with dependencies I got it to run, however got errors like the one above.
I've also reverted back to using java.util.Date and used @Temporal(TemporalType.DATE)
and TIMESTAMP
but got an error [for date, similar for timestamp but with the time part]:
Caused by: java.sql.SQLException: Wrong data type: java.lang.NumberFormatException: For input string: "2012-02-29"
I am quite sure I have to add something to the SQLInsert
query, something that would convert string to a date. I can't, however, find anything.
Upvotes: 1
Views: 992
Reputation: 3423
Uhhh...
Everything because a dumb mistake.
SQLInsert should look like:
@SQLInsert(sql = "INSERT INTO battle_log (date_created, some_date, version) VALUES (?,?,?)", check = ResultCheckStyle.NONE)
That's without the ID
column!!
after removing it everything started to work just fine!
EDIT:
The above worked fine for HSQLDB. When I started to work with postgres I ended up again with some weird errors. In the end I had to add the ID, but... what's most important.. The ? ? ? ? values are added in the same order as the order in which they were created [in DDL statement].
Also, here's a link to a sample project where everything described is used:
https://github.com/krstns/grailsPostgresPartitioning
Upvotes: 1