Java


package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import entity.StudentEntity;
/*
* http://stackoverflow.com/questions/4001795/org-hibernate-mappingexception-unknown-entity
* http://www.simplecodestuffs.com/value-object-entity-object-in-hibernate-mapping/
* https://www.developpez.net/forums/d1597126/java/general-java/persistance/hibernate/erreur-unknown-entity/
* la version 4.2 et la version 5.3 ne sont pas compatibles avec son code !
* */

public class HibernateUtil {

public static void main(String[] args) {

Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();

srb.applySettings(cfg.getProperties());
ServiceRegistry sr = srb.build();
SessionFactory sf = cfg.buildSessionFactory(sr);

Session session = sf.openSession();

StudentEntity std = new StudentEntity();
std.setId(24); // Primary Key
std.setName("Jamil");
std.setDepartment("ECE");
std.setCollege("SKCET");

Transaction tx = session.beginTransaction();
session.save(std);
tx.commit();
System.out.println("Object saved successfully.....!!");
session.close();
sf.close();
}
}

Comments are closed