SaveComplexTypeTest.java Source code

Java tutorial

Introduction

Here is the source code for SaveComplexTypeTest.java

Source

import java.util.ArrayList;
import model.entity.Role;
import model.entity.Staff;
import org.hibernate.Session;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author LapTop
 */
public class SaveComplexTypeTest {

    public static void main(String[] args) {
        Session ses = utils.HibernateUtils.getSessionFactory().getCurrentSession();
        Staff s = new Staff();
        ArrayList<Role> role = new ArrayList<>();
        Role r1 = new Role();
        r1.setRoleType(1);
        r1.setStaffId(s);

        Role r2 = new Role();
        r2.setRoleType(0);
        r2.setStaffId(s);
        role.add(r1);
        role.add(r2);
        s.setUserName("godmode");
        s.setPassword("123456");
        s.setRoleCollection(role);
        try {
            ses.beginTransaction();
            ses.save(s);
            ses.getTransaction().commit();
            System.out.println("done");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("false");
            ses.getTransaction().rollback();
        }
        System.exit(0);
    }
}