السلام عليكم
قومت بعمل فئه تتعامل مع الHibernate
لكى توفر عمليه كتابه الكود وعمليه التنظيم
كان فى معظم شروحاتنا اننا نكتب كل شى فى داله الـ main لكن هذا ليس من مبادئ الـ OOP
لذا عملت الفئه HibernateBuilder لكى نتعامل معها وسوف تعجبكم كثيرا :)
اى اقتراحات او تعديلات او اى اخطاء يرجى ذكر ذلك :)
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
/**
*
* @author JKelany
*
*/
public class HibernateBuilder {
private Session session;
private AnnotationConfiguration config;
/**
* <ol>
* <li>
* <u>configFileName</u> is the xml configuration file path it may take null
* the default name is <b>hibernate.cfg.xml</b> <br/>
* if the name is null or isEmpty or not end with extension xml <br/>
* the default name is used.</li>
* <li>
* <u>create</u> is boolean if <b>true</b> make tables <br/>
*
* @see SchemaExport </li> <li>
* <u>cls</u> is array of classes <br/>
* that have @Entity Annotation <br/>
* it may be one or more. <br/>
* then get the current session</li>
*
* @param configFileName
* @param create
* @param cls
* @see AnnotationConfiguration
*/
@SuppressWarnings("rawtypes")
public void makeConfigurationProcess(String configFileName, boolean create,
Class... cls) {
AnnotationConfiguration config = new AnnotationConfiguration();
for (Class c : cls) {
System.out.println(c.getName());
config.addAnnotatedClass(c);
}
if (configFileName != null && !configFileName.isEmpty()
&& configFileName.endsWith("xml")) {
config.configure(configFileName);
} else {
config.configure();
}
if (create) {
new SchemaExport(config).create(true, true);
}
this.config = config;
makeSession();
}
/**
* take the current session and return it
*
* @see SessionFactory
* @see Session
* @return Session object
* @throws HibernateException
* , SessionException
*/
private Session makeSession() {
if (config != null) {
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.getCurrentSession();
if (session == null) {
session = sessionFactory.openSession();
if (session == null) {
throw new SessionException(
"problem : can't creating the session");
}
}
} else {
throw new HibernateException(
"You should have AnnotationConfiguration refrence first \n"
+ "by using the makeConfigurationProcess method");
}
return session;
}
/**
* take the classes that has @Entity Annotation <br/>
* and add them to the session <br/>
* to be added to the database after commit {@link #commit()}
*
* @see Session
* @param objects
*/
public void save(Object... objects) {
for (Object ob : objects) {
getSession().save(ob);
}
}
/**
* take the classes that has @Entity Annotation <br/>
* and delete them from the session <br/>
* to be deleted from the database after commit
*
* @see Session
* @param objects
*/
public void delete(Object... objects) {
for (Object ob : objects) {
getSession().delete(ob);
}
}
/**
* take the classes that has @Entity Annotation <br/>
* and update them in the session <br/>
* to be updated in the database after commit
*
* @see Session
* @param objects
*/
public void update(Object... objects) {
for (Object ob : objects) {
getSession().update(ob);
}
}
/**
* this is ask the session to begin the transaction process
*
* @see Session
*/
public void beginTransaction() {
getSession().beginTransaction();
}
/**
* to rollback for any saved , updated, deleted objects in the session
*
* @see Session
*/
public void rollback() {
getSession().getTransaction().rollback();
}
/**
* close the session
*
* @see Session
*/
public void close() {
getSession().close();
}
/**
* commit all objects that in the session<br/>
* to make impact in the database
*
* @see Session
*/
public void commit() {
getSession().getTransaction().commit();
}
/**
* return the current session
*
* @see Session
* @return session
*/
public Session getSession() {
if (session == null) {
throw new HibernateException(
"you should create your session first\n"
+ "by calling makeConfigurationProcess method");
}
return session;
}
/**
* return the AnnotationConfiguration object<br/>
* that have all configured classes
*
* @see AnnotationConfiguration
* @return AnnotationConfiguration object
*/
public AnnotationConfiguration getConfig() {
return config;
}
}مثال عليها
سنستشهد بأخر درس موجود لدينا الا وهو الدرس السادس
public class TestManyToMany {
public static void main(String[] args) {
HibernateBuilder hibernateBuilder = new HibernateBuilder();
hibernateBuilder.makeConfigurationProcess("hibernate.cfg.xml", true,
Student.class, Courses.class);
hibernateBuilder.beginTransaction();
{
Courses java = new Courses();
java.setCourseName("JAVA");
Courses oracle = new Courses();
oracle.setCourseName("C++");
Student jkelany = new Student();
jkelany.setStudentName("Mahmoud Kelany");
Student omnia = new Student();
omnia.setStudentName("Omnia");
Student rawan = new Student();
rawan.setStudentName("Rawan");
Student mai = new Student();
mai.setStudentName("Mai");
java.getStudents().add(jkelany);
java.getStudents().add(rawan);
java.getStudents().add(omnia);
oracle.getStudents().add(mai);
oracle.getStudents().add(jkelany);
hibernateBuilder.save(mai, rawan, omnia, jkelany, oracle, java);
}
hibernateBuilder.commit();
}
}