Tuesday, 15 April 2014

java - Entity with Inner class for building the entity -



java - Entity with Inner class for building the entity -

i trying out one-to-one mapping in jpa, here have taken relationship between pupil , contact, each pupil has contact.

i have create pupil entity follows,

@entity @table(name="tbl_student") public class pupil implements serializable{ public student(){ } @id @generatedvalue(strategy=generationtype.identity) @column(name="id") private integer studentid; @onetoone(targetentity=studentcontact.class,fetch=fetchtype.lazy) @joincolumn(name="contact_id") private studentcontact contact; .... .... .... }

now studentcontact entity follows,

@entity @table(name="tbl_std_contact") public class studentcontact extends serializable{ public studentcontact(){ } @id @column(name="id") @generatedvalue(strategy = generationtype.identity) private integer contactid; ... ... // properties mapped, public static class builder{ private integer contactid; private string phoneno; private string streetaddr; .... // properties same studentcontact public builder(string val){ this.city = val; } public builder setcontactid(integer contactid) { this.contactid = contactid; homecoming this; } // rest setter methods above, having homecoming type builder public studentcontact build(){ homecoming new studentcontact(this); } } private studentcontact(builder builder){ this.contactid = builder.contactid; this.city = builder.city; this.phoneno = builder.phoneno; ....... ... } }

in above studentcontact entity can see have created inner class builder, responsibility build studentcontact object using "build" method, can see in below mentioned studenttest class

now have written studenttest class has main method follows,

public class studenttest { public static void main(string [] args){ try{ studentdao dao = new studentdao(); pupil student = dao.getentity(110); studentcontact contact = new studentcontact.builder("bhubaneshwar") .setphoneno("9867342313") .setpincode("400392") .setstate("odhisha").build(); student.setcontact(contact); dao.updateentity(student); }catch(exception e){ e.printstacktrace(); } }

when run studenttest netbeans ide, gives error

exception in thread "main" java.lang.verifyerror: constructor must phone call super() or this() before homecoming in method com.entities.studentcontact.<init>()v @ offset 0

i not able understand error, whether error because inner class have created in studentcontact class,

how can solve this,

java.lang.verifyerror means bytecode not correct. can fixed total clean/rebuild of project. (i saw after package/class renaming, or class moving 1 bundle another).

as mentionned in comments : extends serializable not correct. (maybe cause of bytecode issue ?)

java hibernate jpa orm

No comments:

Post a Comment