2013-04-23 135 views
2
@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外鍵保存問題

+0

你可以在你創建'Applicant'和'DS1350'實體的地方發佈代碼並保存嗎? – dcernahoschi 2013-04-23 16:51:26

+0

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

+0

公共類ApplicantDaoTest { \t @Autowired \t私人ApplicantDao applicantDao; \t \t //域對象 \t private DS1350 ds1350 = new DS1350(); – 2013-04-23 16:54:59

回答

1

擺脫「@Fetch(value = FetchMode.SELECT)」。你告訴它在你的OneToOne註解中急切地獲取,但是告訴它用@Fetch註解來獲取懶惰。

此外,請確保ds1350字段上的加入列具有引用正確的FK字段(如果申請人中的字段未被稱爲id)。

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
@JoinColumn(name = "applicant_id", referencedColumnName= "applicant_id") 
private DS1350 ds1350; 

,我錯過了在第一另一點是,你使用

@GeneratedValue(strategy=IDENTITY) 

引述​​3210: 「指示的持久性提供者必須使用數據庫身份分配爲實體主鍵柱。」

如果您在創建鏈接時沒有手動分配該創意,則該ID不會被保留。使用@GeneratedValue(strategy = AUTO)將告訴數據庫它需要爲您的子類(DS1350)生成ID。這是一個修正,但可能不是你要做的。

此外,根據您的更新你引用的連接列從ds1350錯誤的外鍵,而聯想到你的申請看起來可疑,嘗試這樣的事情:

//in applicant 
    @OneToOne(cascade = CascadeType.ALL) 
    @Fetch(value = FetchMode.SELECT) 
    @JoinColumn(name = "ds1350_id", referencedColumnName= "applicant_id") 
    private DS1350 ds1350; 

    //in ds1350 
    @OneToOne(mappedBy="applicant", cascade=CascadeType.ALL) 
    private Applicant applicant; // Unique id for each applicant 

我的天堂」噸測試了這一點。

+0

Applcant和DS1350的字段都被命名爲applicant_id,申請人中的PK和DS1350中的FK,我應該在申請人中使用referencedColumnName =「applicantId」作爲referencedColumnName =「applicant_id」嗎? – 2013-04-23 18:52:38

+0

嘗試建議的更改,但沒有運氣,同樣的問題。 DS1350中的外鍵未保存。 – 2013-04-23 18:56:45

+0

referencedColumnName = 「applicant_id」 – zmf 2013-04-23 19:02:30