Hibernate单向多对多关系

时间:2017-04-23 10:05:06

标签: hibernate

我有两个课程:UserUserProfile

用户类:

public class User implements Serializable{

@Id @GeneratedValue//(strategy=GenerationType.IDENTITY)
private Integer id;

@NotEmpty
@Column(name="SSO_ID", unique=true, nullable=false)
private String ssoId;

@NotEmpty
@Column(name="PASSWORD", nullable=false)
private String password;

@NotEmpty
@Column(name="FIRST_NAME", nullable=false)
private String firstName;

@NotEmpty
@Column(name="LAST_NAME", nullable=false)
private String lastName;

@NotEmpty
@Column(name="EMAIL", nullable=false)
private String email;

@NotEmpty
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "APP_USER_USER_PROFILE",
         joinColumns = { @JoinColumn(name = "USER_ID") },
         inverseJoinColumns = { @JoinColumn(name = "USER_PROFILE_ID") })
private Set<UserProfile> userProfiles = new HashSet<UserProfile>();

@OneToMany(mappedBy = "sem_creator_id", cascade = CascadeType.ALL)
private Set<Seminar> seminarsTeacher = new HashSet<>();

@OneToMany(mappedBy = "storage_creator_id", cascade = CascadeType.ALL)
private Set<FTPStorage> storageFiles = new HashSet<>();

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getSsoId() {
    return ssoId;
}

public void setSsoId(String ssoId) {
    this.ssoId = ssoId;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Set<UserProfile> getUserProfiles() {
    return userProfiles;
}

public void setUserProfiles(Set<UserProfile> userProfiles) {
    this.userProfiles = userProfiles;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result + ((ssoId == null) ? 0 : ssoId.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof User))
        return false;
    User other = (User) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    if (ssoId == null) {
        if (other.ssoId != null)
            return false;
    } else if (!ssoId.equals(other.ssoId))
        return false;
    return true;
}

@Override
public String toString() {
    return "User [id=" + id + ", ssoId=" + ssoId + ", firstName=" + firstName + ", lastName=" + lastName
            + ", email=" + email + "]";
}

public Set<Seminar> getSeminarsTeacher() {
    return seminarsTeacher;
}

public void setSeminarsTeacher(Set<Seminar> seminarsTeacher) {
    this.seminarsTeacher = seminarsTeacher;
}

public Set<FTPStorage> getStorageFiles() {
    return storageFiles;
}

public void setStorageFiles(Set<FTPStorage> storageFiles) {
    this.storageFiles = storageFiles;
}

用户个人资料类:

public class UserProfile implements Serializable{

@Id
@GeneratedValue
private Integer id; 

@Column(name="TYPE", length=15, unique=true, nullable=false)
private String type = UserProfileType.USER.getUserProfileType();

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result + ((type == null) ? 0 : type.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof UserProfile))
        return false;
    UserProfile other = (UserProfile) obj;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    if (type == null) {
        if (other.type != null)
            return false;
    } else if (!type.equals(other.type))
        return false;
    return true;
}

@Override
public String toString() {
    return "UserProfile [id=" + id + ", type=" + type + "]";
}
}

当我使用此代码时,我在表UserProfile中获得具有多对多关系的重复实体。我使用persist()函数作为保存对象。我做错了什么?

然后我删除cascadeType并得到错误:对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例。

Hibernate日志:

Hibernate: 
insert 
into
    APP_USER
    (EMAIL, FIRST_NAME, LAST_NAME, PASSWORD, SSO_ID) 
values
    (?, ?, ?, ?, ?)

Hibernate: 
insert 
into
    APP_USER_USER_PROFILE
    (USER_ID, USER_PROFILE_ID) 
values
    (?, ?)

1 个答案:

答案 0 :(得分:0)

将此添加到您的criteria代码

   return session.createCriteria(User.class)
  .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)  
  .list();

使用条件获取时。这将删除重复的行。

希望,这会有所帮助