简体   繁体   中英

Persisting non-owning side object in Hibernate @ManyToMany bidirectional mapping does not persist data in joining table

I am trying to implement ManyToMany bidirectional mapping in Hibernate. But, when I test the application by persisting non-owning side object, Hibernate does not persist data in join table.

Here is my code:

College.java:

@Entity
@Table(name="college")
public class College {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="college_id")
    private int id;

    @Column(name="college_name")
    private String name;

    @ManyToMany(mappedBy="colleges",cascade=CascadeType.ALL)
    private Set<Certification> certifications;

    // getters and setters
}

Certification.java:

@Entity
@Table(name="certification")
public class Certification {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="certification_id")
    private int id;

    @Column(name="certification_name")
    private String name;

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="certification_college",joinColumns=@JoinColumn(name="certification_id"),inverseJoinColumns=@JoinColumn(name="college_id"))
    private Set<College> colleges;

    // getters and setters
}

MainApplication.java:

public class MainApplication{

    public static void main(String[] args) {

        Certification certification1 = new Certification();
        certification1.setName("Java");

        Certification certification2 = new Certification();
        certification2.setName("C++");

        Set<Certification> certifications = new HashSet<Certification>();
        certifications.add(certification1);
        certifications.add(certification2);

        College college1 = new College();
        college1.setName("ABC");
        college1.setCertifications(certifications);

        College college2 = new College();
        college2.setName("XYZ");
        college2.setCertifications(certifications);

        SessionFactory sessionFactory =  HibernateUtil.getSessionFactory();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        session.save(college1);
        session.save(college2);

        transaction.commit();
        sessionFactory.close(); 
    }
}

Here's the SQL Output:

Hibernate: insert into college (college_name) values (?)
Hibernate: insert into certification (certification_name) values (?)
Hibernate: insert into certification (certification_name) values (?)
Hibernate: insert into college (college_name) values (?)

Thank you.

You're missing the @JoinTable annotation in your College.java class. You're persisting an entity that is not aware of the table that maps the relationship.

Add the following code after the @ManyToMany annotation of the "certifications" attribute.

@JoinTable(name="certification_college",joinColumns=@JoinColumn(name="college_id"),inverseJoinColumns=@JoinColumn(name="certification_id"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM