I am using Spring 3 and Hibernate 4
I have the following in Entity class
@Id
@Column(name = "PROJECT_NO")
private String projectNumber;
When I insert values into database table, is it possible to insert PROJECT_NO
value which is the Primary key in the following format
20131
where 2013 is the current year and next character should be incremental by one. ie next value to be inserted should be 20132
How can I achieve this using JPA/Hibernate
You have to look at @GeneratedValue and @GenericGenerator annotation.
There are several possibilities to generate value. In your case I believe the you need to create something like the following:
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "year_gen")
@GenericGenerator(name = "year_gen", strategy = "com.example.generator.CustomGenerator")
@Column(name = "PROJECT_NO")
private String projectNumber;
And CustomGenerator
should implement org.hibernate.id.IdentifierGenerator
What about putting the JPA annotation on the getter and using the getter method to format the data as you like?
@Entity
public class MyClass {
private String projectNumber;
@Column(name = "PROJECT_NO")
public String getProjectNumber(){
return doSomeFormatting(this.projectNumber);
}
}
Something like that should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.