Fazer o Hibernate não dropar tabelas

Eu estou iniciando o estudo do hibernate e notei que toda vez que eu inicio minha aplicação, ela dropa todas as tabelas do bando e as recria, já alterei a prorpiedade hibernate.hbm2ddl.auto em todos os parametros que eu conheço mas mesmo assim continua dropando. Alguém tem alguma idéia de como eu faço ele não dar esse drop?

Posta aí o seu persistence XML pra a gente dar uma olhada no seu problema… 8)

<?xml version="1.0" encoding="UTF-8" ?>
<session-factory>  
	<property name="hibernate.dialect">org.hibernate.dialect.MysSQLDialect</property>
	<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
	<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost:9090/academia</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password"></property>
	<property name="show_sql">true</property>
	<property name="hibernate.generate_statistics">false</property>
	<property name="hibernate.use_sql_comments">false</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>  

[quote=DiegoThuran]<?xml version="1.0" encoding="UTF-8" ?>

<session-factory>  
	<property name="hibernate.dialect">org.hibernate.dialect.MysSQLDialect</property>
	<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
	<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost:9090/academia</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password"></property>
	<property name="show_sql">true</property>
	<property name="hibernate.generate_statistics">false</property>
	<property name="hibernate.use_sql_comments">false</property>
	<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>  
[/quote] Cara eu to usando o toplink como provedor de persistencia e postgre com banco, vc tem que procurar uma propriedade análoga a essa: [code][/code] Não tenho certeza mas pelo que eu to vendo me pareçe ser essa: [code]update[/code] Infelismente eu não sei como se deve proceder especificamente no seu caso mas a idéia principal é vc tirar o update e colocar o create :idea:

Boa noite galera !

Muito estranho amigo…

Eu uso assim no persistence.xml:

Posta o seu persistence inteiro…será que não tem mais nada perdido nele não ?

E o Hibernate só atualiza as tabelas se eu faço alterações nas entities.

[]s

Mas eu já o postei inteiro

Vc tem certeza que é esse o XML que o hibernate ta usando?

Quando se utiliza Annotation, a classe de geração de tabelas recebe algo mais ou menos assim…

SchemaExport se = new SchemaExport(cfg); se.create(true, true);

na linha create, é que você seta se é para dropar, ou apenas criar se não existir, dá uma olhada se tem algo parecido em seu código de geração de tabelas, e altere as propriedades true por false.

Tenta apenas validar o schema das tabelas:

tenho sim
a única coisa que tem a mais seria o hibernate.properties
com essa descrição:

hibernate.dialect =org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class =com.mysql.jdbc.Driver
hibernate.connection.url =jdbc:mysql://localhost/academia
hibernate.connection.username =root
hibernate.connection.password =
hibernate.show_sql = true
hibernate.format_sql =true

Mas qual a opcão do SchemaExport que só cria a tabela se não existir já tentei todas aqui e continua dropando

essa é a informação do meu console

INFO - Hibernate Annotations 3.3.0.GA
INFO - Hibernate 3.2.5
INFO - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.hbm2ddl=update, hibernate.dialect=org.hibernate.dialect.MySQLDialect, hibernate.format_sql=true, hibernate.connection.username=root, hibernate.connection.url=jdbc:mysql://localhost/academia, hibernate.bytecode.use_reflection_optimizer=false, hibernate.show_sql=true, hibernate.connection.password=****}
INFO - Bytecode provider name : cglib
INFO - using JDK 1.4 java.sql.Timestamp handling
INFO - Using dialect: org.hibernate.dialect.MySQLDialect
DEBUG - Execute first pass mapping processing
DEBUG - Process hbm files
DEBUG - Process annotated classes
INFO - Binding entity from annotated class: Base.Produto
DEBUG - Binding column DTYPE unique false
DEBUG - Import with entity name=Produto
INFO - Bind entity Base.Produto on table Produto
DEBUG - Processing Base.Produto property annotation
DEBUG - Processing Base.Produto field annotation
DEBUG - Processing annotations of Base.Produto.id
DEBUG - Binding column id unique false
DEBUG - id is an id
DEBUG - building SimpleValue for id
DEBUG - Building property id
DEBUG - Cascading id with null
DEBUG - Bind @Id on id
DEBUG - Processing annotations of Base.Produto.dataInicioVenda
DEBUG - Binding column dataInicioVenda unique false
DEBUG - binding property dataInicioVenda with lazy=false
DEBUG - building SimpleValue for dataInicioVenda
DEBUG - Building property dataInicioVenda
DEBUG - Cascading dataInicioVenda with null
DEBUG - Processing annotations of Base.Produto.nome
DEBUG - Binding column nome unique false
DEBUG - binding property nome with lazy=false
DEBUG - building SimpleValue for nome
DEBUG - Building property nome
DEBUG - Cascading nome with null
DEBUG - Processing annotations of Base.Produto.preco
DEBUG - Binding column preco unique false
DEBUG - binding property preco with lazy=false
DEBUG - building SimpleValue for preco
DEBUG - Building property preco
DEBUG - Cascading preco with null
DEBUG - processing manytoone fk mappings
DEBUG - processing extends queue
DEBUG - processing collection mappings
DEBUG - processing native query and ResultSetMapping mappings
DEBUG - processing association property references
DEBUG - processing foreign key constraints
INFO - Hibernate Validator not found: ignoring
DEBUG - Execute first pass mapping processing
DEBUG - Process hbm files
DEBUG - Process annotated classes
DEBUG - processing manytoone fk mappings
DEBUG - processing extends queue
DEBUG - processing collection mappings
DEBUG - processing native query and ResultSetMapping mappings
DEBUG - processing association property references
DEBUG - processing foreign key constraints
INFO - Hibernate Validator not found: ignoring
INFO - Running hbm2ddl schema export
DEBUG - import file not found: /import.sql

drop table if exists Produto

DEBUG -
drop table if exists Produto

create table Produto (
    id bigint not null auto_increment,
    dataInicioVenda datetime,
    nome varchar(255),
    preco double precision not null,
    primary key (id)
)

DEBUG -
create table Produto (
id bigint not null auto_increment,
dataInicioVenda datetime,
nome varchar(255),
preco double precision not null,
primary key (id)
)
INFO - schema export complete

Vai testando, se não me engano, fica (false, true)

Continua da mesma forma

Que estranho, isto funciona.

hibernate.hbm2ddl.auto = "update"

Coloca como vc cria sua ConnectionFactory.

Assim…:

[code]private static SessionFactory sessionFactory;

static {  
    try {  
        sessionFactory = new AnnotationConfiguration()  
            .configure()  
            .buildSessionFactory();  
          
    } catch (Throwable ex) {  
        // Log exception!  
        throw new ExceptionInInitializerError(ex);  
    }  
}  
 
public static Session getSession() throws HibernateException {  
    return sessionFactory.openSession();  
}  [/code]

[quote=DiegoThuran]Assim…:

[code]private static SessionFactory sessionFactory;

static {  
    try {  
        sessionFactory = new AnnotationConfiguration()  
            .configure()  
            .buildSessionFactory();  
          
    } catch (Throwable ex) {  
        // Log exception!  
        throw new ExceptionInInitializerError(ex);  
    }  
}  
 
public static Session getSession() throws HibernateException {  
    return sessionFactory.openSession();  
}  [/code][/quote]

Faz este teste

		 sessionFactory = new AnnotationConfiguration()  
                .configure()  
                .setProperty(Environment.HBM2DDL_AUTO, "update");
                .buildSessionFactory(); 

Consegui^^

Legal…

Coloca a solução e altera o tópico para [Resolvido].

:slight_smile:

sessionFactory = new AnnotationConfiguration() .configure() .setProperty(Environment.HBM2DDL_AUTO, "update"); .buildSessionFactory();

resolução