JPA无法注解Timestamp

忧云 2010-01-30 12:01:41
用户类:User.java

package com.yoin.model;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* 用户
* @author 忧云
*
*/
@Entity(name="users")
public class User {
//用户ID
private Integer id;
//用户名
private String userName;
//密码
private String password;
//QQ
private String qq;
//电子邮件
private String email;
//性别
private String sex;
//等级
private Integer level=0;
//个人介绍
private String introduction;
//是否可用(1:可用,0:禁用)
private Integer visible=1;
//注册时间
private Timestamp createdTime;
//博文,(一对多关系映射)在多的一方维护
private List<Blog> blog = new ArrayList<Blog>();
//留言(一对多关系映射)在多的一方维护
private List<Comment> comment = new ArrayList<Comment>();
//类别(一对多关系映射)在多的一方维护
private List<Category> categorys = new ArrayList<Category>();


@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=200,nullable=false,name="username")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(length=200,nullable=false,name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="qq")
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
@Column(name="email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="sex")
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Column(length=1000,name="introduction")
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
@Column(name="level")
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
@Column(name="visible")
public Integer getVisible() {
return visible;
}
public void setVisible(Integer visible) {
this.visible = visible;
}
@Column(name="createdtime") @Temporal(TemporalType.TIMESTAMP)
public Timestamp getCreatedTime() {
return createdTime;
}
public void setCreatedTime(Timestamp createdTime) {
this.createdTime = createdTime;
}

@OneToMany(cascade = {CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH},mappedBy="user")
public List<Blog> getBlog() {
return blog;
}
public void setBlog(List<Blog> blog) {
this.blog = blog;
}

@OneToMany(cascade={CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.REFRESH},mappedBy="user")
public List<Comment> getComment() {
return comment;
}
public void setComment(List<Comment> comment) {
this.comment = comment;
}
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="user")
public List<Category> getCategorys() {
return categorys;
}
public void setCategorys(List<Category> categorys) {
this.categorys = categorys;
}
}



我用单元测试时出现下列异常:

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.yoin.model.User.createdTime
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
...
...
Caused by: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: com.yoin.model.User.createdTime
at org.hibernate.cfg.annotations.SimpleValueBinder.setType(SimpleValueBinder.java:93)
at org.hibernate.cfg.annotations.PropertyBinder.bind(PropertyBinder.java:120)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1667)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)


说是org.hibernate.AnnotationException只能注解a java.util.Date or java.util.Calendar这两个类,但是我在网上查过,jpa默认是可以注解Timestamp的啊,请大家帮忙看看,谢谢。
如果不能用Timestamp的话那JPA还有什么注册可以把创建时间类型精确到秒的?
...全文
824 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
忧云 2010-02-02
  • 打赏
  • 举报
回复
确实,我才刚起步设计,表改了三次,只是加上或减去一个属性,然后单元测试一下就行了。难道这样很验证????比xml难???
忧云 2010-01-30
  • 打赏
  • 举报
回复
应该说设成Date类型,把@Temporal(TemporalType.TIMESTAMP)就可以日期与时间一起用了,哈。。
忧云 2010-01-30
  • 打赏
  • 举报
回复
搞掂!用@Temporal(TemporalType.TIME),属性设为Date就行了。

结贴!
忧云 2010-01-30
  • 打赏
  • 举报
回复
换成Date是可以的,但是生成的时间精确到天,没有精确到秒啊。
leavin521 2010-01-30
  • 打赏
  • 举报
回复
private Timestamp createdTime 换成 private Date createdTime 试试
taigarz 2010-01-30
  • 打赏
  • 举报
回复
另外那个 getQq() 以前做这种类似站点的时候也遇到过
看的我脑壳子疼
忧云 2010-01-30
  • 打赏
  • 举报
回复
放在属性与get方法上都是可以的,但是可以的。我以前是用java.util.Data就能够自动生成表,但是时间只能够精确到日不能精确到秒,所以改用java.sql.Timestamp;但是这样子在jpa注解那里不能够生成表,抛的异常是org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property也就是说注解只能是用java.util.Date或者java.util.Calendar而不能用Timestamp,但是在jpa的提示里或上网查找到的都是可以用Timestamp而且还是默认的,但现在就是不能用,有谁知道怎么设置jpa注解时间属性精确到秒的啊。
谢谢~
xixihahahappy 2010-01-30
  • 打赏
  • 举报
回复
new java.sql.Timestamp(new java.util.Date().getTime()) 用这个方法试赋值试一下
BearKin 2010-01-30
  • 打赏
  • 举报
回复
我不知道是在哪本书上写的 把所有的JPA注解都放到了GET方法上 正确的做法应该是放到属性上

另外LZ的几个属性映射的不对 比如说 不可能会出现映射LIST的情况

如果说数据库里的字段类型datetime 那么在这里不需要映射成Timestamp 映射成 Date就行了(java.util.Date 完全可以啊)
Trinx 2010-01-30
  • 打赏
  • 举报
回复
楼主真辛苦半夜12点的帖子,先顶了。
dinghun8leech 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 bearkin 的回复:]
这位兄弟 XML的痛苦你不会明白。。
[/Quote]
BearKin 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 dinghun8leech 的回复:]
我想不通为何大家都喜欢用注释的方式来配置,这样有什么好处吗?我本人还是喜欢写xml。

如果哪天要改配置,那得重新修改类,再重新编译,到底哪种方式更便捷和一目了然?
[/Quote]
这位兄弟 XML的痛苦你不会明白。。
dinghun8leech 2010-01-30
  • 打赏
  • 举报
回复
我想不通为何大家都喜欢用注释的方式来配置,这样有什么好处吗?我本人还是喜欢写xml。

如果哪天要改配置,那得重新修改类,再重新编译,到底哪种方式更便捷和一目了然?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