@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,并且applicantDao.getApplicantByPrimaryKey(applicantId)无法获取ds1350对象。
发布于 2013-04-24 01:26:49
去掉“@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)引用documentation:“指示持久性提供程序必须使用数据库标识列为实体分配主键。”
如果您在创建链接时没有手动分配想法,则id将不会被持久保存。使用@GeneratedValue(strategy=AUTO)将告诉数据库它需要为您的子类(DS1350)生成ID。这是个解决办法但可能不是你想要的。
此外,根据您的更新,您在join列中引用了错误的外键,并且从您的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我还没有测试过。
https://stackoverflow.com/questions/16174213
复制相似问题