留言板

Service builder and auto-increment

Guillaume Saint-Raymond,修改在14 年前。

Service builder and auto-increment

New Member 发布: 1 加入日期: 10-1-28 最近的帖子
Hello,

I have created an entity which have an auto-incremented primary key (criterionId) :
<entity name="ACMCommunityCriterion" uuid="true" local-service="true">
	<column name="criterionId" type="long" primary="true" id-type="identity" />
	<column name="typeId" type="long" /> <!-- mapping-key="typeId" entity="ACMCommunityCriterionType"/-->
	<column name="value" type="long" />
	<column name="communities" type="Collection" entity="ACMCommunity" mapping-table="ACMCommunity_Criterion" />
	<finder name="TypeAndValue" return-type="ACMCommunityCriterion" unique="true">
		<finder-column name="typeId" />
		<finder-column name="value" />
	</finder>
</entity>


To instanciate a new entry, I use the ...LocalServiceUtil and create a new instance with ID 0 :
criterion = ACMCommunityCriterionLocalServiceUtil.createACMCommunityCriterion(0);
criterion.setTypeId(typeId);
criterion.setValue(value);
ACMCommunityCriterionLocalServiceUtil.updateACMCommunityCriterion(criterion);;


The problem is that after calling updateACMCommunityCriterion method, criterion.getCriterionId still return 0 and not the id auto-incremented stored in MySQL.

So, to get the Id, I have to do ACMCommunityCriterionLocalServiceUtil.getCriterion... to get the instance with the right criterionId.

Is there anything am i doing wrong ? Is it a normal behaviour ?

Regards,

Guillaume Saint-Raymond
Pavel Shuvaev,修改在11 年前。

RE: Service builder and auto-increment

New Member 帖子: 13 加入日期: 12-6-7 最近的帖子
Hello, Guillaume Saint-Raymond. I have a similar problem. You resolved this question? If so, please, tell
Siby Mathew,修改在11 年前。

RE: Service builder and auto-increment

Expert 帖子: 268 加入日期: 11-3-4 最近的帖子
Hi Pavel,
When you create, you can pass CounterLocalServiceUtil.increment() as the parameter. This will return the autoIncrement value.
Is that what you are looking for ?

Thanks,
Siby
Pavel Shuvaev,修改在11 年前。

RE: Service builder and auto-increment

New Member 帖子: 13 加入日期: 12-6-7 最近的帖子
Siby Mathew, when I use CounterLocalServiceUtil.increment(), I get a number that is not recorded in the database as a key
Siby Mathew,修改在11 年前。

RE: Service builder and auto-increment

Expert 帖子: 268 加入日期: 11-3-4 最近的帖子
Hi Pavel,
You will of course get a new value when you use CounterLocalServiceUtil.increment().
This is because you are using this number as the parameter for creating the new entity.
After you update, this id will be present in the database.

ThaNks,
Siby
Pavel Shuvaev,修改在11 年前。

RE: Service builder and auto-increment

New Member 帖子: 13 加入日期: 12-6-7 最近的帖子
Code:


long dId;
DictionaryRegion reg;

       dId = CounterLocalServiceUtil.increment( DictionaryRegion.class.getName() );
       reg = DictionaryRegionLocalServiceUtil.createDictionaryRegion(dId);
       DictionaryRegionLocalServiceUtil.addDictionaryRegion( reg );

        reg.setRegionName( "abc" );

        reg = DictionaryRegionLocalServiceUtil.updateDictionaryRegion(reg);


It does work, great! It turns out, after the creation of the essence by createDictionaryRegionit still needs to add a method addDictionaryRegion
Thank you, Siby Mathew!
thumbnail
Daniel Martínez Cisneros,修改在8 年前。

RE: Service builder and auto-increment

Junior Member 帖子: 38 加入日期: 11-7-1 最近的帖子
I suggest this possibility instead of increment manually:

<entity name="Student" local-service="true" cache-enabled="false" table="student">
        <column primary="true" name="studentId" type="long" [b]id-type="increment" [ b]></column>
        <column name="name" type="String"></column>
        <column name="lastname" type="String"></column>
        <column name="std" type="String"></column>
        <column name="address" type="String"></column>
        <column name="phone" type="String"></column>
        <column name="gender" type="int"></column>
    </entity>



You can define id-type: increment as .dtd define.

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.

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