如何将Long id new java.util.Date()。getTime()设置为@GeneratedValue

时间:2016-04-30 18:18:23

标签: java hibernate

如何将主键Long id = new java.util.Date()。getTime()设置为@GeneratedValue?

我的代码:

private Long id;
@Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(nullable = false, name = "id")
    public Long getId() {
        return id;
    }

感谢。

1 个答案:

答案 0 :(得分:2)

你尝试使用自定义生成器,比如;

package x.y.z;
public class LongGenerator implements IdentifierGenerator { 

        @Override 
        public Serializable generate(final SessionImplementor sessionImplementor, final Object object) throws HibernateException { 
            return new java.util.Date().getTime(); 
        }
}

    private Long id;
    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "x.y.z.LongGenerator")
    @Column(nullable = false, name = "id")
    public Long getId() {
        return id;
    }