Sometime, we would require to generate the value of a column in a table and we don't ask this value from the user. The user input form may not have the input field for that value. Another reason could be you need to change the input value with some logic and then you want to store it in a database. You may have seen the condition where the first time password to be generated automatically and is saved in database.
There are various approaches to mitigate the above requirements, like you could write your own logic while you are saving the data in the database. But, If you are using the hibernate, already there is the facility to do this.
Hibernate has the annotation @GeneratorType which allows you to tell to the hibernate how to generate the value of the field on which this annotation is written on.
@GeneratorType annotation
This annotation used to provide the custom generated value to the property. This annotation takes two parameters:
GenrationTime: which tells when this annotation would be effective.
ValueGenerator: class which would be used to generate the value.
Here is the example in which we want to generate the value of the field "enrollNumber" automatically in the student entity. We will write the generator class which would be implementation of the interface ValueGenerator.
class EnrollNumberGenerator implements ValueGenerator<String> {
@Override
public String generateValue( Session session, Object owner) {
Student s=(Student) owner;
return s.getName().toUpperCase()+System.currentTimeMillis();
}
}
Now, you could use this generator to generate the value of the enrollNumber property of the student entity.
@Entity
@Table(name="student")
public class Student {
@Id
private int id;
private String name;
private String email;
@GeneratorType( type = EnrollNumberGenerator.class, when = GenerationTime.INSERT)
private String enrollNumber;
public Student() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEnrollNumber() {
return enrollNumber;
}
public void setEnrollNumber(String enrollNumber) {
this.enrollNumber = enrollNumber;
}
}
Now, you could run the hibernate to test the solution.
package example.configuration.demo2;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import example.configuration.demo2.datamodel.Student;
public class MainClass {
public static void main(String[] args){
StandardServiceRegistry registry=new StandardServiceRegistryBuilder().configure().build();
SessionFactory factory=new MetadataSources(registry).buildMetadata().buildSessionFactory();
Session session=factory.openSession();
Transaction tx=session.beginTransaction();
Student student=new Student();
student.setId(1);
student.setName("John");
student.setEmail("john@gmail.com");
session.save(student);
tx.commit();
session.close();
}
}
Here is the table view where you could see the result of running the above program.