Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA合并问题_Java_Jpa - Fatal编程技术网

Java JPA合并问题

Java JPA合并问题,java,jpa,Java,Jpa,在基于spring/openjpa的应用程序中,我有两个简单的实体: @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

在基于spring/openjpa的应用程序中,我有两个简单的实体:

@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;
...}
我在第行中得到NullPointerException:entity.getTarget().getGames()始终为null,如果我在此处设置空hashmap,则事件为:为什么

我建议:

protected Collection<Game> games = new HashMap<Game>();
protectedcollection games=newhashmap();

因此,默认值将是一个空集合(良好做法),而不是
null
(不良做法)

如果您可以显示调用
createEntity
的代码,我认为这会有所帮助。特别是,如果你能证明当你构造你的游戏对象时,你有一行像
games=newarraylist()标题中的“merge”在哪里?事实上,必须支持merge()(至少在OpenJPA上),如果初始值设置为NULL,则无法在merge()期间删除集合
protected Collection<Game> games = new HashMap<Game>();