xml:
hibernate.cfg.xml
""<""?xml version="1.0" encoding="UTF-8"?>
""<""!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
""<""hibernate-configuration>
""<""session-factory>
"<"property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect
"<"property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver
"<"property name="hibernate.connection.url">jdbc:mysql://localhost:3306/nomeDoBancoDados
"<"property name="hibernate.connection.username">root
"<"property name="hibernate.connection.password">root
"<"mapping class="br.com.umcastec.bean.Cliente"/>
"<"/session-factory>
"<"/hibernate-configuration>
programação:
HibernateUtil.java
package br.com.umcastec.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
AnnotationConfiguration annotationConfig = new AnnotationConfiguration();
try {
annotationConfig.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
annotationConfig.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
annotationConfig.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/nomeDoBancoDados");
annotationConfig.setProperty("hibernate.connection.username", "root");
annotationConfig.setProperty("hibernate.connection.password", "root");
annotationConfig.addAnnotatedClass(br.com.umcastec.bean.Cliente.class); //add todas as classes "JPA annotated"
} catch (Exception ex) {
ex.printStackTrace();
}
sessionFactory = annotationConfig.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
obs: no trecho de código do xml, coloquei aspas no símbolo "<".