1

I have 2 entities: Version and Change log. A Version has N change Logs and a Change Log has one Version.

My Version entity looks like:

@Table(name="change_log_version")
@Entity
public class ChangeLogVersionEntity {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;

   @Column(name="version")
   private String version;

   @Column(name="date")
   private LocalDateTime date;

   @OneToMany(mappedBy = "version", cascade=CascadeType.ALL, fetch = 
   FetchType.EAGER)
   public List<ChangeLogEntity> changeLogEntities;

   public void addChangeLog(ChangeLogEntity changeLogEntity) {
      this.changeLogEntities.add(changeLogEntity);

      changeLogEntity.setVersion(this);
  }

...

}

My Change Log entity looks like:

@Table(name="change_log")
@Entity
public class ChangeLogEntity {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;

   @Column(name="description")
   private String description;

   @Column(name="added")
   private boolean isAdded;

   @Column(name="modified")
   private boolean isModified;

   @Column(name="deleted")
   private boolean isDeleted;

   @Column(name="public")
   private boolean isPublic;

   @Column(name="date")
   private LocalDateTime date;

   @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
   @JoinColumn(name = "version_id")
   private ChangeLogVersionEntity version;
  ...
 }

I am kinda new to hibernate and i am stuck in a null pointer by adding a change log to the version. What do i have to change to archive the relation?

Many thanks in advance so far :)

1 Answers1

1

That's because the changeLogEntites List is not initialized.

You should initialize it in the declaration

@OneToMany(mappedBy = "version", cascade=CascadeType.ALL, fetch =  FetchType.EAGER)
public List<ChangeLogEntity> changeLogEntities = new ArrayList<>();
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • But now i have a circular dependency inside my ChangeLogVersionEntity class in addChangeLog method. A Version has a List of Change Logs. A Change Log has a Version and so on. How to get rid of it? –  May 13 '18 at 14:05
  • Sure that's normal with JPA. With Hibernate a bi directional mapping is even faster. But the question is, do you need both sides? – Simon Martinelli May 13 '18 at 15:03
  • Have a look here https://stackoverflow.com/questions/5360795/what-is-the-difference-between-unidirectional-and-bidirectional-jpa-and-hibernat – Simon Martinelli May 13 '18 at 15:04
  • I understood the difference between Unidirectional and bidirectional. Back to my use case I don’t understand if I add a change log to my version how do I persist the change log if I am using a Unidirectional association? –  May 13 '18 at 15:24
  • Can you please explain what you like to do? Which side of the relationship do you want to keep? – Simon Martinelli May 13 '18 at 15:49
  • ChangeLogVersion should consist a List of ChangeLogs. –  May 13 '18 at 16:07
  • ok i ve got it working now. At changeLogVersion entity i have the following annotations: @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name= "version_id") public List changeLogEntities = new ArrayList<>(); and at the changeLog entity i removed all mapping to changeLogVersion –  May 13 '18 at 16:39
  • Congratulations! Have fun – Simon Martinelli May 13 '18 at 18:50