1

我有一个获得技能表和一个资源表。资源表中没有获取技能的列。只有 get/set 方法。获得技能表具有资源序列号的外键。

当我调用 setObtainedSkill() 然后使用新的已获取技能列表更新资源时,它不会更新。资源的已获取技能仍然为空。

我的问题是,如何将已获得技能添加到我的资源中?我是否必须在“资源”表中为“获得技能”添加另一列?

这是我的测试课:

    public static void main(String[] args) throws Exception {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("XRM_JPA");

    ResourcezManager rm = new ResourcezManager(emf);

    Resourcez resource = rm.getNewResourcez();
    resource = rm.findResourcezBySerialNumber("2G9933");

    for (int i = 0; i < resource.getObtainedSkills().size(); i++) {
        if (resource.getObtainedSkills().get(i) != null) {
            System.out.println(resource.getObtainedSkills().get(i).toString()); 
        }
    }

    emf.close();
}

当它尝试执行 System.out 时,我得到一个 NullPointerException。

以下是相关实体

技能实体

package xrm.model;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;


/**
 * The persistent class for the SKILL database table.
 * 
 */
@Entity
@NamedQueries({@NamedQuery(name = "getSkillBySkillName", query = "SELECT s FROM Skill s      WHERE s.skillName = :skillName"),@NamedQuery(name = "getSkillBySkillDescription", query =   "SELECT s FROM Skill s WHERE s.skillDescription = :skillDescription"),
@NamedQuery(name = "getSkillBySkillId", query = "SELECT s FROM Skill s WHERE s.skillId = :skillId"),
@NamedQuery(name = "getSkill", query = "SELECT s FROM Skill s")})
public class Skill implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="SKILL_ID")
private int skillId;

@Column(name="SKILL_DESCRIPTION")
private String skillDescription;

@Column(name="SKILL_NAME")
private String skillName;

//bi-directional many-to-one association to ObtainedSkill
@OneToMany(mappedBy="skill")
private List<ObtainedSkill> obtainedSkills;

public Skill() {
}

public int getSkillId() {
    return this.skillId;
}

public void setSkillId(int skillId) {
    this.skillId = skillId;
}

public String getSkillDescription() {
    return this.skillDescription;
}

public void setSkillDescription(String skillDescription) {
    this.skillDescription = skillDescription;
}

public String getSkillName() {
    return this.skillName;
}

public void setSkillName(String skillName) {
    this.skillName = skillName;
}

public List<ObtainedSkill> getObtainedSkills() {
    return this.obtainedSkills;
}

public void setObtainedSkills(List<ObtainedSkill> obtainedSkills) {
    this.obtainedSkills = obtainedSkills;
}

}

获得技能实体

package xrm.model;

import java.io.Serializable;
import javax.persistence.*;


/**
 * The persistent class for the OBTAINED_SKILL database table.
 * 
 */
@Entity
@Table(name="OBTAINED_SKILL")
@NamedQueries({@NamedQuery(name = "getObtainedSkillBySkill", query = "SELECT o FROM ObtainedSkill o WHERE o.skill.skillId = :skill_skillId"),
@NamedQuery(name = "getObtainedSkillByResourcez", query = "SELECT o FROM ObtainedSkill o WHERE o.resourcez.serialNumber = :resourcez_serialNumber"),
@NamedQuery(name = "getObtainedSkillBySkillLevel", query = "SELECT o FROM ObtainedSkill o WHERE o.skillLevel = :skillLevel"),
@NamedQuery(name = "getObtainedSkillByObtainedSkillId", query = "SELECT o FROM ObtainedSkill o WHERE o.obtainedSkillId = :obtainedSkillId"),
@NamedQuery(name = "getObtainedSkill", query = "SELECT o FROM ObtainedSkill o")})
public class ObtainedSkill implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="OBTAINED_SKILL_ID")
private int obtainedSkillId;

@Column(name="SKILL_LEVEL")
private int skillLevel;

//bi-directional many-to-one association to Resourcez
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="SERIAL_NUMBER")
private Resourcez resourcez;

//bi-directional many-to-one association to Skill
@ManyToOne (cascade=CascadeType.DETACH)
@JoinColumn(name="SKILL_ID")
private Skill skill;

public ObtainedSkill() {
}

public int getObtainedSkillId() {
    return this.obtainedSkillId;
}

public void setObtainedSkillId(int obtainedSkillId) {
    this.obtainedSkillId = obtainedSkillId;
}

public int getSkillLevel() {
    return this.skillLevel;
}

public void setSkillLevel(int skillLevel) {
    this.skillLevel = skillLevel;
}

public Resourcez getResourcez() {
    return this.resourcez;
}

public void setResourcez(Resourcez resourcez) {
    this.resourcez = resourcez;
}

public Skill getSkill() {
    return this.skill;
}

public void setSkill(Skill skill) {
    this.skill = skill;
}

}

资源实体

package xrm.model;

import java.io.Serializable;
import javax.persistence.*;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the RESOURCEZ database table.
 * 
 */
