开发者

Updating JPA entity fails on null value

I am using JPA (Hibernate) to store an entity on a MySQL 5.0 server.

Here is the entity in simplified form:

@Entity
@Table(name = "messages")
public class Message implements Serializable
{
    @Id
    @GeneratedValue
    @Column
    private long id;

    @Column
    private String content;

    @Column(insertable = false)
    private Date read;

    @Column(insertable = false)
    private Date deleted;
}

The columns "read" and "deleted" in table "messages" are defined so that they can contain NULL values. When I first tried to persist one of these entities I got an exception. Apparently, what Hibernate was doing there was listing the "read" and "deleted" columns in the column list of the insert statement but not in the value list. I got around that problem with the "insertable = false" statement in the @Column annotations you see above.

Now, however, I have a bigger problem. I want to set the read or date fields to non-null values. When I do that, I get a similar exception "You have an error in your SQL syntax". What he is doing now is listing all fields in the where part of the u开发者_JAVA技巧pdate statement, including "read" and "deleted". And what he does is check like "...and read=NULL". Which, in MySQL, of course should be "...and read IS NULL".

Rummaging around, I already found the "updatable" parameter for the @Column annotation. But if I set that to false, both "read" and "deleted" are never updated at all, so that is not what I am looking for, either.

...Help?


read is a reserved word in mysql and it looks like the driver is not escaping the name with back quotes. I think the best solution is to rename the column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