Save a hibernation object with a foreign key without loading a dependent object

When we save an object in Hibernate, we do not save the dependent object as an identifier, but when loading this object and saving it.

Ex: the employee has a department foreign key, so if we need to save the employee object, we will do something like:

saveEmployee{
emp.setName(name);
Department department = session.find(Department.class,deptid);
emp.setDepartment(department);
}

Now, if we import 1000 records, and there we have defit as a separate column in excel, then unnecessary 1000 times db will be called to get the corresponding department.

Any better way to do this, then Is it possible to use a foreign key without binding the object to the object?

+4
source share
1 answer

Use loadinstead find:

Department department = session.load(Department.class,deptid);

- , ( , ).

, , N - .

+3

Source: https://habr.com/ru/post/1689299/


All Articles