简体   繁体   English

主键为外键的一对一映射-需要哪些注释

[英]One-to-one mapping where primary key is foreign key - what annotations are required

I have the following tables: 我有以下表格:

Table: MyEntity {
    MyEntityId: primary-key, auto-generated
    // other fields ommited
}

Table: MyEntityLastAction {
    MyEntityId: primary-key
    // other fields ommited
}
  • MyEntityLastAction.MyEntityId has a constraint that it must exist in MyEntity . MyEntityLastAction.MyEntityId有一个约束,它必须存在于MyEntity (This is a foreign key) (这是一个外键)
  • MyEntityLastAction is a VERY BIG record, hence why it is split out into another table MyEntityLastAction是一个非常大的记录,因此为什么将其拆分为另一个表

I am failing to setup the entity annotations correctly. 我无法正确设置实体注释。 Here is roughly where I am: 我大致在这里:

@Entity
public class MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long myEntityId;
    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    public MyEntityLastAction lastAction;
}

@Entity
public class MyEntityLastAction {
    @Id
    public long myEntityId;
    @OneToOne(mappedBy = "MyEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "MyEntityId")
    public MyEntity myEntity;
}

I image trying to run the following: 我想尝试运行以下图像:

MyEntity myEntity = new MyEntity();
myEntity.lastAction = new MyEntityLastAction();

em.save(myEntity);

I think you are looking for @MapsId annotation. 我认为您正在寻找@MapsId注释。 This allows you to have @OneToOne with shared primary key. 这使您可以使用具有共享主键的@OneToOne

See the below code for reference: 请参阅以下代码以供参考:

@Entity
class MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long myEntityId;
    @OneToOne(mappedBy = "myEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    public MyEntityLastAction lastAction;
}

@Entity
class MyEntityLastAction {
    @Id
    public long myEntityId;

    @OneToOne
    @JoinColumn(name="MyEntityId") 
    @MapsId     
    public MyEntity myEntity;
}

And to save the entity: 并保存实体:

        MyEntity me = new MyEntity();
        me.lastAction = new MyEntityLastAction();
        me.lastAction.myEntity = me;
        entityManager.persist(me);

There are two issues: First, your mappedBy attribute should be the field name in MyEntity (in this case lastAction ). 有两个问题:首先,您的mappedBy属性应该是MyEntity的字段名称(在本例中为lastAction )。 Second, since the mappedBy attribute indicates that the other side of the relationship handles how the two objects should be linked together, the join column annotation needs to be moved there. 其次,由于mappedBy属性指示关系的另一端处理应如何将两个对象链接在一起,因此需要将联接列注释移到那里。 This should work: 这应该工作:

@Entity
public class MyEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long myEntityId;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @PrimaryKeyJoinColumn(name = "MyEntityId")
    public MyEntityLastAction lastAction;
}

@Entity
public class MyEntityLastAction {
    @Id
    public long myEntityId;

    @OneToOne(mappedBy = "lastAction", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    public MyEntity myEntity;
}

Note that if you're creating a new MyEntity with JPA, it won't cascade the new MyEntityLastAction within it. 请注意,如果使用JPA创建新的MyEntity ,它将不会在其中级联新的MyEntityLastAction You'll have to first persist MyEntity , then set the ID in MyEntityLastAction to the one assigned to MyEntity , then persist MyEntityLastAction . 你必须坚持先MyEntity ,然后在设定的ID MyEntityLastAction到一个分配给MyEntity ,然后坚持MyEntityLastAction

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

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