package org.academiadecodigo.enuminatti.hibernatebootstrap.associations.many2many;

import org.academiadecodigo.enuminatti.hibernatebootstrap.SaveUtil;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * MIT License
 * (c) 2017 Ricardo Constantino
 */

public class Many2Many {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
        SaveUtil<SecurityGroup> saveUtilSgroups = new SaveUtil<>(emf);
        SaveUtil<User> saveUtilUsers = new SaveUtil<>(emf);

        try {
            SecurityGroup admin = new SecurityGroup("admin");
            SecurityGroup adminSaved = saveUtilSgroups.save(admin);
            System.out.println(adminSaved);

            User jorge = new User("Jorge");
            jorge.addSgroup(adminSaved);
            User jorgeSaved = saveUtilUsers.save(jorge);
            System.out.println(jorgeSaved);

            System.out.println(adminSaved);
            System.out.println(adminSaved.getUsers());

            System.out.println(saveUtilUsers.findByName(User.class, "Jorge"));

        } finally {
            emf.close();
        }

    }
}
