简体   繁体   English

JPA交叉单向OneToOne

[英]JPA cross unidirectional OneToOne

I have two entity-classes A and B. For A there is only one instance of class B. Through a lifecicle of application I need to create new instnace of B for A. But for history log I need to store previous instance of B with links to an this A instance. 我有两个实体类A和B.对于A,只有一个B类实例。通过应用程序的生命周期,我需要为A创建B的新实例。但对于历史日志,我需要存储B的前一个实例链接到此A实例。 So I created next mapping: 所以我创建了下一个映射:

@Entity
class A {
    @OneToOne
    @JoinColumn(name="B_ID")
    B b;
}

@Entity
class B {
    @OneToOne
    @JoinColumn(name="A_ID")
    A a;
}
///////
CREATE TABLE A_TABLE (ID uuid, B_ID uuid, FOREIGN KEY (B_ID) REFERENCES B_TABLE(ID));
CREATE TABLE B_TABLE (ID uuid, A_ID uuid, FOREIGN KEY (A_ID) REFERENCES A_TABLE(ID));

I already have some correct data in my tables, but each time, when I trying to get any instance of A class, the b field is null. 我的表中已经有了一些正确的数据,但每次当我尝试获取A类的任何实例时,b字段为空。 So, what am I doing wrong? 那么,我做错了什么?

UPD_0 Ok, here what I'm realy dealing with: UPD_0好的,这里我真的要处理的是:

@MappedSuperclass
public abstract class BaseUuidEntity {
     @Id
     @Column(name = "ID")
     @Persistent
     protected UUID id;
}

@MappedSuperclass
public class StandartEntity extends BaseUuidEntity { ... }
@MappedSuperclass
public abstract class AbstractEntity extends BaseUuidEntity { ... }
@Entity(name = "CardEntity")
@Table(name = "TBL_CARD")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "CARD_TYPE", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
public class CardEntity extends AbstractEntity { ... }

@Entity("aEntity")
@DiscriminatorValue("1")
@Table(name = "A_TABLE")
class A extends CardEntity { 
    @OneToOne
    @JoinColumn(name="B_ID")
    B b;
    public String getBName() {
        return b.getName(); // NullPointerException
    }
}
@Entity("bEntity")
@Table(name = "B_TABLE")
class B extends StandartEntity {
    @Column(name="name")
    String name;
    public String getName() {
        return this.name;
    }
}

-- A_TABLE (ID, B_ID)
'41a2647d-2ef6-f507-92ee-bff0f784b6f3', '5d3c66da-b377-11e4-bc9c-1008b124eacf'
-- B_TABLE (ID, A_TABLE, NAME)
'5d3c66da-b377-11e4-bc9c-1008b124eacf', '41a2647d-2ef6-f507-92ee-bff0f784b6f3', 'Some text'

I'm getting all A entities (in my case - it's one) 我得到所有A实体(在我的情况下 - 它是一个)

select e from aEntity e

and trying to get name of b entity in each of them, but getting nullpointer, cause b is null; 并尝试在每个实体中获取b实体的名称,但是获得nullpointer,因为b为null;

I'm not sure if it will make any difference, but try changing the code in getBName() to use the getter instead of directly referencing the instance variable b : 我不确定它是否会有任何区别,但是尝试更改getBName()的代码以使用getter而不是直接引用实例变量b

public String getBName() {
    return getB().getName();
}

instead of: 代替:

public String getBName() {
    return b.getName(); // NullPointerException
}

I suspect that this could be the issue because JPA implementations create proxies for entities, which are usually subclasses of your entities, and these proxies override your getters and setters. 我怀疑这可能是问题,因为JPA实现为实体创建代理,实体通常是实体的子类,并且这些代理覆盖了getter和setter。 So if you don't use the getter it could be that the instance variable does not get initialized properly. 因此,如果您不使用getter,则实例变量可能无法正确初始化。

you need mappedBy anotation: http://en.wikibooks.org/wiki/Java_Persistence/OneToOne to inform java who is in charge of this relationship and when the 'foreign'id' is storred. 你需要mappedBy anotation: http//en.wikibooks.org/wiki/Java_Persistence/OneToOne来通知负责这种关系的java以及何时传递'foreign'id'。 You also need the foreign key in only one of the entities not both! 您还需要只有一个实体中的外键而不是两个实体!

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

相关问题 JPA OneToOne单向映射 - JPA OneToOne unidirectional mapping 指定单向 @OneToOne JPA 映射的所有者 - Specifying the owner of a unidirectional @OneToOne JPA mapping Spring-Data-JPA OneToOne单向问题 - Spring-data-jpa OneToOne Unidirectional issue JPA单向@OneToOne,应在另一个表中创建列 - Jpa unidirectional @OneToOne where columns should be created in another table hibernate OneToOne单向映射 - hibernate OneToOne unidirectional mapping 使用 ID 的单向 @OneToOne - Unidirectional @OneToOne using IDs Hibernate JPA 单向 OneToOne Join - 更新 null 外键后插入为 Z37A62759CC0C1DAE0BDABDZ99 - Hibernate JPA UniDirectional OneToOne Join - Updating a null foreign key after been inserted as null 当保存到存储库而不使用级联时,Spring JPA 获取错误分离实体传递给单向 OneToOne 关系 - Spring JPA get error detached entity passed to persist on unidirectional OneToOne relationship when save to repository without cascade JPA 与共享主键的单向@OneToOne 关系始终触发辅助查询,即使 fetchType 为 EAGER - JPA unidirectional @OneToOne relationship with shared primary key always trigger a secondary query even if fetchType is EAGER 如何在 JPA 中使用 .netoone 和 manytoone 单向关系中的 cascade.delete / orphanremoval? - How to use cascade.delete / orphanremoval in onetoone and manytoone unidirectional relationships in JPA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM