What Is The Primary Key Generation Strategy? » My Blogging

What is the primary key generation strategy?

Let’s give table name as users and let’s configure primary key by using add identification and also specify the generated you know primary key generation strategy by using add generated value annotation and specify the strategy that is generation type identity all right so if you don’t specify at table annotation then by default jps smart enough to give a table name as a class name and you can map a field with a column by using add column annotation and we can give a name to the column by using add column annotation with the name attribute.

If you don’t specify add column annotation then GPS smart enough to give a name to the column as field name okay we can remove this at column annotation or if you want to make this column as not null then you can just pass nullable equals to pulse okay all right now we have created a simple JP entity let’s go to the repository package right-click new and choose the interface and let’s give interface name like user repository hit enter and let’s extend this interface from JPEG repository interface well we need to pass an entity type user here and then ID type is long, so JP repository interface is a generic interface.

We need to pass two parameters one is an entity type that is used and another is the ID of type long if you go to the user entity you can see here ID and we have added long as a type to ID and this long type we need to provide here okay just remember whenever our interface extends JP repository we will get accrued methods for this user entity and one more point is here we don’t have to add at repository annotation okay we don’t do we don’t have to add repository annotation to our repository interfaces because spring data JP already added repository annotation to you know default implementation class of JPEG repository.

Look at here simple JPEG repository class is a basic implementation of JPEG repository interface all right and simple JPEG repository is already annotated with at repository annotation hence we don’t need to add a repository annotation again to this interface okay great now let’s go to the service package right-click on service package new and then choose class and let’s do class name as user service okay and let’s annotate this class with at the read service annotation.

When we annotate the class with ad service annotation, so this annotation will you know automatically auto-detect this class while I know auto component scanning and this bin will be registered as a spring bin and remember whenever you create a service class make sure you annotate with ad service annotation so that this class can be recognized as a service class and we typically store business logic in service classes now let’s go ahead and let’s inject user repository here private user repository this is the field injection type okay you can also use a setter or constructor now I’m going to create a method public and then use the return type of the method is user method name is created user and just pass create a user.

I am going to simply return the user repository dot save method and we save user okay pretty simple isn’t if we have created a create user method which will basically save a user to a database and it will return the saved user basically this save method you know returns a saved entity okay great now let’s go to the controller right-click on controller new and then choose a class and let’s give a class name as user controller and let’s annotate this controller with the red rest controller annotation okay and let’s also add the request mapping annotation.

Let’s specify the base URL for this controller like API something like this and let’s create a simple retain point public a response entity and this is the generic type so we can pass a user as a type and the method name is create a user and I’m going to pass user object as a parameter to this method okay now let’s annotate this method with at the rate post mapping annotation so basically we are going to handle the post request that’s the reason we’re going to use post mapping annotation here and just define the response URL users and here we’re going to use add request body annotation to automatically map a JSON request to java object.

Now before that actually we have to inject the user service right next one let’s inject the first user service when I use the user service object in this method and then call save method or create user method and then pass the user object to this one and here we get the result of this method in some user object here to let’s rename this variable it already exists let’s say saved user and return a new object of response entity class and pause save user object in body and the status is HTTP status created pretty simple isn’t it so we use response entity class here to you know create HTTPS phones for this rest API.

This is a handy class provided by spring-boot so we can use this class to construct an HTTP response for the rest of APIs so always use a response entity as a response type to construct HTTP response well we have created a simple rest API now we have created a simple rest API that creates user dpi now it’s time to validate the user domain entity okay now let’s go ahead and let’s add bin validation annotation to name email and password to validate these fields so let’s say the client requirement to validate this name field is something like this username should not be you know null or empty and the second requirement is like username should have at least two characters.

Let’s say a client requirement is like this to validate the name so to adhere to this client requirement we’re going to use at not empty annotation to check whether a name is you on the ow name is null or not so make sure that you choose not empty you know annotation from java x dot validations dot constraints package now this annotation satisfies this requirement now the second requirement is users should have at least two characters.

To implement this requirement we’re gonna use add size annotation and size, annotation provides the attribute min attribute and you can specify min as do and we can also specify the custom validation message like the user should have at least two character something like this okay great now let’s go ahead and let’s add validation for email field let’s say the client requirement is like email should be a valid email format.

Related Posts

Leave a Reply

Your email address will not be published.