Reputation: 39
@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()));
}
}
Here is the code for ds1350 and applicant class. I use hibernate session save() to save the applicant where ds1350 is included in the applicant object. I have OneToMany which are perfectly fine but this OneToOne is not working. applicant.getDs1350() throws a NullPointerException as the foreign key (applicant_id in ds1350) is saved as null and applicantDao.getApplicantByPrimaryKey(applicantId) cannot fetch the ds1350 object.
Upvotes: 2
Views: 2742
Reputation: 9303
Get rid of " @Fetch(value = FetchMode.SELECT)". You're telling it to fetch eagerly in your OneToOne annotation but then telling it to fetch lazily with that @Fetch annotation.
Also, make sure that your join column on the ds1350 field has the correct FK field referenced (if the field in Applicant isn't called id).
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "applicant_id", referencedColumnName= "applicant_id")
private DS1350 ds1350;
Another point that i missed at first is that you're using
@GeneratedValue(strategy=IDENTITY)
To quote the documentation: "Indicates that the persistence provider must assign primary keys for the entity using a database identity column."
If you're not manually assigning the idea when you're creating the link the id won't be persisted. Using @GeneratedValue(strategy=AUTO) will tell the database that it needs to generate the ID for your child class (DS1350). This is A fix, but might not be what you're going for.
Also, based on your update you're referencing the wrong foreign key in the join column, and the association from your ds1350 to your applicant looks suspect, try something like this:
//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
I haven't tested that.
Upvotes: 1