Four ways to create a primary key in Liferay _liferay

Source: Internet
Author: User
Tags db2 postgresql
<! 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"/> <c Olumn name= "Lastmodifiedby" type= "Long"/> <column "name=" Lastmodifiedon "Date" type=/> <column "User"
		Id "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": In the use of a class to generate the primary, works for all Si Tuations id-type= "increment": Works for all databases supported but not in a clustered enviroment id-type= "identity": Wo Rks for DB2, MySQL, and MS SQL Server. Id-type= "id-type=" sequence "id-param=" id_sequence ": Works for DB2, Oracle, PostgreSQL, and SAP DB. Also LIFERAY-SERVICE-BUILDER_5_0_0.DTD Liferay-service-builder_5_1_0.dtd 6.1.DTD

See the DTD most clearly.


The Id-type and Id-param values are used in order to create a auto-generated, auto-incrementing primary key when Inserti NG records into a table. 
This can is implemented in 4 different ways and depending on the type of database being used. In all cases, the primary key of the model object should is assigned a value of NULL, and Hibernate'll know to replace The null value with an auto-generated, auto-incremented value.

If No id-type value is used, it's assumed that primary key would be assigned and not auto-generated. 

The implementation uses a class to generate a primary key. For example: <column name= "id" type= "Integer" primary= "true" Id-type= "class" id-param= "Com.li Feray.counter.service.persistence.IDGenerator "/> In this implementation," class specified in the Id-param value W  Ill be called to retrieve a unique identifier (in the example above, an Integer) that'll be used as the primary key for The new record. This Implementation supported databases. The second implementation generates identifiers that are a unique only if no other process was inserting data into the SAM E table.

This implementation should is used in a clustered environment, but it does work to all supported.  For example: <column name= "id" type= "Integer" primary= "true" id-type= "increment"/> The third

Implementation uses a identity column to generate a primary key. For example: <column name= ' id ' type= ' Integer ' primary= ' true ' id-type= ' identity '/> in ' this IM Plementation, the CREATE table SQL generated for this entity'll create an identity column that natively auto-generates A primary key whenever an insert occurs.

This implementation are 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 are created based on th e id-param value (stored in/sql/sequences.sql). This sequence are then accessed to generate a unique identifier whenever a insert occurs. This implementation are only supported by DB2, Oracle, PostgreSQL, and SAP DB.

So how to create a entity. For example, to create a user, you need to first set the primary key.

Public feed addfeed (feed feed) throws SystemException {
		long feedid = This.counterLocalService.increment ( Feed.class.getName ());
		Feed.setprimarykey (feedid);
		return super.addfeed (Feed);
}
Each of these tables corresponds to a counter

Or:

Long userId = Counterlocalservice.increment ();
The entire database corresponds to a counter


You can view the table counter





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.