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 和申请类别的代码。我使用休眠会话 save() 来保存申请人对象中包含 ds1350 的申请人。我有 OneToMany 非常好,但这个 OneToOne 不起作用。由于外键(ds1350 中的 applicant_id)被保存为 null 并且 applicationDao.getApplicantByPrimaryKey(applicantId) 无法获取 ds1350 对象,因此申请者.getDs1350() 抛出 NullPointerException。

4

1 回答 1

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)

引用文档:“表示持久性提供程序必须使用数据库标识列为实体分配主键。”

如果您在创建链接时没有手动分配想法,则不会保留该 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

我没有测试过。

于 2013-04-23T17:26:49.180 回答