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.dtdLiferay-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
相关文章推荐
- ExecutorService创建线程池的四种方法
- java创建对象的四种方法
- 网络编程(43)—— windows平台下创建线程的四种方法(二)之用户界面线程
- iOS ViewController的四种创建方法
- JavaScript创建对象的四种方法
- sql server创建复合主键的2种方法
- Java 创建对象的四种方法
- 四种创建对象的方法
- Postgresql 创建主键并设置自动递增的三种方法
- java创建线程池的四种方法
- C语言数据结构-创建链表的四种方法
- Java中创建对象的四种方法
- Java创建并执行线程的四种方法
- C++之二叉树的创建、查找、四种次序的周游遍历方法以及内存的释放
- SQL2005创建表时主键问题解决方法
- java创建对象的四种方法
- Postgresql 创建主键并设置自动递增的三种方法
- sql server创建复合主键的2种方法
- Java创建对象的四种方法
- Java中创建对象的四种方法