您的位置:首页 > Web前端

liferay中四种创建主键的方法

2012-06-04 13:02 260 查看
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.0.0//EN"
"http://www.liferay.com/dtd/liferay-service-builder_5_0_0.dtd">
<service-builder package-path="com.linhopesolutions.canopy">
<namespace>Canopy</namespace>

<entity name="Overview" local-service="true" remote-service="false">

<!--
Use a class to generate the primary key.
-->
<column name="overviewId" type="int" primary="true" id-type="class"
id-param="com.liferay.counter.service.persistence.IDGenerator" />
<column name="text" type="String" />
<column name="published" type="boolean" />
<column name="lastModifiedBy" type="long" />
<column name="lastModifiedOn" type="Date" />
<column name="userId" type="long" />

<finder return-type="Collection" name="UserId">
<finder-column name="userId" />
</finder>

<reference package-path="com.liferay.portal.model" entity="User" />
</entity>
</service-builder>


Primay key field

There are four ways to create the primary key:

id-type="class" : You use a class to generate the primary, works for all situations
id-type="increment" : Works for all databases supported but not in a clustered enviroment
id-type="identity" : Works for DB2, MySQL, and MS SQL Server.
id-type="id-type="sequence" id-param="id_sequence" : Works for DB2, Oracle, PostgreSQL, and SAP DB.

See also

Liferay-service-builder_5_0_0.dtd
Liferay-service-builder_5_1_0.dtd
6.1.dtd

看DTD最清楚了。

The id-type and id-param values are used in order to create an auto-generated,
auto-incrementing primary key when inserting records into a table. This can be
implemented in 4 different ways, depending on the type of database being used.
In all cases, the primary key of the model object should be assigned a value of
null, and Hibernate will know to replace the null value with an auto-generated,
auto-incremented value. If no id-type value is used, it is assumed that the
primary key will be assigned and not auto-generated.

The first implementation uses a class to generate a primary key.

For example:

<column
name="id"
type="Integer"
primary="true"
id-type="class"
id-param="com.liferay.counter.service.persistence.IDGenerator"
/>

In this implementation, the class specified in the id-param value will be called
to retrieve a unique identifier (in the example above, an Integer) that will be
used as the primary key for the new record. This implementation works for all
supported databases.

The second implementation generates identifiers that are unique only when no
other process is inserting data into the same table. This implementation should
NOT be used in a clustered environment, but it does work for all supported
databases.

For example:

<column
name="id"
type="Integer"
primary="true"
id-type="increment"
/>

The third implementation uses an identity column to generate a primary key.

For example:

<column
name="id"
type="Integer"
primary="true"
id-type="identity"
/>

In this implementation, the create table SQL generated for this entity will
create an identity column that natively auto-generates a primary key whenever
an insert occurs. This implementation is only supported by DB2, MySQL, and
MS SQL Server.

The fourth implementation uses a sequence to generate a primary key.

For example:

<column
name="id"
type="Integer"
primary="true"
id-type="sequence"
id-param="id_sequence"
/>

In this implementation, a create sequence SQL statement is created based on
the id-param value (stored in /sql/sequences.sql). This sequence is then
accessed to generate a unique identifier whenever an insert occurs. This
implementation is only supported by DB2, Oracle, PostgreSQL, and SAP DB.


那么怎么去创建一个entity了?比如创建User,你需要先给它设置主键。

public Feed addFeed(Feed feed) throws SystemException {
long feedId = this.counterLocalService.increment(Feed.class.getName());
feed.setPrimaryKey(feedId);
return super.addFeed(feed);
}
这个每一个表对应一个counter

或者:

long userId = counterLocalService.increment();
整个数据库对应一个counter

可以查看表Counter
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: