Dashboard > Hibernate Annotations Reference > Entity Bean 2.1-2.3 > Mapping identifier properties
Hibernate Annotations Reference Log In   View a printable version of the current page.
Mapping identifier properties
Added by 菠菜, last edited by 菠菜 on Mar 22, 2006  (view change)
Labels: 
(None)

Mapping identifier properties

The @Id annotation lets you define which
property is the identifier of your entity bean. This property can be set
by the application itself or be generated by Hibernate (preferred). You
can define the identifier generation strategy thanks to the
@GeneratedValue annotation:

使用@Id注解可以将实体bean中的某个属性定义为标识字段.
该标识属性的值可以通过应用自身进行设置,
也可以通过Hiberante生成(推荐).
使用 @GeneratedValue注解可以定义标识字段的生成策略.

AUTO - either identity column, sequence or table depending on the underlying DB

TABLE - table holding the id

IDENTITY - identity column

SEQUENCE - sequence

AUTO - 可以是identity类型的字段,或者sequence类型或者table类型,取决于不同的底层数据库.

TABLE - 使用表保存id值

IDENTITY - identity字段

SEQUENCE - sequence

Hibernate provides more id generators than the basic EJB3 ones.
Check for more informations.

和EJB3规范相比,Hibernate提供了更多的id生成器.详情请查阅 .

The following example shows a sequence generator using the
SEQ_STORE configuration (see below)
下面的例子展示了使用SEQ_STORE配置的sequence生成器

programlisting

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
public Integer getId() { ... }

The next example uses the identity generator:
下面这个例子使用的是identity生成器

programlisting

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... }

The AUTO generator is the preferred type for
portable applications (across several DB vendors). The identifier
generation configuration can be shared for several
@Id mappings with the generator attribute. There are
several configurations available through
@SequenceGenerator and
@TableGenerator. The scope of a generator can be the
application or the class. Class-defined generators are not visible
outside the class and can override application level generators.
Application level generators are defined at package level (see
package-info.java):

AUTO生成器适用于可移植的应用(在多个DB间切换).
identifer生成配置信息可以通过几个@Id进行共享.
通过@SequenceGenerator@TableGenerator可提供好几个配置.
generators的应用范围可以是应用级和class级.
class级的generator在外部是不可见的,
而且class级的generator可以覆盖应用级的generator。
应用级的generator则定义在包一级(如package-info.java):

programlisting

@javax.persistence.TableGenerator(
name="EMP_GEN",
table="GENERATOR_TABLE",
pkColumnName = "key",
valueColumnName = "hi"
pkColumnValue="EMP",
allocationSize=20
)
@javax.persistence.SequenceGenerator(
name="SEQ_GEN",
sequenceName="my_sequence"
)
package org.hibernate.test.metadata;

If package-info.java in the
org.hibernate.test.metadata package is used to
initialize the EJB configuration, EMP_GEN and
SEQ_GEN are application level generators.
EMP_GEN defines a table based id generator using the
hilo algorithm with a max_lo of 20. The hi value is
kept in a table "GENERATOR_TABLE".
The information is kept in a row where pkColumnName
"key" is equals to pkColumnValue
"EMP" and column valueColumnName
"hi" contains the the next high value used.

如果在org.hibernate.test.metadata包下面的
package-info.java文件用于初始化EJB配置,
那么该文件中定义的 EMP_GEN
SEQ_GEN都是系统级的generators.
EMP_GEN定义了一个使用hilo算法(max_lo为20)的id generator,该id
generator基于表存储id的信息.id的hi值保存在GENERATOR_TABLE中.
在该表中 pkColumnName"key"等价于
pkColumnValue "EMP",
valueColumnName "hi"中存储的是下一个要使用的最大值.

SEQ_GEN defines a sequence generator using a
sequence named my_sequence. Note that this version of
Hibernate Annotations does not handle initialValue
and allocationSize parameters in the sequence
generator.

SEQ_GEN则定义了一个sequence generator,
其对应的sequence名为 my_sequence.
注意目前Hibernate Annotations还不支持sequence generator中的
initialValueallocationSize参数.

The next example shows the definition of a sequence generator in a
class scope:

下面这个例子展示了定义在class级的sequence generator:

programlisting

@Entity
@javax.persistence.SequenceGenerator(
name="SEQ_STORE",
sequenceName="my_sequence"
)
public class Store implements Serializable {
private Long id;

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
public Long getId() { return id; }
}

This class will use a sequence named my_sequence and the SEQ_STORE
generator is not visible in other classes. Note that you can check the
Hibernate Annotations tests in the org.hibernate.test.metadata.id
package for more examples.

