简体   繁体   English

休眠约束违反异常

[英]Hibernate Constraint Violation Exception

I'm using Spring with Hibernate and I'm getting this exception. 我正在将Spring与Hibernate结合使用,并且遇到了此异常。 Here is what I'm trying to get: I have User and UserSettings classes, they're bounded with OneToMany and ManyToOne annotations like this: 这是我想要得到的:我有User和UserSettings类,它们受OneToMany和ManyToOne注释的束缚,如下所示:

public class UserImpl implements User, Serializable {

...some fields 

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
    private Set<UserSettingsImpl> settings;

}

public class UserSettingsImpl implements UserSettings, Serializable {

...some fields

    @ManyToOne(cascade = CascadeType.ALL)
    private UserImpl user;

}

Now, I want to add settings for user, that's how I'm doing it: 现在,我想为用户添加设置,这就是我的操作方式:

public long addSettings(final UserSettings settings) {
    userSettingsDAO.persist((UserSettingsImpl) settings);
    UserSettingsImpl settingsImpl = (UserSettingsImpl) settings;
    User user = settings.getUser();
    Set<UserSettingsImpl> settingsSet = (Set<UserSettingsImpl>) user.getSettingsSet();
    settingsSet.add(settingsImpl);
    userManager.updateUser(user); //it's just entityManager.merge(user);
    return ((UserSettingsImpl) settings).getId();
}

And here comes the problem: User from UserSettings holds set with old settings and the new one (the settings, that I've just created and want to add), but old settings holds sets with user, that don't have new settings in it. 问题就来了:来自UserSettings的用户持有带有旧设置和新设置的设置(我刚刚创建并想要添加的设置),但是旧设置持有带有用户的设置,而用户中没有新设置。它。 That's why I'm getting the exception, I suppose, but I don't know how to fix it. 我想这就是为什么我要得到例外,但是我不知道如何解决它。 (I think I'm working with Hibernate in the wrong way) (我认为我与Hibernate的合作方式有误)

You will get old Usersettings in the user object with the current setup. 使用当前设置,您将在user对象中获得旧的Usersettings It is because, the way the merge method works. 这是因为merge方法的工作方式。 The way it works is it will create a copy of the object and then persist and refresh the object and return it back. 它的工作方式是创建对象的副本,然后persistrefresh对象,然后将其返回。 So, in essense, the user object which you do pass in to the userManager will not be updated, but you have to get the returning value for the updated user 因此,从userManager ,您传递给userManageruser对象将不会更新,但是您必须获取更新后的user的返回值

I would try something like below, the userImpl is referenced by the relationship from UserSettingsImpl, if you persist UserSettingsImpl, the operation is applied to the UserImpl because of the cascade, and if UserImpl is new it will be persisted otherwise nothing happens. 我会尝试如下所示,userImpl由UserSettingsImpl中的关系引用,如果持久化UserSettingsImpl,则由于级联操作将操作应用于UserImpl,并且如果UserImpl是新的,它将被持久化,否则将不会发生。

public long saveSettings(final UserSettings settings) {
    userSettingsDAO.persist((UserSettingsImpl) settings);
    return ((UserSettingsImpl) settings).getId();
}

and manage the bidirectional relationship in the class UserImpl: 并在UserImpl类中管理双向关系:

 @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
 private Set<UserSettingsImpl> settings = new HashSet<UserSettingsImpl>();

 public void addUserSettings(UserSettings userSettings) {
    this.settings.add(userSettings);
    userSettings.setUserImpl(this);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM