私は、ObtainedSkill テーブルと Resource テーブルを持っています。リソース テーブルには、ObtainedSkill の列はありません。get/set メソッドしかありません。ObtainedSkill テーブルには、リソース シリアル番号への forien キーがあります。
setObtainedSkill() を呼び出してから、リソースを新しい ObtainedSkills リストで更新すると、更新されません。リソースのObtainedSkillsはまだnullです。
私の質問は、ObtainedSkills を自分のリソースに追加するにはどうすればよいですか? リソース テーブルに、ObtainedSkills 用に別の列を作成する必要がありますか?
ここに私のテストクラスがあります:
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 のシリアル番号です。他の折りたたまれたフィールドも varchar のミドル ネームとラスト ネームです。