Abhijeet Panwar
Abhijeet Panwar

Reputation: 1857

Confused with Hibernate hbm2ddl.auto

I am using hibernate as ORM tool is my project. It worked fine for me previously and created table properly.My last table had around 200 columns ,after some changes now my table need 150 columns.So I have edited my mapped pojo class .Have removed extra attributes and getters setters from it Hb2ddl works perfectly as I can see drop table, create table syntax on my console.But new Table generated by hibernate consists some old column schema values too , which are not being used and does't exist in the mapped pojo?

For checking purpose when I change pojo name then it generates rigth table with 150 columns.

Have added image for first condition.Values below mir_ids are from old schema.I have not used them im my mapped pojo.

Image1: enter image description here :

And when to check I try a differnt name for pojo.It I get correct output .(Those are 166 rows)

Image2: :

enter image description here

Those extra value's are not present and works perfectly fine.But I don't want to do it.I will need to change table name in all project.

what can be the reason for it ? How to correct it?

Note: I have edited mapped class name and it worked for me.I got a new table created with 151 colums as I needed but hibernate still creates that old table itself.Even there is no mapping of that in hibernate.cfg.xml file.

Code for hibernate.cfg.xml file:

<property name="hibernate.connection.autocommit">false</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
        <property name="hibernate.connection.password">abhijeet</property> <property 
        name="hibernate.connection.url">jdbc:mysql://localhost:3306/jdbctest</property> 
        <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.current_session_context_class">thread</property>
     <property name="hibernate.connection.pool_size">1</property>  
<!--    <property name="hibernate.connection.datasource">jdbc/mysql_pool1</property> --> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>  
    <mapping class="com.abhijeet.nabi.pojos.TablePojo" />

Why does hibernate creates an extra table , for which I don't have any mapping.

Upvotes: 0

Views: 2315

Answers (1)

Alexander R&#252;hl
Alexander R&#252;hl

Reputation: 6939

I assume by using the create option you can run into mixed up tables when having structural changes - when you changed your table's name it created a new one and it worked out fine. So during development, you should use the option create-drop - that way you are sure, to have a correct table each time.

During integration and of course production, you should use validate and have the schema created separately.


Update:

I think the problem is a mix of Hibernate and JPA configurations.

You didn't specify the version of Java EE you were using, but since it's Java EE, you do have JPA available and as long as you're not using any proprietary API from Hibernate, there is no need in having a hibernate.cfg.xml, but only a persistence.xml (see here).

Also, you don't need to list all the classes you want to map, but use auto-detection, so it uses all annotated entities automatically.

You also may check out the chapter of the Java EE 7 tutorial.

Upvotes: 1

Related Questions