@Entity
@Table(name = "applicant")
public class Applicant implements Serializable {
private static final long serialVersionUID = -8634638904962909584L;
// Primary id required by Hibernate
@Id
@GeneratedValue(strategy=IDENTITY)
@Column(name = "applicant_id", nullable=false, unique=true)
private Long applicantId; // Unique id for each applicant
@OneToOne(cascade = CascadeType.ALL)
@Fetch(value = FetchMode.SELECT)
@JoinColumn(name = "applicant_id", referencedColumnName= "applicant_id")
private DS1350 ds1350;
public Applicant() {
}
}
@Entity
@Table(name = "ds_1350")
public class DS1350 implements Serializable {
private static final long serialVersionUID = -7370747595057569296L;
// Primary id required by Hibernate
@Id
@GeneratedValue(strategy=IDENTITY)
@Column(name = "ds_1350_id", nullable=false, unique=true)
private Long ds1350Id;
@Column(name = "applicant_id", unique=true, nullable=false)
// @GeneratedValue(generator="gen")
// @GenericGenerator(name = "gen", strategy = "foreign", parameters = @Parameter(name = "property", value = "applicant"))
private Long applicantId; // Unique id for each applicant
@Column(name = "ds1350_no", length = 50)
private String ds1350Number;
}
public class ApplicantDaoTest {
@Autowired
private ApplicantDao applicantDao;
private Applicant applicant;
private DS1350 ds1350 = new DS1350();
@BeforeClass
public static void beforeClass() {
}
@AfterClass
public static void afterClass() {
}
@Before
public void setup() {
this.initApplicant();
}
@After
public void teardown() {
}
private void initApplicant() {
applicant = new Applicant();
applicant.setFirstName("John");
Calendar calendar = Calendar.getInstance();
applicant.setDob(calendar);
applicant.setSsn("123456789");
applicant.setCreatedBy("JUNIT");
applicant.setCreatedDate(Calendar.getInstance());
applicant.setModifiedBy("JUnit");
applicant.setModifiedDate(Calendar.getInstance());
this.initDS1350();
}
private void initDS1350() {
ds1350.setDs1350Number("ds1350Number");
ds1350.setCreatedBy("JUNIT");
ds1350.setCreatedDate(Calendar.getInstance());
applicant.setDs1350(ds1350);
}
@Test
public void testSaveApplicant() {
Long applicantId = applicantDao.saveApplicant(applicant);
applicant = applicantDao.getApplicantByPrimaryKey(applicantId);
assertTrue("ds1350Number".equals(applicant.getDs1350().getDs1350Number()));
}
}
以下是ds1350和申請人類的代碼。我使用hibernate session save()來保存ds1350包含在申請人對象中的申請人。我有OneToMany,這是完美的,但這OneToOne不工作。 applicant.getDs1350()拋出一個NullPointerException異常,因爲外鍵(ds1350中的applicant_id)保存爲null,而applicantsDao.getApplicantByPrimaryKey(applicantId)無法獲取ds1350對象。OneToOne外鍵保存問題
你可以在你創建'Applicant'和'DS1350'實體的地方發佈代碼並保存嗎? – dcernahoschi 2013-04-23 16:51:26
private void initDS1350(){ \t \t ds1350 = new DS1350(); \t \t \t \t ds1350.setDs1350Number(「ds1350Number」); \t \t ds1350.setCreatedBy(「JUNIT」); \t \t ds1350.setCreatedDate(Calendar.getInstance()); \t \t \t \t \t applicant.setDs1350(ds1350); \t} – 2013-04-23 16:54:22
公共類ApplicantDaoTest { \t @Autowired \t私人ApplicantDao applicantDao; \t \t //域對象 \t private DS1350 ds1350 = new DS1350(); – 2013-04-23 16:54:59