如题,主键类型为 varchar,应选用那种主键策略呢?
- Java code
@Id @GeneratedValue(generator = "persistenceGenerator") @GenericGenerator(name = "persistenceGenerator", strategy = "increment")
- Java code
@Id @GeneratedValue(strategy=GenerationType.AUTO)
- Java code
@Id @GeneratedValue(strategy=GenerationType.identity)
以上都试过,不行
------解决方案--------------------
pirvate Long id;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public Long getId(){
return this.id;
}
------解决方案--------------------
需要在主键上增加一个@Id标识
view plaincopy to clipboardprint?
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
GenerationType 类型的枚举值,它的内容将指定 OpenJPA 容器自动生成实体标识的方式。有 GeneratorType.AUTO,GenerationType.IDENTITY,GenerationType.SEQUENCE, GenerationType.TABLE 四种方式
@GeneratedValue(strategy = GenerationType.IDENTITY)
一般可以根据你采取的底层数据库来选择,比如是用oracle,id用sequence的话,可以采取sequence方式,如果是用mysql,一般就用auto模式
// 默认相当于native ID生成策略,JPA 1.0 中只有4个可选值
// 如果只写@Id ,主键字段不会自动产生,需要我们手动输入
// @GeneratedValue 默认为策略产生ID
// 如果我要让ID为IDENTITY 则需要手动给他指定值--@GeneratedValue(strategy=GenerationType.IDENTITY)
这样选择了主键生成策略,在给实体赋值时就不用设置ID值了。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shendaiming/archive/2010/03/09/5359536.aspx
------解决方案--------------------
varchar的用uuid.hex自增的
------解决方案--------------------
------解决方案--------------------
@Id
@GeneratedValue(generator = "system-uuid")[该属性名自定义]
@GenericGenerator(name = " system-uuid ", strategy = "uuid")
[name可自定义,但是strategy属性必须是Hibernate中有效的主键策略]
@Column (name = "user_id")
------解决方案--------------------
JavaCode:
- Java code
1. @GeneratedValue(generator = "system-uuid") 2. @GenericGenerator(name = "system-uuid", strategy = "uuid")
------解决方案--------------------
UUID
------解决方案--------------------
@Id
@SequenceGenerator(name = "SEQ_AM_LOGISTICS_BILL", sequenceName = "SEQ_AM_LOGISTICS_BILL", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_AM_LOGISTICS_BILL")
@Column(name = "LOGISTICS_BILL_ID")
------解决方案--------------------
varchar怎么自增?
只能用自己定义的TableCustomerizeID了,需要实现一个Hibernate中的接口类
------解决方案--------------------
@GenericGenerator(name = "generator", strategy = "uuid.hex")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false, length = 32)