Skip to main content

Posts

Showing posts with the label Annotation

Hibernate : Make a field Non Updatable in an Entity

Points To Remember By default all the fields in the Hibernate Entity are updatable and insertable. To make a field non updatable in hibernate you nee to add the annotation  @Column(updatable = false) Make a field Non Updatable in an Hibernate Entity Suppose you have a User class and you want to make email as a non updatable field, all you need to do is add the  @Column(updatable = false)  annotation as shown in the example below. @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name = "email",updatable = false, nullable = false, unique = true) private String email; // GETTERS and SETTERS } Note : This only puts a hibernate level constraint , however you can still manually go and change the value of email by using db level queries.

Hibernate : How to use @Temporal annotation

Points To Remember @Temporal annotation must be used with the persistent fields or properties of type java.util.Date  java.util.Calendar How to use @Temporal annotation @Temporal Annotation is defined as following @Target({ METHOD, FIELD }) @Retention(RUNTIME) public @interface Temporal { /** * The type used in mapping <code>java.util.Date</code> or <code>java.util.Calendar</code>. */ TemporalType value(); } So the TemporalType is an Enum and looks like following public enum TemporalType { DATE, TIME, TIMESTAMP } So @Temporal annotation can take three values DATE, TIME and TIMESTAMP . And it will create the following database ddl type. TemporalType Database DDL Type DATE date TIMESTAMP datetime TIME time If we the following class as Hibernate Entity package com.ekiras.domian; import javax.persistence.*; import java.util.Date; @Entity public class User { @Id @SequenceGenerator(name = "test") @GeneratedValue(strategy = GenerationT...

Hibernate : How to use @Column Annotation

Points To Remember @Column annotation is used to define the column name, type, constraints. It should be used with the fields either above the getter or above field declaration. The @Column annotation can take the following configuration. Property Type Default Value Description name Optional "" The name of the column. Defaults to the property or field name unique Optional false Whether the column is a unique key. nullable Optional true Whether the database column is nullable. insertable Optional true Whether the column is included in SQL INSERT statements generated by the persistence provider. updatable Optional true Whether the column is included in SQL UPDATE statements generated by the persistence provider. columnDefinition Optional "" The SQL fragment that is used when generating the DDL for the column. table Optional "" The name of the table that contains the column. If absent the column is assumed to be in the primary table. length Optional 255 The...

Hibernate : How to use @Id annotation

Points to remember There should be only one @Id annotation in a hibernate entity class. There should be at least one field specified as primary id. You can use different approaches for creating primary id using these  Primary Key Generation Strategies. How to use @Id annotation in Hibernate Taking the example in the previous example .  @Id  annotation tells the hibernate to @Id annotation tells the hibernate that this field will be the primary key of the table. @Id annotation class looks like the following @Target({METHOD, FIELD}) @Retention(RUNTIME) public @interface Id { } If we do not add @Id annotation on any of the fields in Hibernate entity then you will get the following error. Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1249) at org.hibernate.jpa.boot.int...

Hibernate : How to use @Entity annotation

How to use @Entity annotation in Hibernate Suppose we want to make a User  domain with the following fields Id Name Age Address Gender Email Mobile We can create tell the hibernate to register this Domain class as a Hibernate Entity object by adding @Entity annotation  at the class level. package com.ekiras.domian; import com.ekiras.enums.Gender; import javax.persistence.Entity; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private Integer age; private String address; private Gender gender; private String email; private Long mobile; // GETTERS and SETTERS } So what the above line means is that, hibernate treats the class User as a Hibernate Persistence Entity. We will look at the  @Id and  @GeneratedValue annotations in a short while. @Entity annotation looks like the following @Documented @Target(TYPE) @Retention(RUNTIME) public @interface Entity...

Hibernate : @Table Annotation

Points To Remember @Table Annotation is an annotation that is used to give the table level information. It can be uesd to specify the following Table name  (optional,default ="")- to set the name of the table in database Table schema  (optional,default ="")- to set the schema of the table in database Table catalog  (optional,default ="") -to set the catalog of the table Table Constraints  (optional,default = []) to set the constraints on the table. It belongs to the package javax.persistence.Table @Table( name="person", schema="ekiras", uniqueConstraints = {@UniqueConstraint(columnNames = {"id","email"})} ) public class Person { private Long id; private String name; private String email; // code } If you do not define the name  property of the  @Table annotation then the hibernate would have created table by name Person  but now it will create the table by name person . The person  table will have the unique...

How to create a Custom Annotation in Java

Points To Remember An annotation can be used as a replacement of interfaces. You can make both empty/marker annotations and annotations with methods. You can define the retention policy for the annotation and where the annotation can be used. Program : Create A Custom Annotation The following program creates a custom empty annotation. import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.annotation.ElementType; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Table { public String name() default "table"; } The above class can be used as a custom annotation with only one method name with a default value. This annotation can be applied only to the class i.e it is class level annotation and can not be applied to any member of a class. The following program creates a custom annotation with methods defined. import java.lang.annotation.ElementType; import java.lang.ann...