Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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>
- <!-- Database connection settings -->
- <property name="connection.drive_class">com.mysql.jdbc.Driver</property>
- <property name="connection.url">jdbc:mysql://127.0.0.1:3306/testebanco</property>
- <property name="connection.username">root</property>
- <property name="connection.password">102030</property>
- <!-- JDBC connection pool(use the built-in) -->
- <property name="connection.pool_size">1</property>
- <!-- SQL dialect -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
- <!-- Enable Hibernate's automatic session context management -->
- <property name="current_session_context_class">thread</property>
- <!-- Disable the second-level cache -->
- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
- <!-- Echo all executed SQL to stdout -->
- <property name="show_sql">true</property>
- <!-- Drop and re-create the database schema on startup -->
- <property name="hbm2ddl.auto">create</property>
- <!-- Mapeamento das entidades -->
- <mapping class="teste.DAO.Estado" />
- </session-factory>
- public class HibernateUtil {
- private static SessionFactory sf = createSessionFactory();
- public static SessionFactory getSf() {
- return sf;
- }
- private static SessionFactory createSessionFactory(){
- try{
- Configuration cfg = new Configuration().configure();
- ServiceRegistry registro = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
- SessionFactory f = cfg.buildSessionFactory(registro);
- return f;
- }catch(Throwable ex){
- System.err.println("Erro ao criar a sessão " + ex);
- throw new ExceptionInInitializerError(ex);
- }
- }
- }
- public class HibernateUtilTeste {
- @Test
- public void conectar(){
- Session sessao = HibernateUtil.getSf().openSession();
- sessao.close();
- HibernateUtil.getSf().close();
- }
- }
- @Entity
- @Table
- public class Estado extends GenericID {
- private String nome;
- private String sigla;
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public String getSigla() {
- return sigla;
- }
- public void setSigla(String sigla) {
- this.sigla = sigla;
- }
- @SuppressWarnings("serial")
- @MappedSuperclass
- public class GenericID implements Serializable {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement