简体   繁体   English

DataNucleus中的多对一单向关系

[英]Many-to-one unidirectional relation in DataNucleus

For the context, client-side I use the MVP pattern, so the view with the One list knows only the ID, and when my new Many is received on the server, I want to be able to just update the One 's foreign key, with a "setOneId" or an empty One object with an ID set to the wanted value. 对于上下文,客户端我使用MVP模式,因此具有One列表的视图只知道ID,当我在服务器上收到新的Many时,我希望能够只更新One的外键,使用“setOneId”或空的One对象,其ID设置为所需值。

So I try to create a many-to-one unidirectional in DataNucleus, and I'm struggling a bit. 所以我尝试在DataNucleus中创建一个多对一的单向,我有点挣扎。 I'm ok to use JDO or JPA, I don't really care. 我可以使用JDO或JPA,我真的不在乎。 In JPA, I tried this : 在JPA中,我试过这个:

@Entity
public class Many {
    @Id
    String id;

    @ManyToOne
    @Join(name = "idOne")
    One one;
}

@Entity
public class One {
    @Id
    String id;
}

It's almost what I want. 这几乎就是我想要的。 The one-to-many is created but with a join table. 创建了一对多,但使用了连接表。 I want to have a direct relation. 我希望有一个直接的关系。 And when I insert/update a Many , I don't want to insert/update the related One , just update the idOne with the good id in my Many object. 当我插入/更新一个Many ,我不想插入/更新相关的One ,只需在我的Many对象中使用好的id更新idOne

I found this blogpost , but it's with Hibernate, and I think it still use a join table : 我找到了这个博文 ,但它是Hibernate,我认为它仍然使用连接表:

@Entity
public class Many {
    @Id
    public String id;

    @Column(name="idOne")
    private String idOne;

    @ManyToOne
    @JoinColumn(name="idOne", nullable=false, insertable=false, updatable=false)
    private One one;
}

I tried it, but I got exactly this error . 我试过了,但我得到了这个错误

I don't understand how I am struggling with that. 我不明白我是如何与之斗争的。 My goal is to have a table that keep some reference data (like a list of country as the class One ), and a list of "working item" (like a town as the class Many ) that I create/update without create/update the reference data, just its foreign key in the Many object. 我的目标是有保留一些参考数据(比如作为类国家的名单表One ),以及(如镇为类“工作项目”列表Many ,我没有创建/更新创建/更新)引用数据,只是它在Many对象中的外键。

If its a unidirectional association, and Many is the owning side (as per your second example), you are heading in the wrong direction. 如果它是单向关联,并且Many是拥有方(根据你的第二个例子),你正走向错误的方向。 It doesn't make much sense to delegate the update and insert responsibility on the owning side of a unidirectional relationship (as done with the insertable=false and updateable=false). 委托更新并在单向关系的拥有方插入责任没有多大意义(与insertable = false和updateable = false一样)。

EDIT: updated answer So what you want is a many-to-one, with a foreign key column on the owning side. 编辑:更新的答案所以你想要的是多对一,拥有一个外键列。 Try this 尝试这个

@Entity
public class Many {
    @Id
    String id;

    @ManyToOne
    @JoinColumn(name = "foreignKeyColumn")
    One one;
}
@Entity
public class A {
    @Id
    String id;

    @OneToOne(cascade=CascadeType.ALL)
    B b;
}

@Entity
public class B {
    @Id
    String id;
}

and then if you persisted initial objects as 如果你将初始对象保持为

tx.begin();
A a = new A("FirstA");
B b1 = new B("FirstB");
B b2 = new B("SecondB");
a.setB(b1);
em.persist(a);
em.persist(b2);
tx.commit();

... (some time later)
tx.begin();
A a = em.find(A.class, "FirstA");
B b2 = em.getReference(B.class, "SecondB");

// update the B in A to the second one
a.setB(b2);
tx.commit();

This updates the FK between A and B. Can't get simpler 这会更新A和B之间的FK。不能简单

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM