Home Shared column on two relationships causing null in both
Reply: 0

Shared column on two relationships causing null in both

Rafael Rossignol
1#
Rafael Rossignol Published in 2017-12-05 12:27:25Z

I had this entity that relationships with another two, but there is a column in common.

@Entity
@Table(name = "RH_EVENT")
public class RhEvent implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ESOC_ID", insertable=false, updatable=false)   
    private long esocId;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="ESOC_ID_COMPANY", insertable=false, updatable=false) 
    private Company company;

    @ManyToOne(optional=true,fetch=FetchType.EAGER)
    @JoinColumns({
       @JoinColumn(name="ESOC_ID_COMPANY", referencedColumnName="BRAN_ID_COMPANY", updatable=false, insertable= false),
       @JoinColumn(name="ESOC_ID_BRANCH", referencedColumnName="BRAN_ID_BRANCH", nullable=true, updatable=false, insertable= false),
    })
    @NotFound(action=NotFoundAction.IGNORE)
    private Branch branch;

    @Column(name="ESOC_FLAG")
    private String esocFlag;


    //more fields
}

The column is ESOC_ID_COMPANY, to query information everything is working fine. But when i update ESOC_FLAG field, making:

RhEvent myEvent = entityManager.find(RhEvent.class,1l);
myEvent.setEsocFlag("Y");
entityManager.commit();

JPA update ESOC_ID_COMPANY with null value, i believe the problem is that column BRAN_ID_BRANCH is null and branch relationship finds null, so all columns of relationship are updated.

How i solve this? The table came from a legacy system, so i cannot change relationships.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298199 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO