Hibernate não cria banco de dados

2 respostas
SanjuanRJ

Olá, eu estou tentando fazer uma aplicação usando JSF + Hibernate + Spring e estou me enrolando nessa parte de integração do hibernate com o spring.

Eu fiz essa configuração do applicationContext.xml, mas parece que tem algo de errado.

No caso a minha aplicação chegar a rodar, porém ele não está criando o banco de dados.

Por isso vem o erro:
Table 'faclube' doesn't exist

Alguém pode me ajudar???

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="annotatedClasses">
            <list>
                <value>officeMusic.model.Faclube</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
                <prop key="hibernate.connection.password">senha</prop>
                <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/banco</prop>
                <prop key="hibernate.connection.username">username</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

                <!-- Configuração do DB -->

                <!-- Patch the HSQL error for batching -->
                <prop key="hibernate.jdbc.batch_size" >0</prop>

                <!-- Enable Hibernate's automatic session context management -->
                <prop key="current_session_context_class">thread</prop>

                <!-- Disable the second-level cache  -->
                <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>

                <!-- Echo all executed SQL to stdout -->
                <prop key="hibernate.show_sql" >true</prop>
                <prop key="hibernate.format_sql" >true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>

                <!-- Drop and re-create the database schema on startup -->
                <!-- <property name="hbm2ddl.auto">update</property> -->
                <prop key="hbm2ddl.auto">create-drop</prop>
            </props>
        </property>
    </bean>

     <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>

    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>


	<!-- Identifica os beans anotados com @Service, @Repository, @Controller  -->
    <context:component-scan base-package="officeMusic.action" />

	<!-- Habilita a configuracao de beans via anotacoes
	sem a necessidade de usar PersistenceAnnotationBeanPostProcessor,
	AutowiredAnnotationBeanPostProcessor e etc
	 -->
    <context:annotation-config />

    <!-- Trabalha com a anotacao  @Autowired
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
    -->
	<!-- Trabalha com a anotacao  @Resource -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

	<!-- Declaracao de Beans no Spring -->
    <bean id="anexoDao" class="officeMusic.dao.impl.AnexoDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="faclubeDao" class="officeMusic.dao.impl.FaclubeDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
	<!-- Envia E-mail -->
    <bean id="enviarEmail" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="HOST"/>
        <property name="username" value="USUARIO@HOST"/>
        <property name="password" value="SUA_SENHA"/>
    </bean>

</beans>

2 Respostas

W

Olá,

Não sei se já resolveu, mas é o seguinte. Eu nunca fiz desta maneira, porém me parece que falta uma configuração no Hibernate que é:

<mapping class="pojos.Usuario"/>

Pode colocar abaixo da chave

<prop key="hibernate.show_sql" >true</prop>

Eu faço de maneira diferente, crio um arquivo hibernate.cfg.xml da seguinte maneira:

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost/teste</property>
        <property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">root</property>        
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<mapping class="pojos.Usuario"/>
		<mapping class="pojos.Equipe"/>
    </session-factory>
</hibernate-configuration>

E uma classe GeraBanco.java como abaixo e executo

package examples.helloworld;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class GeraBanco {
	public static void main(String[] args) {
	Configuration conf = new AnnotationConfiguration();
	conf.configure();
	SchemaExport se = new SchemaExport(conf);
	se.create(true, true);
	}
}

Espero ajudar.
Depois posta ai o resultado.
Att.
Wallfox

SanjuanRJ

Já resolvi, era uma coisa besta eu esqueci de botar o hibernate. na frente

create-drop
troca por
create-drop

Eu estou usando o spring junto com o hibernate e por isso a minha configuração está um pouco diferente da “normal”.

Mas valeu a ajuda.

Criado 4 de julho de 2009
Ultima resposta 8 de jul. de 2009
Respostas 2
Participantes 2