How to prevent saving a child using JPA?

I have a OneToMany relationship between school and student organizations. What I want to do is when I save the school object, do not save and do not update the student object (and, of course, do not delete them).

When I try to save a school object as shown below, it also updates my student objects, but I do not want them to be updated, but only connected. Is there any way?

I uninstalled Cascade, but it still does not work.

School school = new School();
school.setStudents(studentList);
repository.save(school);

My essence;

@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "school_student", joinColumns = {@JoinColumn(name = "school_id")},
        inverseJoinColumns = {@JoinColumn(name = "student_id")})
private List<Student> students;

EDIT:

  • When I try to save / update / delete the School object, do not save / update / delete the Student object. Think that I do not have the authority to insert / update / delete in the Student table.
+4
5

, , -, , , . ?

, , , - , , . , , .

, "" " " "". (, , ), , , . , Student s.

, School . , , Student School? , , . , Student School .

School Student s, , School s. , , - cascade @OneToMany .

, , . Student, , , , , .

, , School Student, Student, , ​​. School s, School - .

: JPA Student, School, Student School Student. Student (, School), . School, , Student, School s 'students.

, , / . , , , .

( JPA); , https://blogs.oracle.com/JPQL01/entry/native_query_in_java_persistence. . , EntityManager, , . , , , , .

, Student School ( , ). EntityManager.detach() EntityManager.clear(). , , , , .

, , .

, .

+7

, , "--", . --.

@ManyToMany(cascade = {}, targetEntity = Student.class)
@JoinTable(name = "school_student", joinColumns = {
// references column "id" of "school"
    @JoinColumn(name = "school_id", referencedColumnName = "id")
}, inverseJoinColumns = {
// references column "id" of "student"
    @JoinColumn(name = "student_id", referencedColumnName = "id")
})
private List<Student> students;

.

+2

, @JoinColumn:

@OneToMany
@JoinColumn(name = "school_id", updatable = false, insertable = false)
private List<Student> students;
+2

@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "school_student", joinColumns = {@JoinColumn(name = "school_id")},
    inverseJoinColumns = {@JoinColumn(name = "student_id")})

,

, set

private List<Student> students;

,

+1

school.setStudents(studentList), hibernate many2one . session.clear(), , , . scool , .

package main;

import org.hibernate.Session;

import hibernate.ConnectionFactory;
import hibernate.models.Car;
import hibernate.models.Client;

public class MainClass {

    public static void main(String[] args) {
        try {
            /*
             * if you remove cascading configuration from both side 
             * hibernate will not save unsaved transient instance
             */
            if(ConnectionFactory.buildSessionFactory()){
                Session session = ConnectionFactory.getSessionFactory().openSession();
                session.beginTransaction();
                // save a client instance 
                Client client = new Client();
                client.setName("client");
                session.save(client);
                session.getTransaction().commit();
                session.close();


                session = ConnectionFactory.getSessionFactory().openSession();
                session.beginTransaction();
                Car car = new Car();
                car.setName("Audi");
                client = (Client) session.get(Client.class, 1);
                client.setName("client1");
                car.setClient_id(client);
                /* inset statement
                 * to prevent hibernate from updating client instance just clear
                 * the session and save only the wanted instance 
                 */
                session.clear();
                session.save(car);
                session.getTransaction().commit();
                session.close();

                session = ConnectionFactory.getSessionFactory().openSession();
                session.beginTransaction();
                car = (Car) session.get(Car.class, 1);
                car.setName("Audi1");
                client = (Client) session.get(Client.class, 1);
                client.setName("Charif1");
                car.setClient_id(client);
                /* update statement
                 * to prevent hibernate from saving other instance just clear
                 * the session and update only the wanted instance 
                 */
                session.clear();
                session.saveOrUpdate(car);
                session.getTransaction().commit();
                session.close();

                /**
                 * but you you are doing from the wrong side
                 * you are trying to tell that this client have
                 * this cars but Hibernate will try to link this 
                 * car by editing the many2one field ??!! 
                 * so remove the statement 
                 * school.setStudents(studentList);
                 * 
                 * 
                 */
            }
        } catch (Exception e) {
            // TODO: handle exception
        }



        try {
            ConnectionFactory.getSessionFactory().close();
        } catch (Exception e) {e.printStackTrace();}

    }

}
+1

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


All Articles