设置JPA时间戳列由数据库生成的? [英] Setting a JPA timestamp column to be generated by the database?

查看:1032
本文介绍了设置JPA时间戳列由数据库生成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的SQL Server 2000数据库,我有一个时间戳(在功能上没有数据类型)命名lastTouched设置为datetime类型的列(GETDATE())作为其默认值/绑定。

In my SQL Server 2000 database, I have a timestamp (in function not in data type) column of type datetime named lastTouched set to (getdate()) as its default value/binding.

我使用NetBeans 6.5生成的JPA实体类,并有这在我的code

I am using the Netbeans 6.5 generated JPA entity classes, and have this in my code

@Basic(optional = false)
@Column(name = "LastTouched")
@Temporal(TemporalType.TIMESTAMP)
private Date lastTouched;

然而,当我试图把该对象到数据库中,我得到,

However when I try to put the object into the database I get,

javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value: com.generic.Stuff.lastTouched

我已经尝试设置@Basic设置(可选= true),但却是抛出一个异常说数据库不允许空值时间戳列,它被设计不。

I've tried setting setting the @Basic to (optional = true), but that throws a exception saying the database doesn't allow null values for the timestamp column, which it doesn't by design.

ERROR JDBCExceptionReporter - Cannot insert the value NULL into column 'LastTouched', table 'DatabaseName.dbo.Stuff'; column does not allow nulls. INSERT fails.

我previously得到这个纯Hibernate的工作,但我有感觉切换到JPA和不知道如何告诉它该列是假设要在数据库端生成。请注意,我仍在使用Hiberate作为我JPA持久化层。

I previously got this to work in pure Hibernate, but I have sense switched over to JPA and have no idea how to tell it that this column is suppose to be generated on the database side. Note that I am still using Hiberate as my JPA persistance layer.

推荐答案

我通过改变code到

@Basic(optional = false)
@Column(name = "LastTouched", insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date lastTouched;

所以生成SQL插入时,timestamp列被忽略。不知道这是去对此的最佳途径。反馈是受欢迎的。

So the timestamp column is ignored when generating SQL inserts. Not sure if this is the best way to go about this. Feedback is welcome.

这篇关于设置JPA时间戳列由数据库生成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