@Entity
@NamedQueries({@NamedQuery(name = "getResourcezByUserInformation", query = "SELECT r FROM Resourcez r WHERE r.userInformation.ibmEmailAddress = :userInformation_ibmEmailAddress"),@NamedQuery(name = "getResourcezBySource", query = "SELECT r FROM Resourcez r WHERE r.source.sourceId = :source_sourceId"),
@NamedQuery(name = "getResourcezByRole", query = "SELECT r FROM Resourcez r WHERE r.role.roleId = :role_roleId"),
@NamedQuery(name = "getResourcezByLocation", query = "SELECT r FROM Resourcez r WHERE r.location.locationId = :location_locationId"),
@NamedQuery(name = "getResourcezByMiddleInitial", query = "SELECT r FROM Resourcez r WHERE r.middleInitial = :middleInitial"),
@NamedQuery(name = "getResourcezByLastName", query = "SELECT r FROM Resourcez r WHERE r.lastName = :lastName"),
@NamedQuery(name = "getResourcezByHireDate", query = "SELECT r FROM Resourcez r WHERE r.hireDate = :hireDate"),
@NamedQuery(name = "getResourcezByFirstName", query = "SELECT r FROM Resourcez r WHERE r.firstName = :firstName"),
@NamedQuery(name = "getResourcezByBirthday", query = "SELECT r FROM Resourcez r WHERE r.birthday = :birthday"),
@NamedQuery(name = "getResourcezBySerialNumber", query = "SELECT r FROM Resourcez r WHERE r.serialNumber = :serialNumber"),
@NamedQuery(name = "getResourcez", query = "SELECT r FROM Resourcez r")})
public class Resourcez implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="SERIAL_NUMBER")
private String serialNumber;

@Temporal( TemporalType.DATE)
private Date birthday;

@Column(name="FIRST_NAME")
private String firstName;

@Temporal( TemporalType.DATE)
@Column(name="HIRE_DATE")
private Date hireDate;

@Column(name="LAST_NAME")
private String lastName;

@Column(name="MIDDLE_INITIAL")
private String middleInitial;

//bi-directional many-to-one association to Assignment
@OneToMany(mappedBy="resourcez", cascade=CascadeType.DETACH)
private List<Assignment> assignments;

//bi-directional many-to-one association to ObtainedCertification
@OneToMany(mappedBy="resourcez", cascade=CascadeType.ALL)
private List<ObtainedCertification> obtainedCertifications;

//bi-directional many-to-one association to ObtainedSkill
@OneToMany(mappedBy="resourcez", cascade=CascadeType.ALL)
private List<ObtainedSkill> obtainedSkills;

//bi-directional many-to-one association to PhoneNumber
@OneToMany(mappedBy="resourcez", cascade=CascadeType.ALL)
private List<PhoneNumber> phoneNumbers;

//bi-directional many-to-one association to PrivilegeRequest
@OneToMany(mappedBy="resourcez", cascade=CascadeType.ALL)
private List<PrivilegeRequest> privilegeRequests;

//bi-directional many-to-one association to Location
@ManyToOne
@JoinColumn(name="LOCATION_ID")
private Location location;

//bi-directional many-to-one association to Role
@ManyToOne
@JoinColumn(name="ROLE_ID")
private Role role;

//bi-directional many-to-one association to Source
@ManyToOne
@JoinColumn(name="SOURCE_ID")
private Source source;

//bi-directional many-to-one association to UserInformation
@ManyToOne
@JoinColumn(name="IBM_EMAIL_ADDRESS")
private UserInformation userInformation;

public Resourcez() {
}

public String getSerialNumber() {
    return this.serialNumber;
}

public void setSerialNumber(String serialNumber) {
    this.serialNumber = serialNumber;
}

public Date getBirthday() {
    return this.birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

public String getFirstName() {
    return this.firstName;
}

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

public Date getHireDate() {
    return this.hireDate;
}

public void setHireDate(Date hireDate) {
    this.hireDate = hireDate;
}

public String getLastName() {
    return this.lastName;
}

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

public String getMiddleInitial() {
    return this.middleInitial;
}

public void setMiddleInitial(String middleInitial) {
    this.middleInitial = middleInitial;
}

public List<Assignment> getAssignments() {
    return this.assignments;
}

public void setAssignments(List<Assignment> assignments) {
    this.assignments = assignments;
}

public List<ObtainedCertification> getObtainedCertifications() {
    return this.obtainedCertifications;
}

public void setObtainedCertifications(List<ObtainedCertification> obtainedCertifications) {
    this.obtainedCertifications = obtainedCertifications;
}

public List<ObtainedSkill> getObtainedSkills() {
    return this.obtainedSkills;
}

public void setObtainedSkills(List<ObtainedSkill> obtainedSkills) {
    this.obtainedSkills = obtainedSkills;
}

public List<PhoneNumber> getPhoneNumbers() {
    return this.phoneNumbers;
}

public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
    this.phoneNumbers = phoneNumbers;
}

public List<PrivilegeRequest> getPrivilegeRequests() {
    return this.privilegeRequests;
}

public void setPrivilegeRequests(List<PrivilegeRequest> privilegeRequests) {
    this.privilegeRequests = privilegeRequests;
}

public Location getLocation() {
    return this.location;
}

public void setLocation(Location location) {
    this.location = location;
}

public Role getRole() {
    return this.role;
}

public void setRole(Role role) {
    this.role = role;
}

public Source getSource() {
    return this.source;
}

public void setSource(Source source) {
    this.source = source;
}

public UserInformation getUserInformation() {
    return this.userInformation;
}

public void setUserInformation(UserInformation userInformation) {
    this.userInformation = userInformation;
}

}

技能表 技能表

获得的技能表 获得的技能表

资源表 第一个字段是序列号,它是一个 varchar 其他折叠的字段也是 varchars Middle 和 Last name。 资源表

4

0 回答 0