Page 1 of 1

IdGenerationService - Adding new entity

Posted: Sat Jun 28, 2014 2:05 pm
by gowthamgutha
I have seen this in the CustomerServiceImpl.java

Code: Select all

return idGenerationService.findNextId("org.broadleafcommerce.profile.core.domain.Customer");


I would like to know if I really could use the same method for my custom entity something like..

com.mycompany.core.catalog.domain.MyCustomer by simply doing the following..

Code: Select all

return idGenerationService.findNextId("com.mycompany.core.catalog.domain.MyCustomer");


or do I need to do anything else apart from that?

Thanks in advance. Hope you will reply as soon as possible.

Re: IdGenerationService - Adding new entity

Posted: Sun Jun 29, 2014 8:48 pm
by phillipuniverse
Customers work that way specifically so that customers do not have to be persisted immediately but can be later persisted into the database.

Customer IDs are a very one-off case and 99% of the time you should use the way IDs are generated in the rest of the application. Take ProductImpl for instance:

Code: Select all

@Id
@GeneratedValue(generator"ProductId")
@
GenericGenerator(
    
name="ProductId",
    
strategy="org.broadleafcommerce.common.persistence.IdOverrideTableGenerator",
    
parameters = {
        @
Parameter(name="segment_value"value="ProductImpl"),
        @
Parameter(name="entity_name"value="org.broadleafcommerce.core.catalog.domain.ProductImpl")
    }
)
@
Column(name "PRODUCT_ID")
protected 
Long id;
 


This will use a 'ProductImpl' key in the SEQUENCE_GENERATOR table to obtain IDs. This is what we recommend for ID generation for new entities.

Re: IdGenerationService - Adding new entity

Posted: Mon Jun 30, 2014 8:19 am
by gowthamgutha
So, just adding the entity with a name and with a val in the SEQUENCE_GENERATOR table will be enough and then marking it as in the ProductImpl with annotations.

Do I need anything apart from that?

By the way, what is ID_VAL in that table?

Thanks in advance. Hope you will reply as soon as possible.

Re: IdGenerationService - Adding new entity

Posted: Mon Jun 30, 2014 11:34 am
by phillipuniverse
Yes, just adding that annotation just like in ProductImpl with obviously changed values for the annotation is enough. ID_VAL is what Hibernate uses to determine the actual sequence number.

Re: IdGenerationService - Adding new entity

Posted: Mon Jun 30, 2014 3:06 pm
by gowthamgutha
What is this ID_VAL? Is it the starting number or the difference between two successive ids?