同一对象上的EntityManager.merge()不包括第二个的所有映射

时间:2018-01-23 19:46:17

标签: java mysql jpa

当我将GameObjectives添加到其父级(无论是另一个GameObjective还是Game本身)并调用EntityManager.merge(parent)时,GameObjective和UserGameObjective被添加到数据库(MySQL)没问题。

但是当我向同一个父母添加另一个GameObjective并再次调用合并时,GameObjective会被添加,但UserGameObjective不会。

如果我关闭程序并将另一个GameObjective添加到同一个父级,则新的GameObjective和UserGameObjective都会再次添加。

有谁看到问题是什么?

GameObjective

@Entity
public class GameObjective extends IEntityUser<UserGameObjective> implements Serializable{

@JoinColumn(nullable = false)
@ManyToOne
private Game game;
@JoinColumn(nullable = true)
@ManyToOne
private GameObjective parent;
@OneToMany(mappedBy = "parent")
private List<GameObjective> objectives;
...

IEntityUser

@MappedSuperclass
public abstract class IEntityUser<T extends UserEntity> extends IEntity{

    @OneToMany(mappedBy = "entity")
    protected List<T> userEntities;
    ...

UserEntity

@MappedSuperclass
@IdClass(UserEntityId.class)
public abstract class UserEntity {

    @Id
    @ManyToOne
    @JoinColumn(name = "userId", referencedColumnName = "id")
    private User user;
    ...

UserGameObjective

@Entity
public class UserGameObjective extends UserEntity implements Serializable{

    @Id
    @ManyToOne
    @JoinColumn(name = "gameObjectiveId", referencedColumnName = "id")
    private GameObjective entity;
    ...

UserEntityId

public class UserEntityId implements Serializable{

    private int user;
    private int entity;
    ...

0 个答案:

没有答案