以下の2つのクラスに記載されているように、1対多の関係があります
@Entity
@Table(name="version_mst")
public class Version {
private Long id;
private String versionName;
private String releaseName;
private Set<VersionReleaseComponents> versionReleaseComponents;
@Id
@GeneratedValue
@Column(name="id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="versionName")
public String getVersionName() {
return versionName;
}
public void setVersionName(String versionName) {
this.versionName = versionName;
}
@Column(name="releaseName")
public String getReleaseName() {
return releaseName;
}
public void setReleaseName(String releaseName) {
this.releaseName = releaseName;
}
@OneToMany(mappedBy="version",cascade = { javax.persistence.CascadeType.ALL },fetch = FetchType.EAGER,targetEntity=VersionReleaseComponents.class)
public Set<VersionReleaseComponents> getVersionReleaseComponents() {
return versionReleaseComponents;
}
public void setVersionReleaseComponents(
Set<VersionReleaseComponents> versionReleaseComponents) {
this.versionReleaseComponents = versionReleaseComponents;
}
}
1 つのバージョンには多くの VersionReleaseComponent があります
@Entity
@Table(name="version_release_components")
public class VersionReleaseComponents {
private Long id;
@Id
@GeneratedValue
@Column(name="id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "versionId")
private Long versionId;
public Long getVersionId() {
return versionId;
}
public void setVersionId(Long versionId) {
this.versionId = versionId;
}
private Version version;
private String componentName;
private String versionNumber;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="versionId", referencedColumnName="id", insertable = false, updatable = false)
public Version getVersion() {
return version;
}
public void setVersion(Version version) {
this.version = version;
}
@Column(name="componentName")
public String getComponentName() {
return componentName;
}
public void setComponentName(String componentName) {
this.componentName = componentName;
}
@Column(name="versionNumber")
public String getVersionNumber() {
return versionNumber;
}
public void setVersionNumber(String versionNumber) {
this.versionNumber = versionNumber;
}
}
次を実行しようとすると
Version v = new Version();
v.setReleaseName("RN1");
v.setVersionName("VName1");
VersionReleaseComponents vrc = new VersionReleaseComponents();
vrc.setComponentName("cn1");
vrc.setVersionNumber("VNum1");
vrc.setVersion(v);
v.getVersionReleaseComponents().add(vrc); // null pointer error on here
session.saveOrUpdate(v);
セットを取得する際にヌル ポインター例外が発生します。ガイドしてください。
クエリの作成は次のとおりです
CREATE TABLE `version_mst` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`versionName` varchar(20) NOT NULL,
`releaseName` varchar(20) NOT NULL,
`last_modified_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
UNIQUE KEY `VERSION_MAPPING_MST_UNIQUE` (`versionName`,`releaseName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `version_release_components` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`versionId` int(10) NOT NULL ,
`componentName` varchar(20) NOT NULL,
`versionNumber` varchar(20) NOT NULL,
`last_modified_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`),
CONSTRAINT `FK_VERSION` FOREIGN KEY (`versionId`) REFERENCES `version_mst` (`id`),
UNIQUE KEY `VERSION_RELEASE_UNIQUE` (`componentName`,`versionNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;