簡體   English   中英

使用休眠將對象保存到與另一個表有關系的表中

[英]Saving an object to table that has relationship with another table using hibernate

我有2張桌子。

Student (id,name,class_id)<br>
class(id,name)

student通過class_idclassclass_id

使用休眠並生成對象后,我將有2個對象

Student { int id; String name; Class class }
Class {int id; String name; Set<Student> student}

我想使用休眠( session.save(student) )保存學生信息。 但是,我必須通過class_id來調用class。 因此,是否有任何方法可以在不使用休眠狀態獲取類對象的情況下保存學生?

這只是一個例子,我正在使用一個數據庫,其中一個表與其他表有很多關系,這對我來說很痛苦。

我猜

student.setClass(null);
session.save(student);

應該有所幫助。 雖然我不確定“。但是,我必須通過class_id來調用class”的意思。

您可能正在研究從班級到學生的單向一對多關系(基於上面給出的班級定義)。 您還需要了解級聯。 通過訪問這個這個 ,你說你也有類似的情況,了解關聯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM