Reputation:
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 :)
Upvotes: 1
Views: 400
Reputation: 36173
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<>();
Upvotes: 1