简体   繁体   English

Hibernate - 同步映射同一列的两个属性

[英]Hibernate - synchronize two properties mapping the same column

Is it possible to keep sync two properties on the same entity mapped to the same column (with basic JPA, or with hibernate)? 是否可以在映射到同一列的同一实体上保持同步两个属性(使用基本JPA或使用hibernate)?

I have two properties: 我有两个属性:

@JoinColumn(referencedColumnName = "id", insertable = false, name = "parent", updatable = false)
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;
@Column(name = "parent")
private Integer parentId;

Basically, I would like the following: 基本上,我想要以下内容:

System.out.println(element.getParentId());
System.out.println(element.getParent().getId());
element.setParentId(2);
System.out.println(element.getParentId());
System.out.println(element.getParent().getId());

This to print "3,3,2,2" instead of "3,3,2,3". 这打印“3,3,2,2”而不是“3,3,2,3”。

Is it possible (without writing a custom lazy-loading on the getParent() getter?) 是否可以(没有在getParent()getter上编写自定义延迟加载?)

Thx in advance Thx提前

IIRC, Hibernate will not do that for you. IIRC,Hibernate不会为你做那件事。 You're the one responsible if you have something like that (I remembered doing something similiar). 如果你有这样的东西,你就是那个人(我记得做过类似的事情)。

The first solution will be to save the entity first, then refresh using the entity manager. 第一种解决方案是先保存实体,然后使用实体管理器进行刷新。

element.setParentId(2);
entityManager.merge(element);
entityManager.refresh(element);

Refresh will make it fetch the object from the database again, and it will have the correct value. 刷新将使它再次从数据库中获取对象,并且它将具有正确的值。

The second option which I did, was to change the setter of the entity to maintain the state. 我做的第二个选项是更改实体的setter以维持状态。 Something like: 就像是:

public void setParent(Parent parent) {
    this.parent = parent;
    parentId = parent.id;
}

Try this 试试这个

@JoinColumn(referencedColumnName = "id", insertable = false, name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;


@Column(name = "parent")
private Integer parentId;

If that don't work: 如果那不起作用:

Try this if parentId is the primary key of the Parent entity: 如果parentId是Parent实体的主键,请尝试此操作:

@JoinColumn(referencedColumnName = "id", insertable = false, name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;

@Id
@GeneratedValue(strategy = GenerateType.AUTO)
@Column(name = "parent")
private Integer parentId;

Try this: 尝试这个:

@JoinColumn(referencedColumnName = "id", name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;


@Column(name = "parent")
private Integer parentId;

If that don't work: 如果那不起作用:

Try this: 尝试这个:

@JoinColumn(name = "parent")
@ManyToOne(fetch = FetchType.LAZY)
private Person parent;


@Column(name = "parent")
private Integer parentId;

You can have two properties for the same column like this : 您可以为同一列提供两个属性,如下所示:

@JoinColumn(name = "CGRADO_CODIGO", referencedColumnName = "CGRADO_CODIGO")
    @ManyToOne
    @NotFound(action=NotFoundAction.IGNORE)
    private SipreGrado                  sipreGrado;

    @Column(name = "CGRADO_CODIGO",insertable=false,updatable=false)
    private String                      sipreGradoCodigo;

Your output in the inspect code will be like this : 检查代码中的输出将如下所示:

sipreGradoCodigo   "001"
sipreGrado         null

or 要么

sipreGradoCodigo   "001"
sipreGrado         Entity SipreGrado ["001","other property","etc"]

Remember if you have sometimes the entity NULL you can skip it with that annotation 请记住,如果有时实体为NULL,则可以使用该注释跳过它

@NotFound(action=NotFoundAction.IGNORE)

Also,Remember to set 另外,请记住设置

insertable=false,updatable=false 

for the one that you dont want to include in the insert/update queries. 对于您不希望包含在插入/更新查询中的那个。

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

相关问题 Hibernate:在同一列中映射两个属性 - Hibernate: mapping two properties in the same colums hibernate中的一对多映射:将两个不同的属性引用到单个列 - one to many mapping in hibernate : Referring two different properties to a single column Hibernate映射相同的列两次 - Hibernate Mapping same Column twice 使用每个层次结构的表时,将同一数据库列映射到Hibernate中子类的不同字段/属性 - Mapping same DB column to different fields/properties of subclasses in Hibernate when using table per hierarchy 休眠两次映射同一列并插入 - Hibernate Mapping same Column twice and insert Hibernate:2个子类映射相同的键列 - Hibernate: 2 subclass mapping the same key column 休眠映射-具有相同列的两个表引用具有外键的一个表 - Hibernate mapping - Two tables with same column referencing to one table with foreign key 使用Hibernate HBM文件将两列映射到查找表中的同一列 - Mapping two columns to same column in a lookup table using hibernate hbm file 在数据库和Hibernate映射文件和POJO之间进行同步 - Synchronize between database and Hibernate Mapping Files and POJOs 具有相同外键的休眠多对多的“集合映射中的重复列”? - “Repeated column in mapping for collection” for hibernate ManyToMany with same foreign key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM