Hibernate Annotations: Store an int in a varchar column -
i'm using hibernate annotations.
in pojo have year field int.
i persist value in char(4) column in db, , have hibernate convert types , forth. there anyway can (i started @type annotation, don't want have write own custom type if possible)?
if pojo 's field mapped db 's char(4) column access property , hibernate call setter , getter mapping between database , pojo . , conversion logic can implemented inside setter , getter of property. besides , intdate should mark @transient tell hibernate ignore mapping field.
public class tableabc { private int id; private int intdate; @id @generatedvalue public int getid() { return id; } public void setid(int id) { this.id = id; } @column(length=4) private string getchardate() { return string.valueof(this.intdate); } private void setchardate(string chardate) { try { this.intdate = integer.parseint(chardate); } catch (numberformatexception e) { //logic handle when chardate cannot convert integer this.intdate = 0; } } @transient public int getintdate() { return intdate; } public void setintdate(int intdate) { this.intdate = intdate; } }
Comments
Post a Comment