gpt4 book ai didi

java - OneToOne外键保存问题

转载 作者:行者123 更新时间:2023-11-30 11:33:15 26 4
gpt4 key购买 nike

   @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 对象。

最佳答案

去掉“@Fetch(value = FetchMode.SELECT)”。您在 OneToOne 注释中告诉它急切获取,但随后告诉它使用 @Fetch 注释延迟获取。

此外,请确保您在 ds1350 字段上的加入列引用了正确的 FK 字段(如果 Applicant 中的字段未称为 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。这是一个修复,但可能不是您想要的。

此外,根据您的更新,您在连接列中引用了错误的外键,并且从您的 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

我还没有测试过。

关于java - OneToOne外键保存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16174213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com