我想在关系的一侧插入数据
这是我的父实体
public class Parent implements Serializable{
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String pfirstName;
private String plastName;
private int parentAge;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPfirstName() {
return pfirstName;
}
public void setPfirstName(String pfirstName) {
this.pfirstName = pfirstName;
}
public String getPlastName() {
return plastName;
}
public void setPlastName(String plastName) {
this.plastName = plastName;
}
public int getParentAge() {
return parentAge;
}
public void setParentAge(int parentAge) {
this.parentAge= parentAge;
}
}
这是我的子实体
查看plaincopy到剪贴板打印?注意:代码块中的文本内容会自动换行
public class Child implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String cfirstName;
private String plastName;
private int childAge;
@OneToOne(cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
@JoinColumn(name="pId")
private Parent parent;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCfirstName() {
return cfirstName;
}
public void setCfirstName(String cfirstName) {
this.cfirstName = cfirstName;
}
public String getClastName() {
return clastName;
}
public void setClastName(String clastName) {
this.clastName = clastName;
}
public int getChildAge() {
return childAge;
}
public void setChildAge(int childAge) {
this.childAge = childAge;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
远程接口实现
@`enter code here`Stateless
@Remote(InformationRemote.class)
public class Information implements InformationRemote {
/**
* Default constructor.
*/
public Information() {
// TODO Auto-generated constructor s
}
@PersistenceContext(unitName="JPADB")
private EntityManager em;
public String save(Child child) throws Exception
{
System.out.println("First it si coming");
em.persist(child);
return("sucess");
}
}
这是我的插入类保存方法
public void save1()
{
Parent p =new Parent();
Child c=new Child();
InformationRemote bean=doLookup();
System.out.println("new method coming");
p.setParentAge(1);
p.setPfirstName("GOL");
p.setPlastName("JON");
c.setCfirstName("jj");
c.setClastName("gg");
c.setChildAge(1);
c.setParent(p);
try
{
String res=bean.save(c);
}
catch(Exception e)
{
System.out.println("this is a exception"+e);
}
}
当我调用 Save matheod
我的数据库显示结果为
此 Id 中的父表是主键
id pfirstName parentAge plastName
1 戈尔 1 乔恩
子表在这个子表ID是主键,pid是外键,指的是父表ID
id cfirstName childtAge clastName pid
1 jj 1 gg 1
现在我只想插入下面的子表
id cfirstName childtAge clastName pid
1 jj 1 gg 1
2 克 2 欧 1
怎么可能?