OpenJPA

OpenJPA - Null relationship values not being persisted on merge  #angularjs #reactjs

  • Basically, I have the following two entities:

    As you can see, a Person can have an Image.

  • This is an optional column and supports a null value.
  • When a call from the REST Web Service comes in to update a Person changing the Image, if the Image is being set to null the DB isn’t being updated, however if the Image is being changed to a different existing Image, it is saving.
  • I suspect this has something to do with the fact that the Person is coming in from the outside (detached), since if I obtain the Person from the DAO Service, set the Image to null and call the update/merge method using that instance it is updating the DB.
  • I was able to get it to work by adding the following property:

    However this seems to have some drawbacks as other relationships aren’t being populated anymore when fetching from the DB.

I’m having a problem understanding/figuring out a problem I’m having with OpenJPA and was hoping someone here can help out.

@ng_real_ninja: OpenJPA – Null relationship values not being persisted on merge #angularjs #reactjs

I’m having a problem understanding/figuring out a problem I’m having with OpenJPA and was hoping someone here can help out.

Basically, I have the following two entities:

@Entity @Table(name = “images”) public class Image { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = “id”) private Long id; @Column(name = “name”) private String name; @Column(name = “path”) private String path; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } } @Entity @Table(name = “people”) public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name = “id”) private Long id; @Column(name = “name”) private String name; @ManyToOne @JoinColumn(name = “image_id”) @ForeignKey(name = “fk_person-image”) private Image image; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Image getImage() { return image; } public void setImage(Image image) { this.image = image; } }

As you can see, a Person can have an Image. This is an optional column and supports a null value. When a call from the REST Web Service comes in to update a Person changing the Image, if the Image is being set to null the DB isn’t being updated, however if the Image is being changed to a different existing Image, it is saving.

I suspect this has something to do with the fact that the Person is coming in from the outside (detached), since if I obtain the Person from the DAO Service, set the Image to null and call the update/merge method using that instance it is updating the DB.

I was able to get it to work by adding the following property:

However this seems to have some drawbacks as other relationships aren’t being populated anymore when fetching from the DB. (Rooms in a Building when retrieving the Building for example)

Has anyone encountered this before? Any ideas on how this should be handled? I’m tempted to test out Hibernate/Eclipselink to see if/how they handle this.

Any help would be appreciated, I’ve been racking my brain around this for the past 2 days.

OpenJPA