2014-03-03 3 views
1

최대 절전 모드 OneToMany 관계에 문제가 있습니다. 몇 가지 이유로 최대 절전 모드에서는 관계를 올바른 방식으로 저장하는 데 문제가 있습니다. 여기에 두 개의 클래스는 다음과 같습니다최대 절전 모드 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(); 
} 
} 

2 등석 :

@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 사이의 OnetoMany 관계에서 발생한다. 어떤 이유로 최대 절전 모드가 데이터베이스의 오브젝트를 올바르게 저장하지 못하기 때문에, workID 테이블에 projectID가 없으므로 링크가 없다.

편집 : 나는 ..., 전에 getter 및 setter 있었다 지금은 전체 클래스를 게시

EDIT2 :

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

2 등석 : 나를 위해

@ManyToOne 
private Project project; 

작품 .. ..

+1

당신은 getter와 setter가 없으므로 최대 절전 모드로 pojo 클래스를 생성하고 차이점을 확인하십시오. – user2377971

답변

1

매핑은 WorkingPackage를 관계의 소유자로 정의합니다. WorkingPackage.project가 설정된 경우에만 projectId 열이 업데이트됩니다 (요소가 Project.workingPackages에 추가되지 않은 경우).

참조 : http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html : 연관성은 양방향 일 수 있습니다. 양방향 관계에서 측면 중 하나 (그리고 오직 하나)는 소유자 여야합니다. 소유자가 연관 열을 업데이트해야합니다. 관계를 담당하지 않는 쪽을 선언하려면 mappedBy 특성이 사용됩니다.

관련 문제