我想根据数据库中已有的值为对象生成自定义 ID。
我知道有人问了几个关于那个主题的问题,但我想不出一个解决方案...
这是我的类(class):
@Entity
class A {
// primary key for table
@GeneratedValue
@Id
private long tableId;
// id -> should be generated as (1+ (max id of type 'type'))
@Formula("1+(select t.id from mytable t where t.type=type)")
private long id;
// type
private String type;
}
我想到了@Formula 注释,但我无法让它工作...
引发异常:
java.sql.SQLException: Field 'id' doesn't have a default value
我不确定 @Formula
是好的解决方案...
有人知道我如何让它发挥作用吗?
非常感谢,
本
最佳答案
试试这个
@Formula(value = "(select t.id+1 from mytable t where t.type=type)")
private long id;
关于java - 使用带注释的 Hibernate 的复杂主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5978466/