Reputation: 705
I am doing One to One Relationship using JSP. I understand that One To One means,One object is associated with one object only.I created two tables with One to 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 to 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 to Parenttable
@OneToOne
@JoinColumn(name="id")
private Parenttable parenttable;
Is there any wrong while creating table with One To One relationship. If it's not I am trying to inserting data into 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("[email protected]");
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();
} finally {
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 all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.IntegrityException
Exception [EclipseLink-48] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Multiple writable mappings exist for the field [CHILDTABLE.ID]. Only one may be defined as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.OneToOneMapping[parenttable]
Descriptor: RelationalDescriptor(com.demo1.OneToOne.Childtable --> [DatabaseTable(CHILDTABLE)])
Upvotes: 1
Views: 2332
Reputation: 2663
Edit in parent table
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private int num;
//bi-directional one-to-one association to 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 to Parenttable
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private Parenttable parenttable;
Upvotes: 1