In my spring/openjpa based application, I have two simple entities:
@Entity
public class Game {
@Id
@GeneratedValue
protected Long id;
@ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE})
@JoinColumn(name = "target_id")
protected GameObject target;
...}
@Entity
public class GameObject {
@Id
@GeneratedValue
protected Long id;
@Column
protected String name;
@OneToMany(mappedBy = "target", cascade = CascadeType.ALL)
protected Collection<Game> games;
...}
I'm trying to save game object with associated targetObject:
@PersistenceContext
protected EntityManager entityManager;
@Override
@Transactional
public Game createEntity(Game entity) {
getEntityManager().persist(entity);
if (entity.getTarget() != null) {
entity.getTarget().getGames().add(this);
}
getEntityManager().getEntityManager().flush();
return entity;
}
And I get NullPointerException in line: entity.getTarget().getGames() is always null, event if I set here empty hashmap. :/ Why