在这个例子中,Store类使用名为my_sequence的sequence,并且SEQ_STORE generator对于其他class是不可见的.注意在org.hibernate.test.metadata.id包下的测试代码有更多演示Hibernate Annotations用法的例子..

You can define a composite primary key through several
syntaxes:
下面是定义组合主键的几种语法:

annotate the component property as @Id and make the component class @Embeddable

annotate the component property as @EmbeddedId

annotate the class as @IdClass and annotate each property of the entity involved in the primary key with @Id

将组件类注解为@Embeddable,并将组件的属性注解为@Id

将组件的属性注解为@EmbeddedId

将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id

While quite common to the EJB2 developer,
@IdClass is likely new for Hibernate users. The
composite primary key class corresponds to multiple fields or properties
of the entity class, and the names of primary key fields or properties
in the primary key class and those of the entity class must match and
their types must be the same. Let's look at an example:

对于EJB2的开发人员来说 @IdClass是很常见的,
但是对于HIbernate的用户来说就是一个崭新的用法.
组合主键类对应了一个实体类中的多个字段或属性,
而且主键类中用于定义主键的字段或属性和
实体类中对应的字段或属性在类型上必须一致.下面我们看一个例子:

programlisting

@Entity
@IdClass(FootballerPk.class)
public class Footballer {
//part of the id key
@Id public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

//part of the id key
@Id public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getClub() {
return club;
}

public void setClub(String club) {
this.club = club;
}

//appropriate equals() and hashCode() implementation
}

@Embeddable
public class FootballerPk implements Serializable {
//same name and type as in Footballer
public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

//same name and type as in Footballer
public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}

//appropriate equals() and hashCode() implementation
}

As you may have seen, @IdClass points to the
corresponding primary key class.
如上, @IdClass指向对应的主键类.

二审后的建议 

1.The <literal>@Id</literal> annotation lets you define which property is the identifier of your entity bean.
  使用<literal>@Id</literal>注解可以将实体bean中的某个属性定义为标识属性

修改原因:和下文的'标识属性'保持一致。

2。You can define the identifier generation strategy thanks to the <literal>@GeneratedValue</literal> annotation:
使用 <literal>@GeneratedValue</literal>注解可以定义标识属性的生成策略.

修改原因:和上文的'标识属性'保持一致。

3。The identifier generation configuration can be shared for several <literal>@Id</literal> mappings with the generator attribute.

多个<literal>@Id</literal>可以共享同一个identifier生成器,只要把 'generator' 设成相同的值就可以了。

 修改原因:我觉得上一个版本翻译的不太准确。

4。There are several configurations available through <literal>@SequenceGenerator</literal> and
      <literal>@TableGenerator</literal>.

通过<literal>@SequenceGenerator</literal> 和<literal>@TableGenerator</literal>,你可以配置不同的identifier生成器.

 5。The scope of a generator can be the application or the class.

每一个identifier生成器都有自己的适用范围,它可以为整个应用服务,也可以只为某个类服务。 

修改原因:我觉得上一个版本翻译的不太准确。

6。Class-defined generators are not visible outside the class and can override application level generators.

class级的生成器在外部是不可见的,而且class级的生成器可以覆盖应用级的生成器

修改原因:我建议把'generator翻译成'生成器',而在一审的版本中很多处generator都没有翻译,在这里我没有全部列出,请自行选择。

7。Application level generators are defined at package level (see <classname>package-info.java</classname>)

应用级的生成器定义在包一级(如<classname>package-info.java</classname>)

8. <literal>EMP_GEN</literal> and <literal>SEQ_GEN</literal> are application level generators

<literal>EMP_GEN</literal> 和<literal>SEQ_GEN</literal>都是应用级的生成器

9。<literal>EMP_GEN</literal> defines a table based id generator using the hilo algorithm with a <literal>max_lo</literal> of 20.

<literal>EMP_GEN</literal>定义了一个使用hilo算法(max_lo为20)的id 生成器,该生成器将id的信息存在数据库的某个表中。

10。The information is kept in a row where <literal>pkColumnName</literal>
      "key" is equals to <literal>pkColumnValue</literal>
      "<literal>EMP</literal>" and column <literal>valueColumnName</literal>
      "<literal>hi</literal>" contains the the next high value used.

  在该表中 <literal>pkColumnName</literal>"key"等价于 <literal>pkColumnValue</literal> "<literal>EMP</literal>", 而<literal>valueColumnName</literal> "<literal>hi</literal>"中存储的是下一个要使用的最大值.

