2014-03-03 205 views
1

我遇到了休眠OneToMany關係的問題。由於某種原因,hibernate在正確保存關係時遇到了問題。這裏有兩類:休眠OneToMany關係

@Entity 
@Table 
public class Project { 

@Id 
@GenericGenerator(name = "generator", strategy = "increment") 
@GeneratedValue(generator = "generator") 
@Column(unique = true, nullable = false) 
private int projectId; 

@Column(unique = true, nullable = false) 
private String name; 

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "projects", targetEntity = User.class) 
private Set<User> users; 

@OneToMany(fetch = FetchType.EAGER, mappedBy = "project", 
     cascade = CascadeType.ALL, targetEntity = WorkingPackage.class) 
private Set<WorkingPackage> workingPackages; 

/** 
* Default Constructor. Creates an empty object. 
*/ 
public Project() { 
    // nothing to do here! 
} 

/** 
* Convenience Constructor. Use this constructor to create a new {@link Project} object. 
* 
* @param name The name of the project. May not be null. 
*/ 
public Project(String name) { 
    this.name = name; 
} 

/** 
* The id is the unique identifier for the {@link Project} in the database. DO NOT set the 
* id manually since it will be generated by Hibernate. 
* 
* @return The unique identifier for the project. 
*/ 
public int getProjectId() { 
    return projectId; 
} 

/** 
* The id is the unique identifier for the {@link Project} in the database. DO NOT set the 
* id manually since it will be generated by Hibernate. 
* 
* @param projectId The unique identifier for the project. 
*/ 
public void setProjectId(int projectId) { 
    this.projectId = projectId; 
} 

/** 
* Refers to the name of the {@link Project}. 
* 
* @return The name of the project. 
*/ 
public String getName() { 
    return name; 
} 

/** 
* Refers to the name of the {@link Project}. 
* 
* @param name The name of the project. 
*/ 
public void setName(String name) { 
    this.name = name; 
} 


/** 
* Gets the working packages. 
* 
* @return the working packages 
*/ 
public Set<WorkingPackage> getWorkingPackages() { 
    return workingPackages; 
} 

/** 
* Sets the working packages. 
* 
* @param workingPackages the new working packages 
*/ 
public void setWorkingPackages(Set<WorkingPackage> workingPackages) { 
    this.workingPackages = workingPackages; 
} 

/** 
* Gets the users. 
* 
* @return the users 
*/ 
public Set<User> getUsers() { 
    return users; 
} 

/** 
* Sets the users. 
* 
* @param users the new users 
*/ 
public void setUsers(Set<User> users) { 
    this.users = users; 
} 

@Override 
public boolean equals(Object other) { 
    if (this == other) { 
     return true; 
    } 
    if (!(other instanceof Project)) { 
     return false; 
    } 

    final Project project = (Project) other; 

    if (!project.getName().equals(getName())) { 
     return false; 
    } 
    return true; 
} 

@Override 
public int hashCode() { 
    return getName().hashCode(); 
} 
} 

二等:

@Entity 
@Table 
public class WorkingPackage { 

@Id 
@GenericGenerator(name = "generator", strategy = "increment") 
@GeneratedValue(generator = "generator") 
@Column(unique = true, nullable = false) 
private int workingPackageId; 

@Column(nullable = false) 
private String name; 

@ManyToOne(fetch = FetchType.EAGER) 
@Cascade({CascadeType.ALL }) 
@JoinColumn (name = "projectId") 
private Project project; 

/** 
* Default Constructor. Creates an empty object.1 
*/ 
public WorkingPackage() { 
    // nothing to do here! 
} 

/** 
* Convenience Constructor. Use this constructor to create a new {@link WorkingPackage} object. 
* 
* @param name The name of the project. May not be null. 
*/ 
public WorkingPackage(String name) { 
    this.name = name; 
} 

/** 
* The id is the unique identifier for the {@link WorkingPackage} in the database. DO NOT set the 
* id manually since it will be generated by Hibernate. 
* 
* @return The unique identifier for the project. 
*/ 
public int getWorkingPackageId() { 
    return workingPackageId; 
} 

/** 
* The id is the unique identifier for the {@link WorkingPackage} in the database. DO NOT set the 
* id manually since it will be generated by Hibernate. 
* 
* @param workingPackageId The unique identifier for the project. 
*/ 
public void setWorkingPackage(int workingPackageId) { 
    this.workingPackageId = workingPackageId; 
} 

/** 
* Refers to the name of the {@link WorkingPackage}. 
* 
* @return The name of the working package. 
*/ 
public String getName() { 
    return name; 
} 

/** 
* Refers to the name of the {@link WorkingPackage}. 
* 
* @param name The name of the working package. 
*/ 
public void setName(String name) { 
    this.name = name; 
} 

/** 
* Refers to the project of the {@link WorkingPackage}. 
* 
* @return The project of the working package. 
*/ 
public Project getProject() { 
    return project; 
} 

/** 
* Refers to the project of the {@link WorkingPackage}. 
* 
* @param project The name of the working package. 
*/ 
public void setProject(Project project) { 
    this.project = project; 
} 

@Override 
public boolean equals(Object other) { 
    if (this == other) { 
     return true; 
    } 
    if (!(other instanceof WorkingPackage)) { 
     return false; 
    } 

    final WorkingPackage workingPackage = (WorkingPackage) other; 

    if (!workingPackage.getName().equals(getName())) { 
     return false; 
    } 
    return true; 
} 

@Override 
public int hashCode() { 
    return getName().hashCode(); 
} 
} 

只在項目和Workingpackage之間的一對多關係出現問題。出於某種原因,hibernate並未正確保存數據庫中的對象,工作包表中缺少projectID,因此沒有鏈接。

編輯:我有getter和setter方法之前,現在公佈的全類...

EDIT2:

@OneToMany 
@Cascade({CascadeType.SAVE_UPDATE }) 
@JoinColumn(name = "project_id") 
private Set<WorkingPackage> workingPackages; 

二等:

@ManyToOne 
private Project project; 

作品我.. ..

+1

你錯過了getters和setters,試着用hibernate生成pojo類並查看其差異 – user2377971

回答

1

您的映射將WorkingPackage定義爲關係的所有者。 只有在設置了WorkingPackage.project時,纔會更新projectId列(如果元素已添加到Project.workingPackages中,則不會更新)。

參見:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html: 該關聯可以是雙向的。在雙向關係中,其中一方(並且只有一方)必須是所有者:所有者負責關聯列更新。要聲明一方不負責關係,使用屬性mappedBy。