Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ObjectDB关系_Java_Jpa_Objectdb - Fatal编程技术网

Java ObjectDB关系

Java ObjectDB关系,java,jpa,objectdb,Java,Jpa,Objectdb,我试图将关系“OneToMany”添加到我正在创建的数据库中,以测试ObjectDB是否对我有好处 到目前为止,我所尝试的: @Entity public class Parent { @Id private int parentID; private int childFK; Set<Child> children; public Parent(int p,int c) { this.parentID = p; this.c

我试图将关系“OneToMany”添加到我正在创建的数据库中,以测试ObjectDB是否对我有好处

到目前为止,我所尝试的:

@Entity
public class Parent {
    @Id
    private int parentID;
    private int childFK;
    Set<Child> children;

    public Parent(int p,int c) {
    this.parentID = p;
    this.childFK = c;
    }

    @OneToMany(orphanRemoval=true)
    @JoinColumn(name="childFK")
    public Set<Child> getChildren(){
        return childern;
    }
}
我的主要方法是:

   EntityManagerFactory emf = Persistence.createEntityManagerFactory("$objectdb/db/test.odb");
   EntityManager em = emf.createEntityManager();

   em.getTransaction().begin();

   Parent p = new Parent(01,02);
   em.persist(p);
   Child c = new Child(02,"bob");
   em.persist(c);

   em.getTransaction().commit();
这会创建数据库—没有问题,但不存在任何关系。 这是文件说明如何做到这一点的关键

我还尝试将实际的child实例添加到数据库中。这在某种程度上起作用,因为它知道有一个数据库实例可用,但不知道如何使用它

我尝试了以下查询:

 SELECT p.getChildren() FROM Parent p
这只是不返回此类方法getChildren()


有什么想法吗?

你的测试中有两个问题。修复这些错误后,将按预期创建父级和子级之间的关系

第一个问题,在JPA中,您可以使用字段访问或属性访问,但不允许将它们混合使用。如果您用@Id注释一个字段,那么您正在使用字段访问。在这种情况下,其他注释也应该在字段上,而不是在属性方法上

因此,您的父类应定义为:

@Entity
public static class Parent {
    @Id
    private int parentID;
    private int childFK;
    @OneToMany(orphanRemoval=true)
    // @JoinColumn(name="childFK") ignored by ObjectDB
    Set<Child> children;

    public Parent(int p,int c) {
        this.parentID = p;
        this.childFK = c;
    }

    public Set<Child> getChildren(){
        return children;
    }
}

使用这些修复程序运行测试后,将使用关系创建ObjectDB数据库。

我尝试了解决方案p.children=Collections.singleton(c),但它对我无效。这是我对这个问题的看法。。。让我们像上面的例子一样使用父对象和类对象

@Entity
public class Parent { 
  @Id
  private int parentID;
  private int childFK;

  Set<Child> children;

  public Parent(int p,int c) {
    this.parentID = p;
    this.childFK = c;
  }

  . . .

  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumn(name="childFK")
  public Set<Child> getChildren(){
    return childern;
  }
  public void setChildren(Set<Child> children) {
    this.children = children;
  }
}


@Entity
public class Child {
  @Id
  private int childID;      
  private String name;
  private int parentID;

  public Child(int c,String n, int p) {
    this.childID = c;
    this.name = n;
    this.parentID = p;
  }
}
@实体
公共类父{
@身份证
私有int-parentID;
私人国际儿童基金会;
设置儿童;
公共父级(int p,int c){
this.parentID=p;
this.childFK=c;
}
. . .
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name=“childFK”)
公共集getChildren(){
返回儿童;
}
公共无效集合子对象(集合子对象){
这个。孩子=孩子;
}
}
@实体
公营儿童{
@身份证
私人智力儿童;
私有字符串名称;
私有int-parentID;
公共子对象(int c、字符串n、int p){
this.childID=c;
this.name=n;
this.parentID=p;
}
}
在真实场景中,您需要从某个源获取childID值。在这种情况下,让我们从Child类获取一个id

public int childLastID(EntityManager em){
  TypedQuery<Child> query = em.createQuery("SELECT c FROM Child c order by i.childID 
                                            DESC", Child.class);

  List<Child> chldx = query.setFirstResult(0).setMaxResults(1).getResultList();

  if(chldx == null) return 100;  // In case your Child is new and empty 
                                 // lets start with 100

  return (chldx.get(0)).getId();                                    
}
public int childLastID(EntityManager em){
TypedQuery query=em.createQuery(“按i.childID从子c顺序中选择c
描述”,儿童班);
List chldx=query.setFirstResult(0.setMaxResults(1.getResultList();
if(chldx==null)返回100;//如果您的孩子是新的并且是空的
//让我们从100开始
return(chldx.get(0)).getId();
}
现在让我们填充这些父类和子类

EntityManagerFactory emf = Persistence.createEntityManagerFactory("...");            
EntityManager em = emf.createEntityManager();

int ix = childLastID(em);

em.getTransaction().begin();
Parent p = new Parent(01, ++ix); // increment the last ID to make sure it's unique


Set<Child> cset = new HashSet<Child>();   
Child c1 = new Child(ix,"bob", 01);
cset.add(c1);

Child c2 = new Child(++ix,"beb", 01);
cset.add(c2);

Child c3 = new Child(++ix, "bib", 01);
cset.add(c3)


p.setChildren(cset);
em.persist(p)    

em.getTransaction().commit();
EntityManagerFactory emf=Persistence.createEntityManagerFactory(“…”);
EntityManager em=emf.createEntityManager();
int ix=childLastID(em);
em.getTransaction().begin();
父p=新父(01,++ix);//增加最后一个ID以确保其唯一性
Set cset=newhashset();
子女c1=新子女(ix,“bob”,01);
c增加(c1);
子女c2=新子女(++ix,“beb”,01);
cset.add(c2);
儿童c3=新儿童(++ix,“bib”,01);
cset.add(c3)
p、 setChildren(cset);
em.persist(p)
em.getTransaction().commit();
现在,收集子c1。。。c3将在父类上持久化。我增加了c2和c3上的++ix,因为它们需要在子类上有一个唯一的id。我在子类上添加了一个parentID,作为父类和子类的引用

我使用FetchType.EAGER,以便在检索父对象时,也会检索这些子引用

此外,实体类的ID应该是long而不是int类型,所以只需将这些int更改为long基元类型即可

希望这将有助于


Nelson Deogracias

如果您添加了-1,请发表评论并说明原因。没有人会从中受益。谢谢,到底是什么问题?问题是没有孩子吗?您是否尝试过从父对象p中选择p,然后对返回的父对象调用
getChildren()
public int childLastID(EntityManager em){
  TypedQuery<Child> query = em.createQuery("SELECT c FROM Child c order by i.childID 
                                            DESC", Child.class);

  List<Child> chldx = query.setFirstResult(0).setMaxResults(1).getResultList();

  if(chldx == null) return 100;  // In case your Child is new and empty 
                                 // lets start with 100

  return (chldx.get(0)).getId();                                    
}
EntityManagerFactory emf = Persistence.createEntityManagerFactory("...");            
EntityManager em = emf.createEntityManager();

int ix = childLastID(em);

em.getTransaction().begin();
Parent p = new Parent(01, ++ix); // increment the last ID to make sure it's unique


Set<Child> cset = new HashSet<Child>();   
Child c1 = new Child(ix,"bob", 01);
cset.add(c1);

Child c2 = new Child(++ix,"beb", 01);
cset.add(c2);

Child c3 = new Child(++ix, "bib", 01);
cset.add(c3)


p.setChildren(cset);
em.persist(p)    

em.getTransaction().commit();