这一段的英文我没看懂。。 

 11。While quite common to the EJB2 developer, <literal>@IdClass</literal> is likely new for Hibernate users.

 对于EJB2的开发人员来说 <literal>@IdClass</literal>是很常见的, 但是对于Hibernate的用户来说就是一个崭新的用法.

12. The composite primary key class corresponds to multiple fields or properties of the entity class
组合主键类对应了一个实体类中的多个字段或属性

 我还不清楚 field和 property有什么区别,不如统统翻译成'属性'?注意在下文还有几处'字段'出现。

Posted by Song Guoqiang at Mar 19, 2006 18:29 | Permalink

嘿嘿,field和property还是有区别的,field是物理级的,就是真正类属的instance variable,property应该是可通过getter/setter访问的,getter/setter的名称未必和field的名称保持一,并且未必所有的getter/setter就一定有对应的field。

Posted by Anonymous at Mar 19, 2006 22:11 | Permalink

 

1.The <literal>@Id</literal> annotation lets you define which property is the identifier of your entity bean.
  使用<literal>@Id</literal>注解可以将实体bean中的某个属性定义为标识属性

修改原因:和下文的'标识属性'保持一致。

2。You can define the identifier generation strategy thanks to the <literal>@GeneratedValue</literal> annotation:
使用 <literal>@GeneratedValue</literal>注解可以定义标识属性的生成策略.

修改原因:和上文的'标识属性'保持一致。

R:1和2统一回复为

该标识属性的值可以通过应用自身进行设置,-》该属性的值可以通过应用自身进行设置,

3。The identifier generation configuration can be shared for several <literal>@Id</literal> mappings with the generator attribute.

多个<literal>@Id</literal>可以共享同一个identifier生成器,只要把 'generator' 设成相同的值就可以了。

 修改原因:我觉得上一个版本翻译的不太准确。

R:accepted

4。There are several configurations available through <literal>@SequenceGenerator</literal> and
      <literal>@TableGenerator</literal>.

通过<literal>@SequenceGenerator</literal> 和<literal>@TableGenerator</literal>,你可以配置不同的identifier生成器.

R:accepted

 5。The scope of a generator can be the application or the class.

每一个identifier生成器都有自己的适用范围,它可以为整个应用服务,也可以只为某个类服务。 

修改原因:我觉得上一个版本翻译的不太准确。

R:"它可以为整个应用服务,也可以只为某个类服务"这一句保持上一个版本

6。Class-defined generators are not visible outside the class and can override application level generators.

class级的生成器在外部是不可见的,而且class级的生成器可以覆盖应用级的生成器

修改原因:我建议把'generator翻译成'生成器',而在一审的版本中很多处generator都没有翻译,在这里我没有全部列出,请自行选择。

R:accepted

7。Application level generators are defined at package level (see <classname>package-info.java</classname>)

应用级的生成器定义在包一级(如<classname>package-info.java</classname>)

R:accepted

8. <literal>EMP_GEN</literal> and <literal>SEQ_GEN</literal> are application level generators

<literal>EMP_GEN</literal> 和<literal>SEQ_GEN</literal>都是应用级的生成器

R:accepted

9。<literal>EMP_GEN</literal> defines a table based id generator using the hilo algorithm with a <literal>max_lo</literal> of 20.

<literal>EMP_GEN</literal>定义了一个使用hilo算法(max_lo为20)的id 生成器,该生成器将id的信息存在数据库的某个表中。

R:accepted

10。The information is kept in a row where <literal>pkColumnName</literal>
      "key" is equals to <literal>pkColumnValue</literal>
      "<literal>EMP</literal>" and column <literal>valueColumnName</literal>
      "<literal>hi</literal>" contains the the next high value used.

  在该表中 <literal>pkColumnName</literal>"key"等价于 <literal>pkColumnValue</literal> "<literal>EMP</literal>", 而<literal>valueColumnName</literal> "<literal>hi</literal>"中存储的是下一个要使用的最大值.

这一段的英文我没看懂。。 

 11。While quite common to the EJB2 developer, <literal>@IdClass</literal> is likely new for Hibernate users.

 对于EJB2的开发人员来说 <literal>@IdClass</literal>是很常见的, 但是对于Hibernate的用户来说就是一个崭新的用法.

R:accepted

12. The composite primary key class corresponds to multiple fields or properties of the entity class
组合主键类对应了一个实体类中的多个字段或属性

 我还不清楚 field和 property有什么区别,不如统统翻译成'属性'?注意在下文还有几处'字段'出现。

R:还是有区别的,保持上一个版本

Posted by 菠菜 at Mar 27, 2006 10:20 | Permalink
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.1.3 Build:#408 Jan 23, 2006) - Bug/feature request - Contact Administrators