java - One to One Mapping using JPA, While inserting data it's throwing an Exception -
i doing 1 one relationship using jsp. understand 1 one means,one object associated 1 object only.i created 2 tables 1 one relationsip. table pojo classes shown below.
parenttable:
@id @generatedvalue(strategy=generationtype.identity) private int id; private int num; //bi-directional one-to-one association childtable @onetoone(mappedby="parenttable") private childtable childtable;
childtable:
@id @generatedvalue(strategy=generationtype.identity) private int id; private string email; private string name; //bi-directional one-to-one association parenttable @onetoone @joincolumn(name="id") private parenttable parenttable;
is there wrong while creating table 1 one relationship. if it's not trying inserting data table.while inserting data it's throwing exception.
insert.java
public class insert { public static void main(string[] args) { parenttable parenttable = new parenttable(); childtable childtable=new childtable(); parenttable.setnum(123); childtable.setname("prabha"); childtable.setemail("prabha@gmail.com"); childtable.setparenttable(parenttable); entitymanagerfactory emf = persistence .createentitymanagerfactory("jpacrudapp"); entitymanager em = emf.createentitymanager(); try { entitytransaction entr = em.gettransaction(); entr.begin(); em.persist(childtable); entr.commit(); } catch (exception e) { e.printstacktrace(); } { em.close(); } } }
exception is:
exception in thread "main" javax.persistence.persistenceexception: exception [eclipselink-28019] (eclipse persistence services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.entitymanagersetupexception exception description: deployment of persistenceunit [jpacrudapp] failed. close factories persistenceunit. internal exception: exception [eclipselink-0] (eclipse persistence services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.integrityexception
descriptor exceptions:
exception [eclipselink-48] (eclipse persistence services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.descriptorexception exception description: multiple writable mappings exist field [childtable.id]. 1 may defined writable, others must specified read-only. mapping: org.eclipse.persistence.mappings.onetoonemapping[parenttable] descriptor: relationaldescriptor(com.demo1.onetoone.childtable --> [databasetable(childtable)])
edit in parent table
@id @generatedvalue(strategy=generationtype.identity) private int id; private int num; //bi-directional one-to-one association childtable @onetoone(fetch = fetchtype.lazy, mappedby = "parenttable", cascade = cascadetype.all) private childtable childtable;
edit in child table
@id @generatedvalue(strategy=generationtype.identity) private int id; private string email; private string name; //bi-directional one-to-one association parenttable @onetoone(fetch = fetchtype.lazy) @primarykeyjoincolumn private parenttable parenttable;
Comments
Post a Comment