【问题标题】:In JPA OneToOne relationship how to insert only one side of the relation在 JPA OneToOne 关系中如何仅插入关系的一侧
【发布时间】:2013-03-25 12:12:10
【问题描述】:

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

这是我的父实体

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;   
}   
}   

这是我的子实体

查看纯副本到剪贴板打印? 注意:代码块中的文本内容会自动换行

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 乔恩

子表 在这个子表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

这怎么可能?

【问题讨论】:

  • 你能简单地说一下你的问题吗?似乎您发布了太多与您的问题无关的代码

标签: java jpa ejb-3.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);
}

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多