Hibernate - syncing two properties mapping the same column

Is it possible to sync two properties on the same object mapped to the same column (basic JPA or 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:

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 is for printing "3,3,2,2" instead of "3,3,2,3".

Is this possible (without writing a custom lazy loading on getParent () getter?)

thanks in advance

+3


source to share


3 answers


IIRC, Hibernate won't do this for you. You are responsible for having something like this (I remember I did something similar).

The first solution is to save the object first and then update it using the entity manager.

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

      



Refresh will force it to fetch the object from the database again and will have the correct value.

The second option I did was to change the object's setter to maintain state. Something like:

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

      

+2


source


try it

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


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

      

If that doesn't work:

Try if parentId is the primary key of the 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 the following:

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


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

      

If that doesn't work:

Try the following:

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


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

      

0


source


You can have two properties for the same column:

@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 validation code will look like this:

sipreGradoCodigo   "001"
sipreGrado         null

      

or

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

      

Remember, sometimes you have a NULL object, you can skip it with this annotation

@NotFound(action=NotFoundAction.IGNORE)

      

Also, don't forget to install

insertable=false,updatable=false 

      

for those you don't want to include in insert / update requests.

0


source







All Articles