Hibernate创建行而不是更新

时间:2015-07-29 11:12:57

标签: java hibernate joincolumn

我有2个实体:用户和角色与JoinTable user_role链接

 @Entity
@Table(name = "core_users")
public class User implements Serializable {

    public User(long id, String name, String password, Calendar passwordExpiresOn, Role group, String firstName, String lastName, String email, Calendar createdOn, Calendar lastLogin, Calendar enabledFrom, Calendar enabledTo, Locale language, String tag_badge, List<Menu> menuList, Long phoneNumber) {
        super();
        this.id = id;
        this.name = name;
        this.password = password;
        this.passwordExpiresOn = passwordExpiresOn;
        this.role = group;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.createdOn = createdOn;
        this.lastLogin = lastLogin;
        this.enabledFrom = enabledFrom;
        this.enabledTo = enabledTo;
        this.language = language;
        this.phoneNumber = phoneNumber;
    }

    public User() {
        super();
    }


    private static final long serialVersionUID = -1611122599430240308L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "core_contatori")
    @TableGenerator(table = "core_contatori", valueColumnName = "valore_numerico", name = "core_contatori", initialValue = 0, pkColumnName = "id_contatore", pkColumnValue = "UG")
    @Column(name = "id")
    @Expose
    private long id;

    @Column(name = "username", unique = true)
    @Expose
    private String name;

    @Column(name = "user_password")
    @Expose
    private String password;

    @Column(name = "user_password_expire_date")
    @Temporal(TemporalType.DATE)
    @Expose
    private Calendar passwordExpiresOn;

    @OneToOne(optional = false, orphanRemoval = true)
    @Cascade(CascadeType.SAVE_UPDATE)
    @JoinTable(name = "core_user_roles", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") })
    @Expose
    private Role role;

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((createdOn == null) ? 0 : createdOn.hashCode());
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((enabledFrom == null) ? 0 : enabledFrom.hashCode());
        result = prime * result + ((enabledTo == null) ? 0 : enabledTo.hashCode());
        result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
        result = prime * result + (int) (id ^ (id >>> 32));
        result = prime * result + ((language == null) ? 0 : language.hashCode());
        result = prime * result + ((lastLogin == null) ? 0 : lastLogin.hashCode());
        result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((passwordExpiresOn == null) ? 0 : passwordExpiresOn.hashCode());
        result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
        result = prime * result + ((role == null) ? 0 : role.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (createdOn == null) {
            if (other.createdOn != null)
                return false;
        } else if (!createdOn.equals(other.createdOn))
            return false;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (enabledFrom == null) {
            if (other.enabledFrom != null)
                return false;
        } else if (!enabledFrom.equals(other.enabledFrom))
            return false;
        if (enabledTo == null) {
            if (other.enabledTo != null)
                return false;
        } else if (!enabledTo.equals(other.enabledTo))
            return false;
        if (firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!firstName.equals(other.firstName))
            return false;
        if (id != other.id)
            return false;
        if (language == null) {
            if (other.language != null)
                return false;
        } else if (!language.equals(other.language))
            return false;
        if (lastLogin == null) {
            if (other.lastLogin != null)
                return false;
        } else if (!lastLogin.equals(other.lastLogin))
            return false;
        if (lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!lastName.equals(other.lastName))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (passwordExpiresOn == null) {
            if (other.passwordExpiresOn != null)
                return false;
        } else if (!passwordExpiresOn.equals(other.passwordExpiresOn))
            return false;
        if (phoneNumber == null) {
            if (other.phoneNumber != null)
                return false;
        } else if (!phoneNumber.equals(other.phoneNumber))
            return false;
        if (role == null) {
            if (other.role != null)
                return false;
        } else if (!role.equals(other.role))
            return false;
        return true;
    }

第二个:

 @Entity
@Table(name = "core_roles")
public class Role implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "core_contatori")
    @TableGenerator(table = "core_contatori", valueColumnName = "valore_numerico", name = "core_contatori", initialValue = 0, pkColumnName = "id_contatore", pkColumnValue = "CG")
    @Column(name = "id")
    @Expose
    private long id;

    @Expose
    @Column(name = "role_name")
    private String name;

    @Expose
    @Column(name = "role_enabled")
    private boolean enabled;

    @OneToMany
    @Cascade(org.hibernate.annotations.CascadeType.REMOVE)
    @JoinTable(name = "core_user_roles", joinColumns = { @JoinColumn(name = "role_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "id") })
    private Set<User> userList;

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (enabled ? 1231 : 1237);
        result = prime * result + (int) (id ^ (id >>> 32));
        result = prime * result + ((menuList == null) ? 0 : menuList.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((userList == null) ? 0 : userList.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Role other = (Role) obj;
        if (id != other.id)
            return false;
        return true;
    }

在更新(用户)时,hibernate始终创建一个新角色,而不是更新现有角色。 我也试着在没有JoinTable的情况下使用JoinColumn获得相同的结果。

使用HASHCODE和EQUALS进行更新

0 个答案:

没有答案