在JPA OneToOne关系中如何只插入关系的一侧

时间:2013-03-25 12:12:11

标签: java jpa ejb-3.0

我想在关系的一侧插入数据

这是我的父实体

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到clipboardprint? 注意:代码块中的文本内容是自动换行的

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 GOL 1 JON

儿童表 在此子表中,ID是主键,pid是外键,它引用父表ID

id cfirstName childtAge clastName pid

1 jj 1 gg 1

现在我想只插入子表,如下所示

id cfirstName childtAge clastName pid

1 jj 1 gg 1

2 kk 2 oo 1

怎么可能?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望将子项添加到现有父项。所以这很简单。由于父级已存在,因此您将获得对它的引用,然后将父级分配给新子级:

public void createChild(String firstName, String lastName, int age, int parentId) {
    Parent parent = entityManager.getReference(Parent.class, parentId);
    Child child = new Child();
    child.setFirstName(firstName);
    child.setLastName(lastName);
    child.setAge(age);
    child.setParent(parent);
    entityManager.persist(child);
}